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);
}
}
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);
}
}
Aggregations