Package com.ning.compress.lzf.util
Class LZFFileInputStream
java.lang.Object
java.io.InputStream
java.io.FileInputStream
com.ning.compress.lzf.util.LZFFileInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
Helper class that allows use of LZF compression even if a library requires
use of
FileInputStream
.
Note that use of this class is not recommended unless you absolutely must
use a FileInputStream
instance; otherwise basic LZFInputStream
(which uses aggregation for underlying streams) is more appropriate
Implementation note: much of the code is just copied from LZFInputStream
,
so care must be taken to keep implementations in sync if there are fixes.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
This simple wrapper is needed to re-route read calls so that they will use "raw" reads -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
Length of the current uncompressed bytes bufferprotected int
The current position (next char to output) in the uncompressed bytes buffer.protected boolean
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[]
the buffer of uncompressed bytes from which content is readprotected final ChunkDecoder
Underlying decoder in use.protected byte[]
the current buffer of compressed bytes (from which to decode)protected boolean
Flag that indicates if we have already called 'inputStream.close()' (to avoid calling it multiple times)protected final BufferRecycler
Object that handles details of buffer recyclingprotected final LZFFileInputStream.Wrapper
Wrapper object we use to allow decoder to read directly from the stream, without ending in infinite loop... -
Constructor Summary
ConstructorsConstructorDescriptionLZFFileInputStream
(File file) LZFFileInputStream
(FileDescriptor fdObj) LZFFileInputStream
(FileDescriptor fdObj, ChunkDecoder decompressor) LZFFileInputStream
(FileDescriptor fdObj, ChunkDecoder decompressor, BufferRecycler bufferRecycler) LZFFileInputStream
(File file, ChunkDecoder decompressor) LZFFileInputStream
(File file, ChunkDecoder decompressor, BufferRecycler bufferRecycler) LZFFileInputStream
(String name) LZFFileInputStream
(String name, ChunkDecoder decompressor) LZFFileInputStream
(String name, ChunkDecoder decompressor, BufferRecycler bufferRecycler) -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
int
read()
int
read
(byte[] b) int
read
(byte[] buffer, int offset, int length) int
readAndWrite
(OutputStream out) Convenience method that will read and uncompress all data available, and write it using givenOutputStream
.protected final int
readRaw
(byte[] buffer, int offset, int length) protected boolean
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.protected final long
skipRaw
(long amount) Methods inherited from class java.io.FileInputStream
getChannel, getFD, readAllBytes, readNBytes, transferTo
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, readNBytes, reset, skipNBytes
-
Field Details
-
_decompressor
Underlying decoder in use. -
_recycler
Object that handles details of buffer recycling -
_inputStreamClosed
protected boolean _inputStreamClosedFlag that indicates if we have already called 'inputStream.close()' (to avoid calling it multiple times) -
_cfgFullReads
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). Default is false, meaning that 'optimal' read is used. -
_inputBuffer
protected byte[] _inputBufferthe current buffer of compressed bytes (from which to decode) -
_decodedBytes
protected byte[] _decodedBytesthe buffer of uncompressed bytes from which content is read -
_bufferPosition
protected int _bufferPositionThe current position (next char to output) in the uncompressed bytes buffer. -
_bufferLength
protected int _bufferLengthLength of the current uncompressed bytes buffer -
_wrapper
Wrapper object we use to allow decoder to read directly from the stream, without ending in infinite loop...
-
-
Constructor Details
-
LZFFileInputStream
- Throws:
FileNotFoundException
-
LZFFileInputStream
-
LZFFileInputStream
- Throws:
FileNotFoundException
-
LZFFileInputStream
- Throws:
FileNotFoundException
-
LZFFileInputStream
-
LZFFileInputStream
- Throws:
FileNotFoundException
-
LZFFileInputStream
public LZFFileInputStream(File file, ChunkDecoder decompressor, BufferRecycler bufferRecycler) throws FileNotFoundException - Throws:
FileNotFoundException
-
LZFFileInputStream
public LZFFileInputStream(FileDescriptor fdObj, ChunkDecoder decompressor, BufferRecycler bufferRecycler) -
LZFFileInputStream
public LZFFileInputStream(String name, ChunkDecoder decompressor, BufferRecycler bufferRecycler) throws FileNotFoundException - Throws:
FileNotFoundException
-
-
Method Details
-
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()- Overrides:
available
in classFileInputStream
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFileInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classFileInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classFileInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classFileInputStream
- Throws:
IOException
-
skip
Overridden to implement efficient skipping by skipping full chunks whenever possible.- Overrides:
skip
in classFileInputStream
- Throws:
IOException
-
readAndWrite
Convenience 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:
IOException
-
readyBuffer
Fill the uncompressed bytes buffer by reading the underlying inputStream.- Throws:
IOException
-
readRaw
- Throws:
IOException
-
skipRaw
- Throws:
IOException
-