IOUtil.java 2.6 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.aemfd.watchfolder.job;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IOUtil {
    public static void copyFile(File aSource, File aDestination) throws IOException {
        if (!aSource.exists()) {
            throw new IOException("File: " + aSource.getAbsolutePath() + " does not exist.");
        }
        if (aSource.isFile() && aDestination.isFile() && aDestination.exists() && !aDestination.delete()) {
            throw new IOException("File: " + aDestination.getAbsolutePath() + " already exists and cannot be deleted.");
        }
        if (aDestination.isDirectory() && !aDestination.exists() && !aDestination.mkdir()) {
            throw new IOException("Cannot create directory: " + aDestination.getAbsolutePath());
        }
        if (aSource.isDirectory()) {
            if (!aDestination.isDirectory()) {
                throw new IOException("Connot copy directory: " + aSource.getAbsolutePath() + " to file: " + aDestination.getAbsolutePath());
            }
            File[] _files = aSource.listFiles();
            for (int i = 0; i < _files.length; ++i) {
                File _dest = new File(aDestination, _files[i].getName());
                if (_files[i].isDirectory() && !_dest.mkdir()) {
                    throw new IOException("Cannot create directory: " + _dest.getAbsolutePath());
                }
                IOUtil.copyFile(_files[i], _dest);
            }
            return;
        }
        File _dest = null;
        _dest = aDestination.isDirectory() ? new File(aDestination, aSource.getName()) : aDestination;
        FileInputStream _fin = new FileInputStream(aSource);
        FileOutputStream _fout = new FileOutputStream(_dest);
        IOUtil.writeInput2OutputStream(_fin, _fout, 10000, 1);
        _fout.close();
        _fin.close();
    }

    public static void writeInput2OutputStream(InputStream aInputStream, OutputStream aOutputStream, int aBufferSize, int aAutoFlushInterval) throws IOException {
        byte[] _buf = new byte[aBufferSize];
        int x = 0;
        int i = 1;
        while ((x = aInputStream.read(_buf)) > 0) {
            aOutputStream.write(_buf, 0, x);
            _buf = new byte[aBufferSize];
            if (aAutoFlushInterval > 0 && i % aAutoFlushInterval > 0) {
                aOutputStream.flush();
            }
            ++i;
        }
        if (aAutoFlushInterval > 0 && (i - 1) % aAutoFlushInterval == 0) {
            aOutputStream.flush();
        }
    }
}