|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.deepfs.fsml.BufferedFileChannel
public final class BufferedFileChannel
FileChannel
implementation.This implementation is optimized for sequential reading of a file or a fragment of a file. An instance of this class can either represent a regular file on disk or a fragment of a file (the instance is a "subchannel").
Field Summary | |
---|---|
static int |
DEFAULT_BUFFER_SIZE
The default buffer size. |
Constructor Summary | |
---|---|
BufferedFileChannel(File file)
Standard constructor for creating a BufferedFileChannel from a
complete file. |
|
BufferedFileChannel(File file,
ByteBuffer buffer)
Creates a BufferedFileChannel from a complete file and uses the
given buffer for caching the data. |
|
BufferedFileChannel(File file,
int bufferSize)
Creates a BufferedFileChannel from a complete file with the given
buffer size. |
Method Summary | |
---|---|
long |
absolutePosition()
Returns this channel's absolute position. |
boolean |
buffer(int n)
Buffers n bytes, if the underlying buffer is large enough. |
void |
close()
Closes the underlying FileChannel . |
void |
finish()
Finishes reading from a channel created by subChannel(String, int)
. |
int |
get()
Relative get method. |
byte[] |
get(byte[] dst)
Reads dst.length bytes from the BufferedFileChannel . |
void |
get(byte[] dst,
int start,
int length)
Reads length bytes from the BufferedFileChannel . |
int |
getBufferSize()
Returns the size of the buffer. |
ByteOrder |
getByteOrder()
Returns this FileChannel's byte order. |
String |
getFileName()
Returns the absolute file name of current file. |
int |
getInt()
Relative get method. |
long |
getOffset()
Returns the offset of this (sub)channel regarding to the regular file in the file system. |
int |
getShort()
Relative get method. |
boolean |
isSubChannel()
Checks if the current BufferedFileChannel instance is a
sub-channel. |
long |
position()
Returns this channel's position. |
void |
position(long newPosition)
Sets this channel's position. |
String |
readLine(String inputEncoding)
Reads a line of text. |
void |
reset()
Resets the channel to its initial position. |
void |
setByteOrder(ByteOrder order)
Modifies this channel's byte order. |
long |
size()
Returns the size of the channel. |
void |
skip(long n)
Skips n bytes in the ByteBuffer. |
BufferedFileChannel |
subChannel(String subfilename,
int bytesToRead)
Creates a "subChannel" view for this BufferedFileChannel instance
that shares the underlying FileChannel and buffer. |
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int DEFAULT_BUFFER_SIZE
Constructor Detail |
---|
public BufferedFileChannel(File file) throws IOException
BufferedFileChannel
from a
complete file.
file
- the file to read from
IOException
- if any error occurs while creating the
BufferedFileChannel
public BufferedFileChannel(File file, int bufferSize) throws IOException
BufferedFileChannel
from a complete file with the given
buffer size.
file
- the file to read frombufferSize
- the size of the buffer
IOException
- if any error occurs while creating the
BufferedFileChannel
public BufferedFileChannel(File file, ByteBuffer buffer) throws IOException
BufferedFileChannel
from a complete file and uses the
given buffer for caching the data. This constructor can be used for
creating many BufferedFileChannel
instances (consecutively) with
the same direct byte buffer to avoid the allocation- and
garbage-collection-overhead for multiple direct byte buffers.
file
- the file to read frombuffer
- the ByteBuffer
to use for caching the data
IOException
- if any error occurs while creating the
BufferedFileChannel
Method Detail |
---|
public BufferedFileChannel subChannel(String subfilename, int bytesToRead) throws IOException
Creates a "subChannel" view for this BufferedFileChannel
instance
that shares the underlying FileChannel
and buffer.
The content of the new channel will start at this channel's current
position and it's size depends on the given parameter
bytesToRead
.
The original channel must not be used before finish()
has been
called on the BufferedFileChannel
created by this method. After
finishing the subChannel, the original channel's position is incremented by
bytesToRead
.
subfilename
- the name of the subfilebytesToRead
- the maximum number of bytes to read
IOException
- if any error occurs while creating the channelpublic int getBufferSize()
public void skip(long n) throws IOException
n
bytes in the ByteBuffer.
n
- number of bytes to skip. May be negative
IOException
- if any error occurs while reading the filepublic long position() throws IOException
IOException
- if any error occurs while reading from the channelpublic long absolutePosition() throws IOException
IOException
- if any error occurs while reading from the channelpublic void position(long newPosition) throws IOException
newPosition
- the new position, a non-negative integer counting the
number of bytes from the beginning of the channel
IOException
- if any error occurs while reading from the channelpublic long size() throws IOException
IOException
- if any error occurs while calculating the sizepublic boolean isSubChannel() throws IOException
BufferedFileChannel
instance is a
sub-channel.
IOException
- if any i/o error occurspublic void reset() throws IOException
IOException
- if any error occurs while reading from the channelpublic byte[] get(byte[] dst) throws IOException
dst.length
bytes from the BufferedFileChannel
.
dst
- the arrray to write the data to
IOException
- if there are less than dst.length
bytes
available or any error occurs while reading from the channelpublic void get(byte[] dst, int start, int length) throws IOException
Reads length
bytes from the BufferedFileChannel
.
Assure that that the buffer is large enough and that enough bytes are
buffered (via buffer(int)
). Otherwise, a
BufferUnderflowException
may be thrown.
dst
- the arrray to write the data tostart
- the first position to write the data tolength
- the number of bytes to read
IOException
- if the channel is lockedpublic int get() throws IOException
Relative get method. Reads the byte at this channel's current position, and then increments the position.
Assure that enough at least one byte is buffered via
#buffer(1)
. Otherwise, a
BufferUnderflowException
may be thrown.
IOException
- if the channel is lockedpublic int getShort() throws IOException
Relative get method. Reads two bytes at this channel's current position, and then increments the position by two.
Assure that enough at least two bytes are buffered via
#buffer(2)
. Otherwise, a
BufferUnderflowException
may be thrown.
IOException
- if the channel is lockedpublic int getInt() throws IOException
Relative get method. Reads four bytes at this channel's current position, and then increments the position by four.
Assure that enough at least four bytes are buffered via
#buffer(4)
. Otherwise, a
BufferUnderflowException
may be thrown.
IOException
- if the channel is lockedpublic void finish() throws IOException
Finishes reading from a channel created by subChannel(String, int)
. Any subsequent read from this BufferedFileChannel will fail.
This method must be called for every BufferedFileChannel
that
was created by subChannel(String, int)
instead of calling
close()
.
IOException
- if any error occurs while finishing the channelclose()
public void close() throws IOException
Closes the underlying FileChannel
.
This method must not be called for BufferedFileChannel
instances
that were created by subChannel(String, int)
.
IOException
- if any error occurs while closing the
FileChannel
finish()
public boolean buffer(int n) throws IOException
Buffers n
bytes, if the underlying buffer is large enough.
Does nothing, if the buffer is too small.
n
- the number of bytes to buffer
n
bytes, false if it is too small
IOException
- if any error occurs while reading the filepublic String getFileName()
public long getOffset()
public void setByteOrder(ByteOrder order)
getShort()
and getInt()
methods.
order
- The new byte order, either BIG_ENDIAN
or LITTLE_ENDIAN
public ByteOrder getByteOrder()
ByteOrder
public String readLine(String inputEncoding) throws IOException
inputEncoding
- the input encoding
IOException
- if an I/O error occurspublic String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |