use of net.i2p.util.ByteCache in project i2p.i2p by i2p.
the class DataHelper method copy.
/**
* Copy in to out. Caller MUST close the streams.
*
* @param in non-null
* @param out non-null
* @since 0.9.29
*/
public static void copy(InputStream in, OutputStream out) throws IOException {
final ByteCache cache = ByteCache.getInstance(8, 8 * 1024);
final ByteArray ba = cache.acquire();
try {
final byte[] buf = ba.getData();
int read;
while ((read = in.read(buf)) != -1) {
out.write(buf, 0, read);
}
} finally {
cache.release(ba);
}
}
use of net.i2p.util.ByteCache 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