Class AbstractSnappyOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    SnappyFramedOutputStream, SnappyOutputStream

    abstract class AbstractSnappyOutputStream
    extends java.io.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
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractSnappyOutputStream​(java.io.OutputStream out, int blockSize, double minCompressionRatio)  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected int calculateCRC32C​(byte[] data, int offset, int length)
      Calculates a CRC32C checksum over the data.
      void close()  
      private void copyToBuffer​(byte[] input, int offset, int length)  
      void flush()  
      private void flushBuffer()
      Compresses and writes out any buffered data.
      void write​(byte[] input, int offset, int length)  
      void write​(int b)  
      protected abstract void writeBlock​(java.io.OutputStream out, byte[] data, int offset, int length, boolean compressed, int crc32c)
      Write a frame (block) to out.
      private void writeCompressed​(byte[] input, int offset, int length)
      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.
      protected abstract void writeHeader​(java.io.OutputStream out)
      Writes the implementation specific header or "marker bytes" to out.
      • Methods inherited from class java.io.OutputStream

        nullOutputStream, write
      • Methods inherited from class java.lang.Object

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

      • blockSize

        private final int blockSize
      • buffer

        private final byte[] buffer
      • outputBuffer

        private final byte[] outputBuffer
      • minCompressionRatio

        private final double minCompressionRatio
      • out

        private final java.io.OutputStream out
      • position

        private int position
      • closed

        private boolean closed
    • Constructor Detail

      • AbstractSnappyOutputStream

        public AbstractSnappyOutputStream​(java.io.OutputStream out,
                                          int blockSize,
                                          double minCompressionRatio)
                                   throws java.io.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:
        java.io.IOException
    • Method Detail

      • writeHeader

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

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

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

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

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

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

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

        private void writeCompressed​(byte[] input,
                                     int offset,
                                     int length)
                              throws java.io.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:
        java.io.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​(java.io.OutputStream out,
                                           byte[] data,
                                           int offset,
                                           int length,
                                           boolean compressed,
                                           int crc32c)
                                    throws java.io.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:
        java.io.IOException