DurboOutputStream.java 5.14 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Binary
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.io.output.DeferredFileOutputStream
 */
package com.day.durbo.impl;

import com.day.durbo.DurboValue;
import com.day.durbo.io.ChunkedDeflaterOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.DeferredFileOutputStream;

import javax.jcr.Binary;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DurboOutputStream {
    private static final byte[] NULL_BYTES = new byte[]{0, 0, 0, 0};
    private static final byte[] OVERFLOW_MARKER = new byte[]{-1, -1, -1, -1};
    private OutputStream out;

    public DurboOutputStream(OutputStream out) {
        this.out = out;
    }

    public void enableCompression() {
        ChunkedDeflaterOutputStream os = new ChunkedDeflaterOutputStream(this.out);
        os.setAutoRestart(0x100000);
        this.out = os;
    }

    public void writeInt(int i) throws IOException {
        if (i == 0) {
            this.out.write(NULL_BYTES);
        } else {
            this.out.write(new byte[]{(byte)(i >> 24), (byte)(i >> 16), (byte)(i >> 8), (byte)i});
        }
    }

    public void writeSize(long l) throws IOException {
        if (l == 0) {
            this.out.write(NULL_BYTES);
        } else if (l < Integer.MAX_VALUE) {
            this.out.write(new byte[]{(byte)(l >> 24), (byte)(l >> 16), (byte)(l >> 8), (byte)l});
        } else {
            this.out.write(OVERFLOW_MARKER);
            this.out.write(new byte[]{(byte)(l >> 56), (byte)(l >> 48), (byte)(l >> 40), (byte)(l >> 32), (byte)(l >> 24), (byte)(l >> 16), (byte)(l >> 8), (byte)l});
        }
    }

    public void writeByte(int b) throws IOException {
        this.out.write(b);
    }

    public void write(String s) throws IOException {
        this.write(s.getBytes("utf-8"));
    }

    public void write(byte[] data) throws IOException {
        this.writeInt(data.length);
        this.out.write(data);
    }

    public void write(InputStream in, int size) throws IOException {
        this.write(in, (long)size);
    }

    public void write(InputStream in, long size) throws IOException {
        if (size < 0) {
            throw new IllegalArgumentException("Size must be greater than zero");
        }
        this.writeSize(size);
        byte[] buffer = new byte[8192];
        int rd = 0;
        long num = size;
        while (num > 0 && rd >= 0) {
            rd = in.read(buffer, 0, (int)Math.min(num, (long)buffer.length));
            if (rd <= 0) continue;
            this.out.write(buffer, 0, rd);
            num -= (long)rd;
        }
        long written = size - num;
        for (long left = size - written; left > 0; --left) {
            this.out.write(0);
        }
    }

    public void write(InputStream in) throws IOException {
        int MAX_MEM = 1048576;
        DeferredFileOutputStream fout = new DeferredFileOutputStream(1048576, "__durbo", ".tmp", null);
        IOUtils.copy((InputStream)in, (OutputStream)fout);
        fout.close();
        this.writeSize(fout.getByteCount());
        if (fout.isInMemory()) {
            this.out.write(fout.getData());
        } else {
            in = FileUtils.openInputStream((File)fout.getFile());
            IOUtils.copy((InputStream)in, (OutputStream)this.out);
            IOUtils.closeQuietly((InputStream)in);
            fout.getFile().delete();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void write(Value value) throws IOException, RepositoryException {
        if (value.getType() == 2) {
            Binary bin = value.getBinary();
            this.writeSize(bin.getSize());
            InputStream in = bin.getStream();
            try {
                IOUtils.copy((InputStream)in, (OutputStream)this.out);
            }
            finally {
                IOUtils.closeQuietly((InputStream)in);
                bin.dispose();
            }
        }
        this.write(value.getString());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void write(DurboValue value) throws IOException {
        if (value.getType() == 2) {
            if (value.isByteArrayBased()) {
                this.write(value.getBytes());
                return;
            }
            this.writeSize(value.getLength());
            InputStream in = value.getStream();
            try {
                IOUtils.copy((InputStream)in, (OutputStream)this.out);
                return;
            }
            finally {
                IOUtils.closeQuietly((InputStream)in);
            }
        } else {
            this.write(value.getString());
        }
    }

    public void close() throws IOException {
        this.out.close();
    }
}