Top Description Inners Fields Constructors Methods
com.ning.compress.lzf.util

public Class LZFFileOutputStream

extends FileOutputStream
implements WritableByteChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.WritableByteChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.File, .FileDescriptor, .FileNotFoundException, .FileOutputStream, .IOException, .InputStream, .OutputStream, java.nio.ByteBuffer, .MappedByteBuffer, java.nio.channels.FileChannel, .FileChannel.MapMode, .WritableByteChannel, com.ning.compress.BufferRecycler, com.ning.compress.lzf.ChunkEncoder, .LZFChunk, .LZFOutputStream

Helper class that allows use of LZF compression even if a library requires use of FileOutputStream.

Note that use of this class is not recommended unless you absolutely must use a FileOutputStream instance; otherwise basic LZFOutputStream (which uses aggregation for underlying streams) is more appropriate

Implementation 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 and Inner Type Summary

Modifier and TypeClass and Description
private class
LZFFileOutputStream.Wrapper

This simple wrapper is needed to re-route read calls so that they will use "raw" writes

Field Summary

Modifier and TypeField and Description
protected boolean
_cfgFinishBlockOnFlush

Configuration setting that governs whether basic 'flush()' should first complete a block or not.

private final ChunkEncoder
protected byte[]
protected boolean
_outputStreamClosed

Flag that indicates if we have already called '_outputStream.close()' (to avoid calling it multiple times)

protected int
private final BufferRecycler
private final LZFFileOutputStream.Wrapper
_wrapper

Wrapper object we use to allow decoder to write directly to the stream, without ending in infinite loop...

private static final int

Constructor Summary

AccessConstructor and Description
public
public
LZFFileOutputStream(File file, boolean append)

public
public
public
LZFFileOutputStream(String name, boolean append)

public
public
LZFFileOutputStream(ChunkEncoder encoder, File file, boolean append)

public
public
public
LZFFileOutputStream(ChunkEncoder encoder, String name, boolean append)

public
LZFFileOutputStream(ChunkEncoder encoder, File file, BufferRecycler bufferRecycler)

public
LZFFileOutputStream(ChunkEncoder encoder, File file, boolean append, BufferRecycler bufferRecycler)

public
public
LZFFileOutputStream(ChunkEncoder encoder, String name, BufferRecycler bufferRecycler)

public
LZFFileOutputStream(ChunkEncoder encoder, String name, boolean append, BufferRecycler bufferRecycler)

Method Summary

Modifier and TypeMethod and Description
protected void
public void
public 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.

public void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.
public boolean
getFinishBlockOnFlush()

Accessor for checking whether call to "flush()" will first finish the current block or not

public boolean
isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.
protected void
rawWrite(byte[] buffer, int offset, int length)

public LZFFileOutputStream
setFinishBlockOnFlush(boolean b)

Method for defining whether call to flush will also complete current block (similar to calling finishBlock()) or not.

public void
write(byte[]
the data.
b
)

Overrides java.io.FileOutputStream.write.

Writes b.length bytes from the specified byte array to this file output stream.
public void
write(byte[]
the data.
buffer
,
int
the start offset in the data.
offset
,
int
the number of bytes to write.
length
)

Overrides java.io.FileOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this file output stream.
public void
write(int
the byte to be written.
b
)

Overrides java.io.FileOutputStream.write.

Writes the specified byte to this file output stream.
public void
write(final InputStream in)

public synchronized int
write(final ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Implements java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.
public void
write(final FileChannel in)

protected void
writeCompressedBlock()

Compress and write the current block to the OutputStream

Inherited from java.io.FileOutputStream:
getChannelgetFD

Field Detail

_cfgFinishBlockOnFlushback to summary
protected boolean _cfgFinishBlockOnFlush

Configuration setting that governs whether basic 'flush()' should first complete a block or not.

Default value is 'true'.

_encoderback to summary
private final ChunkEncoder _encoder
_outputBufferback to summary
protected byte[] _outputBuffer
_outputStreamClosedback to summary
protected boolean _outputStreamClosed

Flag that indicates if we have already called '_outputStream.close()' (to avoid calling it multiple times)

_positionback to summary
protected int _position
_recyclerback to summary
private final BufferRecycler _recycler
_wrapperback to summary
private final LZFFileOutputStream.Wrapper _wrapper

Wrapper object we use to allow decoder to write directly to the stream, without ending in infinite loop...

OUTPUT_BUFFER_SIZEback to summary
private static final int OUTPUT_BUFFER_SIZE

Constructor Detail

LZFFileOutputStreamback to summary
public LZFFileOutputStream(File file) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(File file, boolean append) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(FileDescriptor fdObj)
LZFFileOutputStreamback to summary
public LZFFileOutputStream(String name) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(String name, boolean append) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, File file) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, File file, boolean append) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, FileDescriptor fdObj)
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, String name) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, String name, boolean append) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, File file, BufferRecycler bufferRecycler) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, File file, boolean append, BufferRecycler bufferRecycler) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, FileDescriptor fdObj, BufferRecycler bufferRecycler)
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, String name, BufferRecycler bufferRecycler) throws FileNotFoundException
LZFFileOutputStreamback to summary
public LZFFileOutputStream(ChunkEncoder encoder, String name, boolean append, BufferRecycler bufferRecycler) throws FileNotFoundException

Method Detail

checkNotClosedback to summary
protected void checkNotClosed() throws IOException
closeback to summary
public void close() throws IOException

Overrides java.io.FileOutputStream.close.

Implements java.nio.channels.Channel.close, java.io.Closeable.close.

Doc from java.nio.channels.Channel.close.

Closes this channel.

After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

If this channel is already closed then invoking this method has no effect.

This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
finishBlockback to summary
public LZFFileOutputStream finishBlock() throws 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.

flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
getFinishBlockOnFlushback to summary
public boolean getFinishBlockOnFlush()

Accessor for checking whether call to "flush()" will first finish the current block or not

isOpenback to summary
public boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Doc from java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Returns:boolean

true if, and only if, this channel is open

Annotations
@Override
rawWriteback to summary
protected void rawWrite(byte[] buffer, int offset, int length) throws IOException
setFinishBlockOnFlushback to summary
public LZFFileOutputStream setFinishBlockOnFlush(boolean b)

Method for defining whether call to flush will also complete current block (similar to calling finishBlock()) or not.

writeback to summary
public void write(byte[] b) throws IOException

Overrides java.io.FileOutputStream.write.

Doc from java.io.FileOutputStream.write.

Writes b.length bytes from the specified byte array to this file output stream.

Parameters
b:byte[]

the data.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] buffer, int offset, int length) throws IOException

Overrides java.io.FileOutputStream.write.

Doc from java.io.FileOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this file output stream.

Parameters
buffer:byte[]

the data.

offset:int

the start offset in the data.

length:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(int b) throws IOException

Overrides java.io.FileOutputStream.write.

Doc from java.io.FileOutputStream.write.

Writes the specified byte to this file output stream. Implements the write method of OutputStream.

Parameters
b:int

the byte to be written.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(final InputStream in) throws IOException
writeback to summary
public synchronized int write(final ByteBuffer src) throws IOException

Implements java.nio.channels.WritableByteChannel.write.

Doc from java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, src.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is written, where 0 <= n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer's position at the moment this method is invoked; the index of the last byte written will be p + n - 1. Upon return the buffer's position will be equal to p + n; its limit will not have changed.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
src:ByteBuffer

The buffer from which bytes are to be retrieved

Returns:int

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
writeback to summary
public void write(final FileChannel in) throws IOException
writeCompressedBlockback to summary
protected void writeCompressedBlock() throws IOException

Compress and write the current block to the OutputStream

com.ning.compress.lzf.util back to summary

private final Class LZFFileOutputStream.Wrapper

extends OutputStream
Class Inheritance

This simple wrapper is needed to re-route read calls so that they will use "raw" writes

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
write(int
the byte.
arg0
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.
public void
write(byte[]
the data.
buffer
,
int
the start offset in the data.
offset
,
int
the number of bytes to write.
length
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.
Inherited from java.io.OutputStream:
closeflushnullOutputStreamwrite

Constructor Detail

Wrapperback to summary
private Wrapper()

Method Detail

writeback to summary
public void write(int arg0) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
arg0:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(byte[] buffer, int offset, int length) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
buffer:byte[]

the data.

offset:int

the start offset in the data.

length:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.