⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ioutil.java

📁 java插件系统源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2006 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

/**
 * Input/Output, File and URL/URI related utilities.
 *
 * @version $Id: IoUtil.java,v 1.8 2006/10/19 18:41:19 ddimon Exp $
 */
public final class IoUtil {
    private static final String PACKAGE_NAME = "org.java.plugin.util"; //$NON-NLS-1$
    
    /**
     * Copies one file, existing file will be overridden.
     * @param src source file to copy FROM
     * @param dest destination file to copy TO
     * @throws IOException if any I/O error has occurred
     */
    public static void copyFile(final File src, final File dest)
            throws IOException {
        if (!src.isFile()) {
            throw new IOException(
                    ResourceManager.getMessage(PACKAGE_NAME, "notAFile", src)); //$NON-NLS-1$
        }
        if (dest.isDirectory()) {
            throw new IOException(
                    ResourceManager.getMessage(PACKAGE_NAME, "isFolder", dest)); //$NON-NLS-1$
        }
        BufferedInputStream in = new BufferedInputStream(
                new FileInputStream(src));
        try {
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(dest, false));
            try {
                copyStream(in, out, 1024);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
        dest.setLastModified(src.lastModified());
    }

    /**
     * Copies folder recursively, existing files will be overridden
     * @param src source folder
     * @param dest target folder
     * @throws IOException if any I/O error has occurred
     */
    public static void copyFolder(final File src, final File dest)
            throws IOException {
        copyFolder(src, dest, true, false, null);
    }

    /**
     * Copies folder, existing files will be overridden
     * @param src source folder
     * @param dest target folder
     * @param reqursive if <code>true</code>, processes folder recursively
     * @throws IOException if any I/O error has occurred
     */
    public static void copyFolder(final File src, final File dest,
            final boolean reqursive) throws IOException {
        copyFolder(src, dest, reqursive, false, null);
    }


    /**
     * Copies folder.
     * @param src source folder
     * @param dest target folder
     * @param reqursive if <code>true</code>, processes folder recursively
     * @param onlyNew if <code>true</code>, target file will be overridden if it
     *                is older than source file only
     * @throws IOException if any I/O error has occurred
     */
    public static void copyFolder(final File src, final File dest,
            final boolean reqursive, final boolean onlyNew) throws IOException {
        copyFolder(src, dest, reqursive, onlyNew, null);
    }
    
    /**
     * Copies folder.
     * @param src source folder
     * @param dest target folder
     * @param reqursive if <code>true</code>, processes folder recursively
     * @param onlyNew if <code>true</code>, target file will be overridden if it
     *                is older than source file only
     * @param filter file filter, optional, if <code>null</code> all files will
     *               be copied
     * @throws IOException if any I/O error has occurred
     */
    public static void copyFolder(final File src, final File dest,
            final boolean reqursive, final boolean onlyNew,
            final FileFilter filter) throws IOException {
        if (!src.isDirectory()) {
            throw new IOException(
                    ResourceManager.getMessage(PACKAGE_NAME,
                            "notAFolder", src)); //$NON-NLS-1$
        }
        if (dest.isFile()) {
            throw new IOException(
                    ResourceManager.getMessage(PACKAGE_NAME, "isFile", dest)); //$NON-NLS-1$
        }
        if (!dest.exists() && !dest.mkdirs()) {
            throw new IOException(
                    ResourceManager.getMessage(PACKAGE_NAME,
                            "cantMakeFolder", dest)); //$NON-NLS-1$
        }
        File[] srcFiles = src.listFiles();
        for (int i = 0; i < srcFiles.length; i++) {
            File file = srcFiles[i];
            if ((filter != null) && !filter.accept(file)) {
                continue;
            }
            if (file.isDirectory()) {
                if (reqursive) {
                    copyFolder(file, new File(dest, file.getName()), reqursive,
                            onlyNew, filter);
                }
                continue;
            }
            File destFile = new File(dest, file.getName());
            if (onlyNew && destFile.isFile()
                    && (destFile.lastModified() > file.lastModified())) {
                continue;
            }
            copyFile(file, destFile);
        }
        dest.setLastModified(src.lastModified());
    }
    
    /**
     * Copies streams.
     * @param in source stream
     * @param out destination stream
     * @param bufferSize buffer size to use
     * @throws IOException if any I/O error has occurred
     */
    public static void copyStream(final InputStream in, final OutputStream out,
            final int bufferSize) throws IOException {
        byte[] buf = new byte[bufferSize];
        int len;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
    }
    
    /**
     * Recursively deletes whole content of the given folder.
     * @param folder folder to be emptied
     * @return <code>true</code> if given folder becomes empty or not exists
     */
    public static boolean emptyFolder(final File folder) {
        if (!folder.isDirectory()) {
            return true;
        }
        File[] files = folder.listFiles();
        boolean result = true;
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                if (emptyFolder(file)) {
                    result &= file.delete();
                } else {
                    result = false;
                }
            } else {
                result &= file.delete();
            }
        }
        return result;
    }
    
    /**
     * Compares two files for directories/files synchronization purposes.
     * @param file1 one file to compare
     * @param file2 another file to compare
     * @return <code>true</code> if file names are equal (case sensitive), files
     *         have equal lengths and modification dates (milliseconds ignored)
     * 
     * @see #synchronizeFolders(File, File)
     * @see #compareFileDates(Date, Date)
     */
    public static boolean compareFiles(final File file1, final File file2) {
        if (!file1.isFile() || !file2.isFile()) {
            return false;
        }
        if (!file1.getName().equals(file2.getName())) {
            return false;
        }
        if (file1.length() != file2.length()) {
            return false;
        }
        return compareFileDates(new Date(file1.lastModified()),
                new Date(file2.lastModified()));
    }
    
    /**
     * For some reason modification milliseconds for some files are unstable,
     * use this function to compare file dates ignoring milliseconds.
     * @param date1 first file modification date
     * @param date2 second file modification date
     * @return <code>true</code> if files modification dates are equal ignoring
     *         milliseconds
     */
    public static boolean compareFileDates(final Date date1, final Date date2) {
        if ((date1 == null) || (date2 == null)) {
            return false;
        }
        Calendar cldr = Calendar.getInstance(Locale.ENGLISH);
        cldr.setTime(date1);
        cldr.set(Calendar.MILLISECOND, 0);
        long dt1 = cldr.getTimeInMillis();
        cldr.setTime(date2);
        cldr.set(Calendar.MILLISECOND, 0);
        long dt2 = cldr.getTimeInMillis();
        return dt1 == dt2;
    }
    
    /**
     * Performs one-way directories synchronization comparing files only,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -