IOUtils.java 3.59 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.References
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemfd.docmanager.io;

import com.adobe.aemfd.docmanager.TempFileManager;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.References;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1)
@References(value={@Reference(name="tempFileManager", referenceInterface=TempFileManager.class)})
public class IOUtils {
    private static final int BUFFER_SIZE = 4096;
    private static final OutputStream NULL_OUTPUT_STREAM = new NullOutputStream();
    private static TempFileManager tempFileManager;
    private static final Logger logger;

    public static long copy(InputStream is, OutputStream os, Long maxToRead) throws IOException {
        int bufSize;
        long leeway;
        int read;
        long totalRead = 0;
        byte[] buffer = new byte[4096];
        long l = leeway = maxToRead == null ? Long.MAX_VALUE : maxToRead;
        while (leeway > 0 && (read = is.read(buffer, 0, bufSize = leeway >= (long)buffer.length ? buffer.length : (int)leeway)) != -1) {
            os.write(buffer, 0, read);
            totalRead += (long)read;
            leeway -= (long)read;
        }
        os.flush();
        return totalRead;
    }

    public static long copy(InputStream is, OutputStream os) throws IOException {
        return IOUtils.copy(is, os, null);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static long copyToFile(InputStream is, File f) throws IOException {
        FileOutputStream fos = new FileOutputStream(f);
        try {
            long l = IOUtils.copy(is, fos);
            return l;
        }
        finally {
            try {
                fos.close();
            }
            catch (IOException e) {
                logger.warn("Error closing output stream for file " + f.getAbsolutePath(), (Throwable)e);
            }
        }
    }

    public static long length(InputStream is) throws IOException {
        return IOUtils.copy(is, NULL_OUTPUT_STREAM);
    }

    public static byte[] getBytes(InputStream is, Integer maxToRead) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
        IOUtils.copy(is, baos, maxToRead == null ? null : Long.valueOf(maxToRead.longValue()));
        return baos.toByteArray();
    }

    public static File createTempFile(String suffix) throws IOException {
        return tempFileManager.getTempFile("docmanager-", suffix == null ? ".dm" : suffix);
    }

    public static TempFileManager getTempFileManager() {
        return tempFileManager;
    }

    public void bindTempFileManager(TempFileManager tfm) {
        tempFileManager = tfm;
    }

    public void unbindTempFileManager(TempFileManager tfm) {
        tempFileManager = null;
    }

    static {
        logger = LoggerFactory.getLogger(IOUtils.class);
    }

    private static class NullOutputStream
    extends OutputStream {
        private NullOutputStream() {
        }

        public void write(int b) throws IOException {
        }

        public void write(byte[] b, int off, int len) throws IOException {
        }
    }

}