Class StreamCompressor

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public abstract class StreamCompressor
    extends java.lang.Object
    implements java.io.Closeable
    Encapsulates a Deflater and CRC calculator, handling multiple types of output streams. Currently ZipEntry.DEFLATED and ZipEntry.STORED are the only supported compression methods.
    Since:
    1.10
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      static StreamCompressor create​(int compressionLevel, ScatterGatherBackingStore bs)
      Creates a stream compressor with the given compression level.
      static StreamCompressor create​(ScatterGatherBackingStore bs)
      Creates a stream compressor with the default compression level.
      void deflate​(java.io.InputStream source, int method)
      Deflates the given source using the supplied compression method
      long getBytesRead()
      Gets the number of bytes read from the source stream
      long getBytesWrittenForLastEntry()
      Gets the number of bytes written to the output for the last entry
      long getCrc32()
      Gets the CRC-32 of the last deflated file
      long getTotalBytesWritten()
      Gets the total number of bytes written to the output for all files
      void writeCounted​(byte[] data)
      Writes the specified byte array to the output stream.
      void writeCounted​(byte[] data, int offset, int length)
      Writes len bytes from the specified byte array starting at offset off to the output stream.
      protected abstract void writeOut​(byte[] data, int offset, int length)
      Writes len bytes from the specified byte array starting at offset off to the output stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • create

        public static StreamCompressor create​(int compressionLevel,
                                              ScatterGatherBackingStore bs)
        Creates a stream compressor with the given compression level.
        Parameters:
        compressionLevel - The Deflater compression level
        bs - The ScatterGatherBackingStore to receive output
        Returns:
        A stream compressor
      • create

        public static StreamCompressor create​(ScatterGatherBackingStore bs)
        Creates a stream compressor with the default compression level.
        Parameters:
        bs - The ScatterGatherBackingStore to receive output
        Returns:
        A stream compressor
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • deflate

        public void deflate​(java.io.InputStream source,
                            int method)
                     throws java.io.IOException
        Deflates the given source using the supplied compression method
        Parameters:
        source - The source to compress
        method - The #ZipArchiveEntry compression method
        Throws:
        java.io.IOException - When failures happen
      • getBytesRead

        public long getBytesRead()
        Gets the number of bytes read from the source stream
        Returns:
        The number of bytes read, never negative
      • getBytesWrittenForLastEntry

        public long getBytesWrittenForLastEntry()
        Gets the number of bytes written to the output for the last entry
        Returns:
        The number of bytes, never negative
      • getCrc32

        public long getCrc32()
        Gets the CRC-32 of the last deflated file
        Returns:
        the CRC-32
      • getTotalBytesWritten

        public long getTotalBytesWritten()
        Gets the total number of bytes written to the output for all files
        Returns:
        The number of bytes, never negative
      • writeCounted

        public void writeCounted​(byte[] data)
                          throws java.io.IOException
        Writes the specified byte array to the output stream.
        Parameters:
        data - the data.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • writeCounted

        public void writeCounted​(byte[] data,
                                 int offset,
                                 int length)
                          throws java.io.IOException
        Writes len bytes from the specified byte array starting at offset off to the output stream.
        Parameters:
        data - the data.
        offset - the start offset in the data.
        length - the number of bytes to write.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • writeOut

        protected abstract void writeOut​(byte[] data,
                                         int offset,
                                         int length)
                                  throws java.io.IOException
        Writes len bytes from the specified byte array starting at offset off to the output stream.
        Parameters:
        data - the data.
        offset - the start offset in the data.
        length - the number of bytes to write.
        Throws:
        java.io.IOException - if an I/O error occurs.