java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.util.zip.DeflaterOutputStream
net.i2p.util.ResettableGZIPOutputStream
net.i2p.util.ReusableGZIPOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Provide a cache of reusable GZIP streams, each handling up to 40 KB output without expansion.
This compresses to memory only. Retrieve the compressed data with getData(). There is no facility to compress to an output stream.
Do NOT use this for compression of unlimited-size data, as it will expand, but never release, the BAOS memory buffer.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final LinkedBlockingQueue<ReusableGZIPOutputStream> Streams ready for reuse.private final ByteArrayOutputStreamCompressed bytes waiting to be written out.private static final booleanOn Apache Harmony 5.0M13 the Deflater does not work after reset(), and neither does Android's - on those platforms every instance is destroyed on release rather than reset and pooled, so caching is disabled there.Fields inherited from class DeflaterOutputStream
buf, defFields inherited from class FilterOutputStream
outConstructor Summary
ConstructorsModifierConstructorDescriptionprivateMakes a stream with an empty buffer.Method Summary
Modifier and TypeMethodDescriptionstatic ReusableGZIPOutputStreamacquire()Pull a cached instance.static voidClear the cache.byte[]getData()Pull the contents of the stream written.static voidRelease an instance back into the cache (this will discard any state).voidreset()Clear the data so we can start again afresh.voidsetLevel(int level) Configure the compression level.Methods inherited from class DeflaterOutputStream
deflate, flushMethods inherited from class OutputStream
nullOutputStream
Field Details
ENABLE_CACHING
private static final boolean ENABLE_CACHINGOn Apache Harmony 5.0M13 the Deflater does not work after reset(), and neither does Android's - on those platforms every instance is destroyed on release rather than reset and pooled, so caching is disabled there._available
Streams ready for reuse._buffer
Compressed bytes waiting to be written out.
Constructor Details
ReusableGZIPOutputStream
private ReusableGZIPOutputStream()Makes a stream with an empty buffer.
Method Details
acquire
Pull a cached instance.- Returns:
- a stream from the pool, or a newly allocated one when the pool is empty or caching is disabled
release
Release an instance back into the cache (this will discard any state).- Parameters:
out- the stream to return to the pool; it is destroyed instead if the pool is disabled or full
reset
public void reset()Clear the data so we can start again afresh.- Overrides:
resetin classResettableGZIPOutputStream
setLevel
public void setLevel(int level) Configure the compression level.- Parameters:
level- aDeflaterconstant from NO_COMPRESSION to BEST_COMPRESSION
getData
public byte[] getData()Pull the contents of the stream written.- Returns:
- the compressed bytes accumulated since the last
reset()
clearCache
public static void clearCache()Clear the cache.