Class SnappyFramedInputStream

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

    public final class SnappyFramedInputStream
    extends java.io.InputStream
    implements java.nio.channels.ReadableByteChannel
    Implements the x-snappy-framed as an InputStream and ReadableByteChannel.
    Since:
    1.1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buffer
      Buffer contains a copy of the uncompressed data for the block.
      private boolean closed
      Indicates if this instance has been closed.
      private boolean eof
      Indicates if we have reached the EOF on input.
      private java.nio.ByteBuffer frameHeader  
      private java.nio.ByteBuffer input
      A single frame read from the underlying InputStream.
      private int position
      The next position to read from buffer.
      private java.nio.channels.ReadableByteChannel rbc  
      private java.nio.ByteBuffer uncompressedDirect
      The decompressed data from input.
      private int valid
      The position in input buffer to read to.
      private boolean verifyChecksums  
    • Constructor Summary

      Constructors 
      Constructor Description
      SnappyFramedInputStream​(java.io.InputStream in)
      Creates a Snappy input stream to read data from the specified underlying input stream.
      SnappyFramedInputStream​(java.io.InputStream in, boolean verifyChecksums)
      Creates a Snappy input stream to read data from the specified underlying input stream.
      SnappyFramedInputStream​(java.nio.channels.ReadableByteChannel in)
      Creates a Snappy input stream to read data from the specified underlying channel.
      SnappyFramedInputStream​(java.nio.channels.ReadableByteChannel in, boolean verifyChecksums)
      Creates a Snappy input stream to read data from the specified underlying channel.
    • Field Detail

      • rbc

        private final java.nio.channels.ReadableByteChannel rbc
      • frameHeader

        private final java.nio.ByteBuffer frameHeader
      • verifyChecksums

        private final boolean verifyChecksums
      • input

        private java.nio.ByteBuffer input
        A single frame read from the underlying InputStream.
      • uncompressedDirect

        private java.nio.ByteBuffer uncompressedDirect
        The decompressed data from input.
      • closed

        private boolean closed
        Indicates if this instance has been closed.
      • eof

        private boolean eof
        Indicates if we have reached the EOF on input.
      • valid

        private int valid
        The position in input buffer to read to.
      • position

        private int position
        The next position to read from buffer.
      • buffer

        private byte[] buffer
        Buffer contains a copy of the uncompressed data for the block.
    • Constructor Detail

      • SnappyFramedInputStream

        public SnappyFramedInputStream​(java.io.InputStream in)
                                throws java.io.IOException
        Creates a Snappy input stream to read data from the specified underlying input stream.
        Parameters:
        in - the underlying input stream. Must not be null.
        Throws:
        java.io.IOException
      • SnappyFramedInputStream

        public SnappyFramedInputStream​(java.io.InputStream in,
                                       boolean verifyChecksums)
                                throws java.io.IOException
        Creates a Snappy input stream to read data from the specified underlying input stream.
        Parameters:
        in - the underlying input stream. Must not be null.
        verifyChecksums - if true, checksums in input stream will be verified
        Throws:
        java.io.IOException
      • SnappyFramedInputStream

        public SnappyFramedInputStream​(java.nio.channels.ReadableByteChannel in)
                                throws java.io.IOException
        Creates a Snappy input stream to read data from the specified underlying channel.
        Parameters:
        in - the underlying readable channel. Must not be null.
        Throws:
        java.io.IOException
      • SnappyFramedInputStream

        public SnappyFramedInputStream​(java.nio.channels.ReadableByteChannel in,
                                       boolean verifyChecksums)
                                throws java.io.IOException
        Creates a Snappy input stream to read data from the specified underlying channel.
        Parameters:
        in - the underlying readable channel. Must not be null.
        verifyChecksums - if true, checksums in input stream will be verified
        Throws:
        java.io.IOException
    • Method Detail

      • allocateBuffersBasedOnSize

        private void allocateBuffersBasedOnSize​(int size)
        Parameters:
        size -
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] output,
                        int offset,
                        int length)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • read

        public int read​(java.nio.ByteBuffer dst)
                 throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Throws:
        java.io.IOException
      • transferTo

        public long transferTo​(java.io.OutputStream os)
                        throws java.io.IOException
        Transfers the entire content of this InputStream to os. This potentially limits the amount of buffering required to decompress content.

        Unlike read(byte[], int, int), this method does not need to be called multiple times. A single call will transfer all available content. Any calls after the source has been exhausted will result in a return value of 0.

        Overrides:
        transferTo in class java.io.InputStream
        Parameters:
        os - The destination to write decompressed content to.
        Returns:
        The number of bytes transferred.
        Throws:
        java.io.IOException
        Since:
        1.1.1
      • transferTo

        public long transferTo​(java.nio.channels.WritableByteChannel wbc)
                        throws java.io.IOException
        Transfers the entire content of this ReadableByteChannel to wbc. This potentially limits the amount of buffering required to decompress content.

        Unlike read(ByteBuffer), this method does not need to be called multiple times. A single call will transfer all available content. Any calls after the source has been exhausted will result in a return value of 0.

        Parameters:
        wbc - The destination to write decompressed content to.
        Returns:
        The number of bytes transferred.
        Throws:
        java.io.IOException
        Since:
        1.1.1
      • close

        public 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.InputStream
        Throws:
        java.io.IOException
      • ensureBuffer

        private boolean ensureBuffer()
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • readBlockHeader

        private boolean readBlockHeader()
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • getFrameMetaData

        private SnappyFramedInputStream.FrameMetaData getFrameMetaData​(java.nio.ByteBuffer frameHeader)
                                                                throws java.io.IOException
        Parameters:
        frameHeader -
        Returns:
        Throws:
        java.io.IOException
      • getFrameData

        private SnappyFramedInputStream.FrameData getFrameData​(java.nio.ByteBuffer content)
                                                        throws java.io.IOException
        Parameters:
        content -
        Returns:
        Throws:
        java.io.IOException
      • getCrc32c

        private int getCrc32c​(java.nio.ByteBuffer content)