messif.objects.nio
Class FileChannelInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by messif.objects.nio.BufferInputStream
          extended by messif.objects.nio.ChannelInputStream
              extended by messif.objects.nio.FileChannelInputStream
All Implemented Interfaces:
java.io.Closeable, BinaryInput

public class FileChannelInputStream
extends ChannelInputStream

Extending class for a ChannelInputStream that operates on a file. The position is restored before every read 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.


Constructor Summary
FileChannelInputStream(int bufferSize, boolean bufferDirect, java.nio.channels.FileChannel fileChannel, long position, long maxLength)
          Creates a new instance of FileChannelInputStream.
 
Method Summary
 long getPosition()
          Returns the current position in the file.
protected  void read(java.nio.ByteBuffer buffer)
          Reads some data into the buffer.
 void reset()
          Repositions this stream to the starting position.
 void setPosition(long position)
          Set the position from which the data will be read.
 long skip(long n)
          Skips over and discards n bytes of data from this input stream.
 
Methods inherited from class messif.objects.nio.BufferInputStream
available, bufferedSize, read, read, read, read, readInput
 
Methods inherited from class java.io.InputStream
close, mark, markSupported, read
 
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.BinaryInput
readInput
 

Constructor Detail

FileChannelInputStream

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

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

skip

public long skip(long n)
          throws java.io.IOException
Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

Specified by:
skip in interface BinaryInput
Overrides:
skip in class BufferInputStream
Parameters:
n - the number of bytes to be skipped
Returns:
the actual number of bytes skipped
Throws:
java.io.IOException - if there was an error using readChannel

reset

public void reset()
           throws java.io.IOException
Repositions this stream to the starting position.

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException

getPosition

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

Overrides:
getPosition in class BufferInputStream
Returns:
the current position in the file

setPosition

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

Overrides:
setPosition in class BufferInputStream
Parameters:
position - the new position
Throws:
java.io.IOException - if the specified position is outside the boundaries

read

protected void read(java.nio.ByteBuffer buffer)
             throws java.io.EOFException,
                    java.io.IOException
Reads some data into the buffer. This method blocks until at least one byte is read or, if there are no more data, EOFException is thrown.

Data are accessed correctly regardless of the actual position in the fileChannel.

Overrides:
read in class ChannelInputStream
Parameters:
buffer - the buffer into which to read additional data
Throws:
java.io.EOFException - if there are no more data available
java.io.IOException - if there was an error reading data