public class FileChannelInputStream extends ChannelInputStream
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 and Description |
---|
FileChannelInputStream(int bufferSize,
boolean bufferDirect,
java.nio.channels.FileChannel fileChannel,
long position,
long maxLength)
Creates a new instance of FileChannelInputStream.
|
Modifier and Type | Method and Description |
---|---|
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. |
available, bufferedSize, order, order, read, read, read, read, readInput
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
readInput
public FileChannelInputStream(int bufferSize, boolean bufferDirect, java.nio.channels.FileChannel fileChannel, long position, long maxLength) throws java.io.IOException
bufferSize
- the size of the internal buffer used for flushingbufferDirect
- allocate the internal buffer as direct
fileChannel
- the file channel from which to read dataposition
- the starting position of the filemaxLength
- the maximal length of datajava.io.IOException
- if there was an error using readChannelpublic long skip(long n) throws java.io.IOException
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.skip
in interface BinaryInput
skip
in class BufferInputStream
n
- the number of bytes to be skippedjava.io.IOException
- if there was an error using readChannelpublic void reset() throws java.io.IOException
reset
in class java.io.InputStream
java.io.IOException
public long getPosition()
getPosition
in class BufferInputStream
public void setPosition(long position) throws java.io.IOException
setPosition
in class BufferInputStream
position
- the new positionjava.io.IOException
- if the specified position is outside the boundariesprotected void read(java.nio.ByteBuffer buffer) throws java.io.EOFException, java.io.IOException
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.
read
in class ChannelInputStream
buffer
- the buffer into which to read additional datajava.io.EOFException
- if there are no more data availablejava.io.IOException
- if there was an error reading data