org.apache.commons.compress.compressors.snappy

## 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:
    Snappy compressed format description
  This class is not thread-safe

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SnappyCompressorOutputStream(OutputStream os, long uncompressedSize)` Constructor using the default block size of 32k.
    `SnappyCompressorOutputStream(OutputStream os, long uncompressedSize, int blockSize)` Constructor using a configurable block size.
    `SnappyCompressorOutputStream(OutputStream os, long uncompressedSize, Parameters params)` Constructor providing full control over the underlying LZ77 compressor.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `static Parameters.Builder` | `createParameterBuilder(int blockSize)` Returns a builder correctly configured for the Snappy algorithm using the gven block size.
    `void` | `finish()` Compresses all remaining data and writes it to the stream, doesn't close the underlying stream.
    `void` | `write(byte[] data, int off, int len)`
    `void` | `write(int b)`

    - ### Methods inherited from class java.io.OutputStream

      `flush, write`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### 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 Detail

    - #### createParameterBuilder

          public static Parameters.Builder createParameterBuilder(int blockSize)

      Returns a builder correctly configured for the Snappy algorithm using the gven block size.
      Parameters:
        `blockSize` - the block size.
      Returns:
        a builder correctly configured for the Snappy algorithm using the gven block size

    - #### close

          public void close()
                     throws IOException

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Overrides:
        `close` in class `OutputStream`
      Throws:
        `IOException`

    - #### finish

          public void finish()
                      throws IOException

      Compresses all remaining data and writes it to the stream, doesn't close the underlying stream.
      Throws:
        `IOException` - if an error occurs

    - #### write

          public void write(byte[] data,
                            int off,
                            int len)
                     throws IOException

      Overrides:
        `write` in class `OutputStream`
      Throws:
        `IOException`

    - #### write

          public void write(int b)
                     throws IOException

      Specified by:
        `write` in class `OutputStream`
      Throws:
        `IOException`

Copyright © 2002–2022 The Apache Software Foundation. All rights reserved.