Search in sources :

Example 1 with ReusableGZIPInputStream

use of net.i2p.util.ReusableGZIPInputStream in project i2p.i2p by i2p.

the class NewsFetcher method gunzip.

/**
 *  Gunzip the file
 *
 *  @since 0.9.17
 */
private static void gunzip(File from, File to) throws IOException {
    ReusableGZIPInputStream in = ReusableGZIPInputStream.acquire();
    OutputStream out = null;
    try {
        in.initialize(new FileInputStream(from));
        out = new SecureFileOutputStream(to);
        DataHelper.copy(in, out);
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException ioe) {
            }
        ReusableGZIPInputStream.release(in);
    }
}
Also used : SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ReusableGZIPInputStream(net.i2p.util.ReusableGZIPInputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 2 with ReusableGZIPInputStream

use of net.i2p.util.ReusableGZIPInputStream in project i2p.i2p by i2p.

the class DataHelper method decompress.

/**
 *  Decompress the GZIP compressed data (returning null on error).
 *  @throws IOException if uncompressed is over 40 KB,
 *                      or on a decompression error
 *  @return null if orig is null
 */
public static byte[] decompress(byte[] orig, int offset, int length) throws IOException {
    if (orig == null)
        return orig;
    if (offset + length > orig.length)
        throw new IOException("Bad params arrlen " + orig.length + " off " + offset + " len " + length);
    ReusableGZIPInputStream in = ReusableGZIPInputStream.acquire();
    in.initialize(new ByteArrayInputStream(orig, offset, length));
    // don't make this a static field, or else I2PAppContext gets initialized too early
    ByteCache cache = ByteCache.getInstance(8, MAX_UNCOMPRESSED);
    ByteArray outBuf = cache.acquire();
    try {
        int written = 0;
        while (true) {
            int read = in.read(outBuf.getData(), written, MAX_UNCOMPRESSED - written);
            if (read == -1)
                break;
            written += read;
            if (written >= MAX_UNCOMPRESSED) {
                if (in.available() > 0)
                    throw new IOException("Uncompressed data larger than " + MAX_UNCOMPRESSED);
                break;
            }
        }
        byte[] rv = new byte[written];
        System.arraycopy(outBuf.getData(), 0, rv, 0, written);
        return rv;
    } finally {
        cache.release(outBuf);
        ReusableGZIPInputStream.release(in);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteCache(net.i2p.util.ByteCache) ReusableGZIPInputStream(net.i2p.util.ReusableGZIPInputStream) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 ReusableGZIPInputStream (net.i2p.util.ReusableGZIPInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ByteCache (net.i2p.util.ByteCache)1 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)1