Package com.ning.compress.lzf.util
Class LZFFileOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FileOutputStream
-
- com.ning.compress.lzf.util.LZFFileOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
,java.nio.channels.Channel
,java.nio.channels.WritableByteChannel
public class LZFFileOutputStream extends java.io.FileOutputStream implements java.nio.channels.WritableByteChannel
Helper class that allows use of LZF compression even if a library requires use ofFileOutputStream
.Note that use of this class is not recommended unless you absolutely must use a
FileOutputStream
instance; otherwise basicLZFOutputStream
(which uses aggregation for underlying streams) is more appropriateImplementation note: much of the code is just copied from
LZFOutputStream
, so care must be taken to keep implementations in sync if there are fixes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
LZFFileOutputStream.Wrapper
This simple wrapper is needed to re-route read calls so that they will use "raw" writes
-
Field Summary
Fields Modifier and Type Field Description protected boolean
_cfgFinishBlockOnFlush
Configuration setting that governs whether basic 'flush()' should first complete a block or not.private ChunkEncoder
_encoder
protected byte[]
_outputBuffer
protected boolean
_outputStreamClosed
Flag that indicates if we have already called '_outputStream.close()' (to avoid calling it multiple times)protected int
_position
private BufferRecycler
_recycler
private LZFFileOutputStream.Wrapper
_wrapper
Wrapper object we use to allow decoder to write directly to the stream, without ending in infinite loop...private static int
OUTPUT_BUFFER_SIZE
-
Constructor Summary
Constructors Constructor Description LZFFileOutputStream(ChunkEncoder encoder, java.io.File file)
LZFFileOutputStream(ChunkEncoder encoder, java.io.FileDescriptor fdObj)
LZFFileOutputStream(ChunkEncoder encoder, java.io.FileDescriptor fdObj, BufferRecycler bufferRecycler)
LZFFileOutputStream(ChunkEncoder encoder, java.io.File file, boolean append)
LZFFileOutputStream(ChunkEncoder encoder, java.io.File file, boolean append, BufferRecycler bufferRecycler)
LZFFileOutputStream(ChunkEncoder encoder, java.io.File file, BufferRecycler bufferRecycler)
LZFFileOutputStream(ChunkEncoder encoder, java.lang.String name)
LZFFileOutputStream(ChunkEncoder encoder, java.lang.String name, boolean append)
LZFFileOutputStream(ChunkEncoder encoder, java.lang.String name, boolean append, BufferRecycler bufferRecycler)
LZFFileOutputStream(ChunkEncoder encoder, java.lang.String name, BufferRecycler bufferRecycler)
LZFFileOutputStream(java.io.File file)
LZFFileOutputStream(java.io.FileDescriptor fdObj)
LZFFileOutputStream(java.io.File file, boolean append)
LZFFileOutputStream(java.lang.String name)
LZFFileOutputStream(java.lang.String name, boolean append)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkNotClosed()
void
close()
LZFFileOutputStream
finishBlock()
Method that can be used to force completion of the current block, which means that all buffered data will be compressed into an LZF block.void
flush()
boolean
getFinishBlockOnFlush()
Accessor for checking whether call to "flush()" will first finish the current block or notboolean
isOpen()
protected void
rawWrite(byte[] buffer, int offset, int length)
LZFFileOutputStream
setFinishBlockOnFlush(boolean b)
Method for defining whether call toflush()
will also complete current block (similar to callingfinishBlock()
) or not.void
write(byte[] b)
void
write(byte[] buffer, int offset, int length)
void
write(int b)
void
write(java.io.InputStream in)
int
write(java.nio.ByteBuffer src)
void
write(java.nio.channels.FileChannel in)
protected void
writeCompressedBlock()
Compress and write the current block to the OutputStream
-
-
-
Field Detail
-
OUTPUT_BUFFER_SIZE
private static final int OUTPUT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
_encoder
private final ChunkEncoder _encoder
-
_recycler
private final BufferRecycler _recycler
-
_outputBuffer
protected byte[] _outputBuffer
-
_position
protected int _position
-
_cfgFinishBlockOnFlush
protected boolean _cfgFinishBlockOnFlush
Configuration setting that governs whether basic 'flush()' should first complete a block or not.Default value is 'true'.
-
_outputStreamClosed
protected boolean _outputStreamClosed
Flag that indicates if we have already called '_outputStream.close()' (to avoid calling it multiple times)
-
_wrapper
private final LZFFileOutputStream.Wrapper _wrapper
Wrapper object we use to allow decoder to write directly to the stream, without ending in infinite loop...
-
-
Constructor Detail
-
LZFFileOutputStream
public LZFFileOutputStream(java.io.File file) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(java.io.File file, boolean append) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(java.io.FileDescriptor fdObj)
-
LZFFileOutputStream
public LZFFileOutputStream(java.lang.String name) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(java.lang.String name, boolean append) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.io.File file) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.io.File file, boolean append) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.io.FileDescriptor fdObj)
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.lang.String name) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.lang.String name, boolean append) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.io.File file, BufferRecycler bufferRecycler) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.io.File file, boolean append, BufferRecycler bufferRecycler) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.io.FileDescriptor fdObj, BufferRecycler bufferRecycler)
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.lang.String name, BufferRecycler bufferRecycler) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LZFFileOutputStream
public LZFFileOutputStream(ChunkEncoder encoder, java.lang.String name, boolean append, BufferRecycler bufferRecycler) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
-
Method Detail
-
setFinishBlockOnFlush
public LZFFileOutputStream setFinishBlockOnFlush(boolean b)
Method for defining whether call toflush()
will also complete current block (similar to callingfinishBlock()
) or not.
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.FileOutputStream
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException
- Overrides:
write
in classjava.io.FileOutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] buffer, int offset, int length) throws java.io.IOException
- Overrides:
write
in classjava.io.FileOutputStream
- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException
- Overrides:
write
in classjava.io.FileOutputStream
- Throws:
java.io.IOException
-
write
public void write(java.io.InputStream in) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Throws:
java.io.IOException
-
write
public void write(java.nio.channels.FileChannel in) throws java.io.IOException
- Throws:
java.io.IOException
-
getFinishBlockOnFlush
public boolean getFinishBlockOnFlush()
Accessor for checking whether call to "flush()" will first finish the current block or not
-
finishBlock
public LZFFileOutputStream finishBlock() throws java.io.IOException
Method that can be used to force completion of the current block, which means that all buffered data will be compressed into an LZF block. This typically results in lower compression ratio as larger blocks compress better; but may be necessary for network connections to ensure timely sending of data.- Throws:
java.io.IOException
-
writeCompressedBlock
protected void writeCompressedBlock() throws java.io.IOException
Compress and write the current block to the OutputStream- Throws:
java.io.IOException
-
rawWrite
protected void rawWrite(byte[] buffer, int offset, int length) throws java.io.IOException
- Throws:
java.io.IOException
-
checkNotClosed
protected void checkNotClosed() throws java.io.IOException
- Throws:
java.io.IOException
-
-