Class SnappyCompressorOutputStream

java.lang.Object
java.io.OutputStream
org.apache.commons.compress.compressors.CompressorOutputStream
org.apache.commons.compress.compressors.snappy.SnappyCompressorOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class SnappyCompressorOutputStream extends CompressorOutputStream
CompressorOutputStream for the raw Snappy format.

This implementation uses an internal buffer in order to handle the back-references that are at the heart of the LZ77 algorithm. The size of the buffer must be at least as big as the biggest offset used in the compressed stream. The current version of the Snappy algorithm as defined by Google works on 32k blocks and doesn't contain offsets bigger than 32k which is the default block size used by this class.

The raw Snappy format requires the uncompressed size to be written at the beginning of the stream using a varint representation, i.e. the number of bytes needed to write the information is not known before the uncompressed size is known. We've chosen to make the uncompressedSize a parameter of the constructor in favor of buffering the whole output until the size is known. When using the FramedSnappyCompressorOutputStream this limitation is taken care of by the warpping framing format.

Since:
1.14
See Also:
  • Field Details

    • compressor

      private final LZ77Compressor compressor
    • os

      private final OutputStream os
    • consumer

      private final ByteUtils.ByteConsumer consumer
    • oneByte

      private final byte[] oneByte
    • finished

      private boolean finished
    • MAX_LITERAL_SIZE_WITHOUT_SIZE_BYTES

      private static final int MAX_LITERAL_SIZE_WITHOUT_SIZE_BYTES
      See Also:
    • MAX_LITERAL_SIZE_WITH_ONE_SIZE_BYTE

      private static final int MAX_LITERAL_SIZE_WITH_ONE_SIZE_BYTE
      See Also:
    • MAX_LITERAL_SIZE_WITH_TWO_SIZE_BYTES

      private static final int MAX_LITERAL_SIZE_WITH_TWO_SIZE_BYTES
      See Also:
    • MAX_LITERAL_SIZE_WITH_THREE_SIZE_BYTES

      private static final int MAX_LITERAL_SIZE_WITH_THREE_SIZE_BYTES
      See Also:
    • ONE_SIZE_BYTE_MARKER

      private static final int ONE_SIZE_BYTE_MARKER
      See Also:
    • TWO_SIZE_BYTE_MARKER

      private static final int TWO_SIZE_BYTE_MARKER
      See Also:
    • THREE_SIZE_BYTE_MARKER

      private static final int THREE_SIZE_BYTE_MARKER
      See Also:
    • FOUR_SIZE_BYTE_MARKER

      private static final int FOUR_SIZE_BYTE_MARKER
      See Also:
    • MIN_MATCH_LENGTH_WITH_ONE_OFFSET_BYTE

      private static final int MIN_MATCH_LENGTH_WITH_ONE_OFFSET_BYTE
      See Also:
    • MAX_MATCH_LENGTH_WITH_ONE_OFFSET_BYTE

      private static final int MAX_MATCH_LENGTH_WITH_ONE_OFFSET_BYTE
      See Also:
    • MAX_OFFSET_WITH_ONE_OFFSET_BYTE

      private static final int MAX_OFFSET_WITH_ONE_OFFSET_BYTE
      See Also:
    • MAX_OFFSET_WITH_TWO_OFFSET_BYTES

      private static final int MAX_OFFSET_WITH_TWO_OFFSET_BYTES
      See Also:
    • ONE_BYTE_COPY_TAG

      private static final int ONE_BYTE_COPY_TAG
      See Also:
    • TWO_BYTE_COPY_TAG

      private static final int TWO_BYTE_COPY_TAG
      See Also:
    • FOUR_BYTE_COPY_TAG

      private static final int FOUR_BYTE_COPY_TAG
      See Also:
    • MIN_MATCH_LENGTH

      private static final int MIN_MATCH_LENGTH
      See Also:
    • MAX_MATCH_LENGTH

      private static final int MAX_MATCH_LENGTH
      See Also:
  • Constructor Details

    • SnappyCompressorOutputStream

      public SnappyCompressorOutputStream(OutputStream os, long uncompressedSize) throws IOException
      Constructor using the default block size of 32k.
      Parameters:
      os - the outputstream to write compressed data to
      uncompressedSize - the uncompressed size of data
      Throws:
      IOException - if writing of the size fails
    • SnappyCompressorOutputStream

      public SnappyCompressorOutputStream(OutputStream os, long uncompressedSize, int blockSize) throws IOException
      Constructor using a configurable block size.
      Parameters:
      os - the outputstream to write compressed data to
      uncompressedSize - the uncompressed size of data
      blockSize - the block size used - must be a power of two
      Throws:
      IOException - if writing of the size fails
    • SnappyCompressorOutputStream

      public SnappyCompressorOutputStream(OutputStream os, long uncompressedSize, Parameters params) throws IOException
      Constructor providing full control over the underlying LZ77 compressor.
      Parameters:
      os - the outputstream to write compressed data to
      uncompressedSize - the uncompressed size of data
      params - the parameters to use by the compressor - note that the format itself imposes some limits like a maximum match length of 64 bytes
      Throws:
      IOException - if writing of the size fails
  • Method Details