messif.objects.nio
Class MappedFileChannelInputStream

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

public class MappedFileChannelInputStream
extends BufferInputStream

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
MappedFileChannelInputStream(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.
 java.nio.ByteBuffer readInput(int minBytes)
          Returns a buffer that contains at least minBytes bytes.
 void reset()
          Repositions this stream to the starting position.
 void setPosition(long position)
          Set the position from which the data will be read.
 
Methods inherited from class messif.objects.nio.BufferInputStream
available, bufferedSize, read, read, read, read, read, skip
 
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
 

Constructor Detail

MappedFileChannelInputStream

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

Parameters:
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

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

readInput

public java.nio.ByteBuffer readInput(int minBytes)
                              throws java.io.IOException
Description copied from interface: BinaryInput
Returns a buffer that contains at least minBytes bytes. If the buffer with the required number of bytes cannot be provided, an IOException is thrown. Note that the returned buffer can provide more than minBytes.

Specified by:
readInput in interface BinaryInput
Overrides:
readInput in class BufferInputStream
Parameters:
minBytes - the minimal number of bytes that must be available for reading from the buffer
Returns:
the buffer prepared for reading
Throws:
java.io.IOException - if there was an error while preparing a buffer with minBytes bytes