Class AbstractSnappyOutputStream

java.lang.Object
java.io.OutputStream
org.iq80.snappy.AbstractSnappyOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
SnappyFramedOutputStream, SnappyOutputStream

abstract class AbstractSnappyOutputStream extends OutputStream
This is a base class supporting both the SnappyOutputStream and SnappyFramedOutputStream.

Delegates writing the header bytes and individual frames to the specific implementations. Implementations may also override the crc32 checksum calculation.

Since:
0.4
  • Field Details

    • recycler

      private final BufferRecycler recycler
    • blockSize

      private final int blockSize
    • buffer

      private final byte[] buffer
    • outputBuffer

      private final byte[] outputBuffer
    • minCompressionRatio

      private final double minCompressionRatio
    • out

      private final OutputStream out
    • position

      private int position
    • closed

      private boolean closed
  • Constructor Details

    • AbstractSnappyOutputStream

      public AbstractSnappyOutputStream(OutputStream out, int blockSize, double minCompressionRatio) throws IOException
      Parameters:
      out - The underlying OutputStream to write to. Must not be null.
      blockSize - The block size (of raw data) to compress before writing frames to out.
      minCompressionRatio - Defines the minimum compression ratio (compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].
      Throws:
      IOException
  • Method Details

    • writeHeader

      protected abstract void writeHeader(OutputStream out) throws IOException
      Writes the implementation specific header or "marker bytes" to out.
      Parameters:
      out - The underlying OutputStream.
      Throws:
      IOException
    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] input, int offset, int length) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • flush

      public final void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • close

      public final void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • copyToBuffer

      private void copyToBuffer(byte[] input, int offset, int length)
    • flushBuffer

      private void flushBuffer() throws IOException
      Compresses and writes out any buffered data. This does nothing if there is no currently buffered data.
      Throws:
      IOException
    • writeCompressed

      private void writeCompressed(byte[] input, int offset, int length) throws IOException
      Calculates the crc, compresses the data, determines if the compression ratio is acceptable and calls writeBlock(OutputStream, byte[], int, int, boolean, int) to actually write the frame.
      Parameters:
      input - The byte[] containing the raw data to be compressed.
      offset - The offset into input where the data starts.
      length - The amount of data in input.
      Throws:
      IOException
    • calculateCRC32C

      protected int calculateCRC32C(byte[] data, int offset, int length)
      Calculates a CRC32C checksum over the data.

      This can be overridden to provider alternative implementations (such as returning 0 if checksums are not desired).

      Returns:
      The CRC32 checksum.
    • writeBlock

      protected abstract void writeBlock(OutputStream out, byte[] data, int offset, int length, boolean compressed, int crc32c) throws IOException
      Write a frame (block) to out.
      Parameters:
      out - The OutputStream to write to.
      data - The data to write.
      offset - The offset in data to start at.
      length - The length of data to use.
      compressed - Indicates if data is the compressed or raw content. This is based on whether the compression ratio desired is reached.
      crc32c - The calculated checksum.
      Throws:
      IOException