All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class ReusableGZIPOutputStreamextends ResettableGZIPOutputStream
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 Details

    • ENABLE_CACHING

      private static final boolean ENABLE_CACHING
      On 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

      private static final LinkedBlockingQueue<ReusableGZIPOutputStream> _available
      Streams ready for reuse.
    • _buffer

      private final ByteArrayOutputStream _buffer
      Compressed bytes waiting to be written out.
  • Constructor Details

    • ReusableGZIPOutputStream

      private ReusableGZIPOutputStream()
      Makes a stream with an empty buffer.
  • Method Details

    • acquire

      public static ReusableGZIPOutputStream 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

      public static void release(ReusableGZIPOutputStream out)
      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:
      reset in class ResettableGZIPOutputStream
    • setLevel

      public void setLevel(int level)
      Configure the compression level.
      Parameters:
      level - a Deflater constant 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.