Class LZFInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class LZFInputStream
    extends java.io.InputStream
    Decorator InputStream implementation used for reading compressed data and uncompressing it on the fly, such that reads return uncompressed data. Its direct counterpart is LZFOutputStream; but there is also LZFCompressingInputStream which does reverse of this class.
    See Also:
    LZFFileInputStream, LZFCompressingInputStream
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int _bufferLength
      Length of the current uncompressed bytes buffer
      protected int _bufferPosition
      The current position (next char to output) in the uncompressed bytes buffer.
      protected boolean _cfgFullReads
      Flag that indicates whether we force full reads (reading of as many bytes as requested), or 'optimal' reads (up to as many as available, but at least one).
      protected byte[] _decodedBytes
      the buffer of uncompressed bytes from which content is read
      protected ChunkDecoder _decoder
      Underlying decoder in use.
      protected byte[] _inputBuffer
      the current buffer of compressed bytes (from which to decode)
      protected java.io.InputStream _inputStream
      stream to be decompressed
      protected boolean _inputStreamClosed
      Flag that indicates if we have already called 'inputStream.close()' (to avoid calling it multiple times)
      protected BufferRecycler _recycler
      Object that handles details of buffer recycling
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Method is overridden to report number of bytes that can now be read from decoded data buffer, without reading bytes from the underlying stream.
      void close()  
      void discardBuffered()
      Method that can be called to discard any already buffered input, read from input source.
      java.io.InputStream getUnderlyingInputStream()
      Method that can be used to find underlying InputStream that we read from to get LZF encoded data to decode.
      int read()  
      int read​(byte[] buffer)  
      int read​(byte[] buffer, int offset, int length)  
      int readAndWrite​(java.io.OutputStream out)
      Convenience method that will read and uncompress all data available, and write it using given OutputStream.
      protected boolean readyBuffer()
      Fill the uncompressed bytes buffer by reading the underlying inputStream.
      void setUseFullReads​(boolean b)
      Method that can be used define whether reads should be "full" or "optimal": former means that full compressed blocks are read right away as needed, optimal that only smaller chunks are read at a time, more being read as needed.
      long skip​(long n)
      Overridden to implement efficient skipping by skipping full chunks whenever possible.
      • Methods inherited from class java.io.InputStream

        mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, transferTo
      • Methods inherited from class java.lang.Object

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

      • _decoder

        protected final ChunkDecoder _decoder
        Underlying decoder in use.
      • _recycler

        protected final BufferRecycler _recycler
        Object that handles details of buffer recycling
      • _inputStream

        protected final java.io.InputStream _inputStream
        stream to be decompressed
      • _inputStreamClosed

        protected boolean _inputStreamClosed
        Flag that indicates if we have already called 'inputStream.close()' (to avoid calling it multiple times)
      • _cfgFullReads

        protected boolean _cfgFullReads
        Flag that indicates whether we force full reads (reading of as many bytes as requested), or 'optimal' reads (up to as many as available, but at least one). Default is false, meaning that 'optimal' read is used.
      • _inputBuffer

        protected byte[] _inputBuffer
        the current buffer of compressed bytes (from which to decode)
      • _decodedBytes

        protected byte[] _decodedBytes
        the buffer of uncompressed bytes from which content is read
      • _bufferPosition

        protected int _bufferPosition
        The current position (next char to output) in the uncompressed bytes buffer.
      • _bufferLength

        protected int _bufferLength
        Length of the current uncompressed bytes buffer
    • Constructor Detail

      • LZFInputStream

        public LZFInputStream​(java.io.InputStream inputStream)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • LZFInputStream

        public LZFInputStream​(ChunkDecoder decoder,
                              java.io.InputStream in)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • LZFInputStream

        public LZFInputStream​(java.io.InputStream in,
                              boolean fullReads)
                       throws java.io.IOException
        Parameters:
        in - Underlying input stream to use
        fullReads - Whether read(byte[]) should try to read exactly as many bytes as requested (true); or just however many happen to be available (false)
        Throws:
        java.io.IOException
      • LZFInputStream

        public LZFInputStream​(ChunkDecoder decoder,
                              java.io.InputStream in,
                              boolean fullReads)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • LZFInputStream

        public LZFInputStream​(java.io.InputStream inputStream,
                              BufferRecycler bufferRecycler)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • LZFInputStream

        public LZFInputStream​(java.io.InputStream in,
                              BufferRecycler bufferRecycler,
                              boolean fullReads)
                       throws java.io.IOException
        Parameters:
        in - Underlying input stream to use
        fullReads - Whether read(byte[]) should try to read exactly as many bytes as requested (true); or just however many happen to be available (false)
        bufferRecycler - Buffer recycler instance, for usages where the caller manages the recycler instances
        Throws:
        java.io.IOException
      • LZFInputStream

        public LZFInputStream​(ChunkDecoder decoder,
                              java.io.InputStream in,
                              BufferRecycler bufferRecycler,
                              boolean fullReads)
                       throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • setUseFullReads

        public void setUseFullReads​(boolean b)
        Method that can be used define whether reads should be "full" or "optimal": former means that full compressed blocks are read right away as needed, optimal that only smaller chunks are read at a time, more being read as needed.
      • available

        public int available()
        Method is overridden to report number of bytes that can now be read from decoded data buffer, without reading bytes from the underlying stream. Never throws an exception; returns number of bytes available without further reads from underlying source; -1 if stream has been closed, or 0 if an actual read (and possible blocking) is needed to find out.
        Overrides:
        available in class java.io.InputStream
      • 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[] buffer)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

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

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

        public long skip​(long n)
                  throws java.io.IOException
        Overridden to implement efficient skipping by skipping full chunks whenever possible.
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException
      • getUnderlyingInputStream

        public java.io.InputStream getUnderlyingInputStream()
        Method that can be used to find underlying InputStream that we read from to get LZF encoded data to decode. Will never return null; although underlying stream may be closed (if this stream has been closed).
      • discardBuffered

        public void discardBuffered()
        Method that can be called to discard any already buffered input, read from input source. Specialized method that only makes sense if the underlying InputStream can be repositioned reliably.
      • readAndWrite

        public int readAndWrite​(java.io.OutputStream out)
                         throws java.io.IOException
        Convenience method that will read and uncompress all data available, and write it using given OutputStream. This avoids having to make an intermediate copy of uncompressed data which would be needed when doing the same manually.
        Parameters:
        out - OutputStream to use for writing content
        Returns:
        Number of bytes written (uncompressed)
        Throws:
        java.io.IOException
      • readyBuffer

        protected boolean readyBuffer()
                               throws java.io.IOException
        Fill the uncompressed bytes buffer by reading the underlying inputStream.
        Returns:
        True if there is now at least one byte to read in the buffer; false if there is no more content to read
        Throws:
        java.io.IOException