Class ZCompressorOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class ZCompressorOutputStream extends FilterOutputStream implements AutoCloseable
A subclass of OutputStream that will compress data as it writes to another OutputStream. Not thread safe. The class implements AutoCloseable. It can be used with the AutoCloseable with a try-with-resources block or without, as shown in the two examples below: Example 1: Allocate and close a ZFile when a try-with-resources block is not used. The an explicit call to the close() close the ZFile object and release the associated resources. The close() method is called in a finally block to assure that the close is performed after the try block completes.
   ZFile zfile = new  new ZFile(fileName, options);
   try {
       byte[] recBuf = new byte[lrecl];
       // read the records sequentially
       while (zfile.read(recBuf) != -1) {
           String record = new String(recBuf);
           System.out.println("Record=" + record);
       }
   }
   finally {
     zfile.close();
   }
  
Example 2: Allocate a ZFile when a try-with-resources block is used. The method close() is not needed. Upon exiting a try-with-resources block, the AutoCloseable will automatically call the close() method which will lose the ZFile object and release the associated resources.
   try (ZFile zfile = new  new ZFile(fileName, options)) {
       byte[] recBuf = new byte[lrecl];
       // read the records sequentially
       while (zfile.read(recBuf) != -1) {
           String record = new String(recBuf);
           System.out.println("Record=" + record);
       }
   }
   ...
  
Since:
2.4.4
See Also:
  • Constructor Details

    • ZCompressorOutputStream

      public ZCompressorOutputStream(ZCompressor compressor, OutputStream out)
      Create a new output stream with default buffer size.
      Parameters:
      compressor - the ZCompressor object used to compress data
      out - the target OutputStream for compressed data
    • ZCompressorOutputStream

      public ZCompressorOutputStream(ZCompressor compressor, OutputStream out, int buflen)
      Create a new output stream with the specified buffer size.
      Parameters:
      compressor - the ZCompressor object used to compress data
      out - the target OutputStream for compressed data
      buflen - internal buffer for compressed data
  • Method Details

    • releaseZCompressorOnClose

      public void releaseZCompressorOnClose(boolean value)
      Sets a flag that will cause the ZCompressor resources to be released when this OutputStream is closed. Defaults to false.
    • getTotalSourceBytes

      public long getTotalSourceBytes()
      Answer the total number of input (uncompressed) bytes written so far.
      Returns:
      the total number of input (uncompressed) bytes written
    • getTotalCompressedBytes

      public long getTotalCompressedBytes()
      Answer the total number of compressed bytes written so far.
      Returns:
      the total number of compressed bytes written
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
      See Also:
    • write

      public void write(int b) throws IOException
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
      See Also:
    • close

      public void close() throws IOException
      This method also flushes data, including any partially compressed last byte.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterOutputStream
      Throws:
      IOException
      See Also:
    • flush

      public void flush() throws IOException
      This flushes the buffer of all compressed data, excluding a possible partial last byte. Remaining unused bits in such a partial byte may be used for compressing more data, and is only written when the stream is closed.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class FilterOutputStream
      Throws:
      IOException
      See Also: