messif.objects.nio
Class FileChannelOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by messif.objects.nio.BufferOutputStream
          extended by messif.objects.nio.ChannelOutputStream
              extended by messif.objects.nio.FileChannelOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, BinaryOutput

public class FileChannelOutputStream
extends ChannelOutputStream

Extending class for ChannelOutputStream operating over a file. The position is restored before every write operation, so it is safe to use multiple instances of this class over the same file channel. However, if multiple threads use the same instance of this class, the access to the instance must be synchronized.


Field Summary
protected  java.nio.channels.FileChannel fileChannel
          The file to which to write data
 
Fields inherited from class messif.objects.nio.BufferOutputStream
MINIMAL_BUFFER_SIZE
 
Constructor Summary
FileChannelOutputStream(int bufferSize, boolean bufferDirect, java.nio.channels.FileChannel fileChannel, long position, long maxLength)
          Creates a new instance of FileChannelOutputStream.
 
Method Summary
 long getPosition()
          Returns the current position in the file.
 void setPosition(long position)
          Set the position at which the data will be written.
protected  void write(java.nio.ByteBuffer buffer)
          Writes the buffered data to the file channel.
 
Methods inherited from class messif.objects.nio.BufferOutputStream
bufferedSize, close, flush, isDirty, prepareOutput, write, write, write, write, write
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface messif.objects.nio.BinaryOutput
flush, prepareOutput
 

Field Detail

fileChannel

protected final java.nio.channels.FileChannel fileChannel
The file to which to write data

Constructor Detail

FileChannelOutputStream

public FileChannelOutputStream(int bufferSize,
                               boolean bufferDirect,
                               java.nio.channels.FileChannel fileChannel,
                               long position,
                               long maxLength)
                        throws java.io.IOException
Creates a new instance of FileChannelOutputStream.

Parameters:
bufferSize - the size of the internal buffer used for flushing
bufferDirect - allocate the internal buffer as direct
fileChannel - the file channel into which to write data
position - the starting position of the file
maxLength - the maximal length of data
Throws:
java.io.IOException - if there was an error using the file channel
Method Detail

getPosition

public long getPosition()
Returns the current position in the file.

Returns:
the current position in the file

setPosition

public void setPosition(long position)
                 throws java.io.IOException
Set the position at which the data will be written.

Parameters:
position - the new position
Throws:
java.io.IOException - if the specified position is outside the boundaries

write

protected void write(java.nio.ByteBuffer buffer)
              throws java.io.IOException
Writes the buffered data to the file channel. The writing is done at the correct position regardless of the underlying file's actual position.

Overrides:
write in class ChannelOutputStream
Parameters:
buffer - the buffer from which to write data
Throws:
java.io.IOException - if there was an error writing the data