messif.objects.nio
Class FileChannelOutputStream
java.lang.Object
java.io.OutputStream
messif.objects.nio.BufferOutputStream
messif.objects.nio.ChannelOutputStream
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 |
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 java.io.OutputStream |
write |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
fileChannel
protected final java.nio.channels.FileChannel fileChannel
- The file to which to write data
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 flushingbufferDirect
- allocate the internal buffer as direct
fileChannel
- the file channel into which to write dataposition
- the starting position of the filemaxLength
- the maximal length of data
- Throws:
java.io.IOException
- if there was an error using the file channel
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