Package com.ning.compress.lzf
Class LZFInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.ning.compress.lzf.LZFInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class LZFInputStream extends java.io.InputStreamDecoratorInputStreamimplementation used for reading compressed data and uncompressing it on the fly, such that reads return uncompressed data. Its direct counterpart isLZFOutputStream; but there is alsoLZFCompressingInputStreamwhich does reverse of this class.- See Also:
LZFFileInputStream,LZFCompressingInputStream
-
-
Field Summary
Fields Modifier and Type Field Description protected int_bufferLengthLength of the current uncompressed bytes bufferprotected int_bufferPositionThe current position (next char to output) in the uncompressed bytes buffer.protected boolean_cfgFullReadsFlag 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[]_decodedBytesthe buffer of uncompressed bytes from which content is readprotected ChunkDecoder_decoderUnderlying decoder in use.protected byte[]_inputBufferthe current buffer of compressed bytes (from which to decode)protected java.io.InputStream_inputStreamstream to be decompressedprotected boolean_inputStreamClosedFlag that indicates if we have already called 'inputStream.close()' (to avoid calling it multiple times)protected BufferRecycler_recyclerObject that handles details of buffer recycling
-
Constructor Summary
Constructors Constructor Description LZFInputStream(ChunkDecoder decoder, java.io.InputStream in)LZFInputStream(ChunkDecoder decoder, java.io.InputStream in, boolean fullReads)LZFInputStream(ChunkDecoder decoder, java.io.InputStream in, BufferRecycler bufferRecycler, boolean fullReads)LZFInputStream(java.io.InputStream inputStream)LZFInputStream(java.io.InputStream in, boolean fullReads)LZFInputStream(java.io.InputStream inputStream, BufferRecycler bufferRecycler)LZFInputStream(java.io.InputStream in, BufferRecycler bufferRecycler, boolean fullReads)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Method is overridden to report number of bytes that can now be read from decoded data buffer, without reading bytes from the underlying stream.voidclose()voiddiscardBuffered()Method that can be called to discard any already buffered input, read from input source.java.io.InputStreamgetUnderlyingInputStream()Method that can be used to find underlyingInputStreamthat we read from to get LZF encoded data to decode.intread()intread(byte[] buffer)intread(byte[] buffer, int offset, int length)intreadAndWrite(java.io.OutputStream out)Convenience method that will read and uncompress all data available, and write it using givenOutputStream.protected booleanreadyBuffer()Fill the uncompressed bytes buffer by reading the underlying inputStream.voidsetUseFullReads(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.longskip(long n)Overridden to implement efficient skipping by skipping full chunks whenever possible.
-
-
-
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 usefullReads- Whetherread(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 usefullReads- Whetherread(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:
availablein classjava.io.InputStream
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] buffer) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] buffer, int offset, int length) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOExceptionOverridden to implement efficient skipping by skipping full chunks whenever possible.- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException
-
getUnderlyingInputStream
public java.io.InputStream getUnderlyingInputStream()
Method that can be used to find underlyingInputStreamthat 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 underlyingInputStreamcan be repositioned reliably.
-
readAndWrite
public int readAndWrite(java.io.OutputStream out) throws java.io.IOExceptionConvenience method that will read and uncompress all data available, and write it using givenOutputStream. 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.IOExceptionFill 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
-
-