Class SnappyFramedOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable, java.nio.channels.Channel, java.nio.channels.WritableByteChannel

    public final class SnappyFramedOutputStream
    extends java.io.OutputStream
    implements java.nio.channels.WritableByteChannel
    Implements the x-snappy-framed as an OutputStream and WritableByteChannel.
    Since:
    1.1.0
    • Field Detail

      • MAX_BLOCK_SIZE

        public static final int MAX_BLOCK_SIZE
        The x-snappy-framed specification allows for a chunk size up to 16,777,211 bytes in length. However, it also goes on to state:

        We place an additional restriction that the uncompressed data in a chunk must be no longer than 65536 bytes. This allows consumers to easily use small fixed-size buffers.

        See Also:
        Constant Field Values
      • DEFAULT_BLOCK_SIZE

        public static final int DEFAULT_BLOCK_SIZE
        The default block size to use.
        See Also:
        Constant Field Values
      • DEFAULT_MIN_COMPRESSION_RATIO

        public static final double DEFAULT_MIN_COMPRESSION_RATIO
        The default min compression ratio to use.
        See Also:
        Constant Field Values
      • crc32

        private final java.util.zip.Checksum crc32
      • headerBuffer

        private final java.nio.ByteBuffer headerBuffer
      • blockSize

        private final int blockSize
      • buffer

        private final java.nio.ByteBuffer buffer
      • directInputBuffer

        private final java.nio.ByteBuffer directInputBuffer
      • outputBuffer

        private final java.nio.ByteBuffer outputBuffer
      • minCompressionRatio

        private final double minCompressionRatio
      • out

        private final java.nio.channels.WritableByteChannel out
      • closed

        private boolean closed
    • Constructor Detail

      • SnappyFramedOutputStream

        public SnappyFramedOutputStream​(java.io.OutputStream out,
                                        BufferPool bufferPool)
                                 throws java.io.IOException
        Parameters:
        out - The underlying OutputStream to write to. Must not be null.
        bufferPool - Used to obtain buffer instances. Must not be null.
        Throws:
        java.io.IOException
      • SnappyFramedOutputStream

        public SnappyFramedOutputStream​(java.io.OutputStream out,
                                        int blockSize,
                                        double minCompressionRatio)
                                 throws java.io.IOException
        Creates a new SnappyFramedOutputStream instance.

        Uses DefaultPoolFactory to obtain BufferPool for buffers.

        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. Must be in (0, 65536].
        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
      • SnappyFramedOutputStream

        public SnappyFramedOutputStream​(java.io.OutputStream out,
                                        int blockSize,
                                        double minCompressionRatio,
                                        BufferPool bufferPool)
                                 throws java.io.IOException
        Creates a new SnappyFramedOutputStream instance.
        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. Must be in (0, 65536].
        minCompressionRatio - Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].
        bufferPool - Used to obtain buffer instances. Must not be null.
        Throws:
        java.io.IOException
      • SnappyFramedOutputStream

        public SnappyFramedOutputStream​(java.nio.channels.WritableByteChannel out,
                                        int blockSize,
                                        double minCompressionRatio)
                                 throws java.io.IOException
        Creates a new SnappyFramedOutputStream instance.
        Parameters:
        out - The underlying WritableByteChannel to write to. Must not be null.
        blockSize - The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].
        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
        Since:
        1.1.1
      • SnappyFramedOutputStream

        public SnappyFramedOutputStream​(java.nio.channels.WritableByteChannel out,
                                        int blockSize,
                                        double minCompressionRatio,
                                        BufferPool bufferPool)
                                 throws java.io.IOException
        Creates a new SnappyFramedOutputStream instance.
        Parameters:
        out - The underlying WritableByteChannel to write to. Must not be null.
        blockSize - The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].
        minCompressionRatio - Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].
        bufferPool - Used to obtain buffer instances. Must not be null.
        Throws:
        java.io.IOException
        Since:
        1.1.1
    • Method Detail

      • writeHeader

        private void writeHeader​(java.nio.channels.WritableByteChannel out)
                          throws java.io.IOException
        Writes the implementation specific header or "marker bytes" to out.
        Parameters:
        out - The underlying OutputStream.
        Throws:
        java.io.IOException
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • 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
      • write

        public int write​(java.nio.ByteBuffer src)
                  throws java.io.IOException
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Throws:
        java.io.IOException
      • transferFrom

        public long transferFrom​(java.io.InputStream is)
                          throws java.io.IOException
        Transfers all the content from is to this OutputStream. This potentially limits the amount of buffering required to compress content.
        Parameters:
        is - The source of data to compress.
        Returns:
        The number of bytes read from is.
        Throws:
        java.io.IOException
        Since:
        1.1.1
      • transferFrom

        public long transferFrom​(java.nio.channels.ReadableByteChannel rbc)
                          throws java.io.IOException
        Transfers all the content from rbc to this WritableByteChannel. This potentially limits the amount of buffering required to compress content.
        Parameters:
        rbc - The source of data to compress.
        Returns:
        The number of bytes read from rbc.
        Throws:
        java.io.IOException
        Since:
        1.1.1
      • 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.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • 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
      • writeBlock

        private void writeBlock​(java.nio.channels.WritableByteChannel out,
                                java.nio.ByteBuffer data,
                                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.
        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