org.basex.build.fs.parser
Class BufferedFileChannel

java.lang.Object
  extended by org.basex.build.fs.parser.BufferedFileChannel

public final class BufferedFileChannel
extends java.lang.Object

Buffered FileChannel implementation.

This implementation is optimized for sequential reading of a file or a fragment of a file.

Author:
Workgroup DBIS, University of Konstanz 2005-09, ISC License, Bastian Lemke

Constructor Summary
BufferedFileChannel(java.io.File file)
          Standard constructor for creating a BufferedFileChannel from a complete file.
BufferedFileChannel(java.io.File file, java.nio.ByteBuffer buffer)
          Creates a BufferedFileChannel from a complete file and uses the given buffer for caching the data.
BufferedFileChannel(java.io.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(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.
 java.lang.String getFileName()
          Returns the absolute file name of current file.
 int getInt()
           Relative get method.
 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.
 long remaining()
          Returns the number of remaining bytes in this BufferedFileChannel.
 void reset()
          Resets the channel to its initial position.
 void setByteOrder(java.nio.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(int bytesToRead)
           Creates a "subChannel" view for this BufferedFileChannel instance that shares the underlying FileChannel and buffer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferedFileChannel

public BufferedFileChannel(java.io.File file)
                    throws java.io.IOException
Standard constructor for creating a BufferedFileChannel from a complete file.

Parameters:
file - the file to read from.
Throws:
java.io.IOException - if any error occurs while creating the BufferedFileChannel.

BufferedFileChannel

public BufferedFileChannel(java.io.File file,
                           int bufferSize)
                    throws java.io.IOException
Creates a BufferedFileChannel from a complete file with the given buffer size.

Parameters:
file - the file to read from.
bufferSize - the size of the buffer.
Throws:
java.io.IOException - if any error occurs while creating the BufferedFileChannel.

BufferedFileChannel

public BufferedFileChannel(java.io.File file,
                           java.nio.ByteBuffer buffer)
                    throws java.io.IOException
Creates a 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.

Parameters:
file - the file to read from.
buffer - the ByteBuffer to use for caching the data.
Throws:
java.io.IOException - if any error occurs while creating the BufferedFileChannel.
Method Detail

subChannel

public BufferedFileChannel subChannel(int bytesToRead)
                               throws java.io.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.

Parameters:
bytesToRead - the maximum number of bytes to read.
Returns:
the new "subChannel".
Throws:
java.io.IOException - if any error occurs while creating the channel.

getBufferSize

public int getBufferSize()
Returns the size of the buffer.

Returns:
the size of the buffer.

skip

public void skip(long n)
          throws java.io.IOException
Skips n bytes in the ByteBuffer.

Parameters:
n - number of bytes to skip. May be negative.
Throws:
java.io.IOException - if any error occurs while reading the file.

position

public long position()
              throws java.io.IOException
Returns this channel's position.

Returns:
This channel's position, a non-negative integer counting the number of bytes from the beginning of the channel to the current position
Throws:
java.io.IOException - if any error occurs while reading from the channel.

absolutePosition

public long absolutePosition()
                      throws java.io.IOException
Returns this channel's absolute position.

Returns:
this channel's absolute position.
Throws:
java.io.IOException - if any error occurs while reading from the channel.

position

public void position(long newPosition)
              throws java.io.IOException
Sets this channel's position.

Parameters:
newPosition - the new position, a non-negative integer counting the number of bytes from the beginning of the channel.
Throws:
java.io.IOException - if any error occurs while reading from the channel.

remaining

public long remaining()
Returns the number of remaining bytes in this BufferedFileChannel.

Returns:
the number of remaining bytes.

size

public long size()
          throws java.io.IOException
Returns the size of the channel.

Returns:
the size of the channel.
Throws:
java.io.IOException - if any error occurs while calculating the size.

isSubChannel

public boolean isSubChannel()
                     throws java.io.IOException
Checks if the current BufferedFileChannel instance is a sub-channel.

Returns:
true if the current instance is a sub-channel.
Throws:
java.io.IOException - if any i/o error occurs.

reset

public void reset()
           throws java.io.IOException
Resets the channel to its initial position.

Throws:
java.io.IOException - if any error occurs while reading from the channel.

get

public byte[] get(byte[] dst)
           throws java.io.IOException
Reads dst.length bytes from the BufferedFileChannel.

Parameters:
dst - the arrray to write the data to.
Returns:
the filled byte array.
Throws:
java.io.IOException - if there are less than dst.length bytes available or any error occurs while reading from the channel.

get

public void get(byte[] dst,
                int start,
                int length)

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.

Parameters:
dst - the arrray to write the data to.
start - the first position to write the data to.
length - the number of bytes to read.

get

public int get()

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.

Returns:
The byte at the channel's current position

getShort

public int getShort()

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.

Returns:
The next two bytes at the channel's current position as integer.

getInt

public int getInt()

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.

Returns:
The next four bytes at the channel's current position as integer.

finish

public void finish()
            throws java.io.IOException

Finishes reading from a channel created by subChannel(int). Any subsequent read from this BufferedFileChannel will fail.

This method must be called for every BufferedFileChannel that was created by subChannel(int) instead of calling close() .

Throws:
java.io.IOException - if any error occurs while finishing the channel.
See Also:
close()

close

public void close()
           throws java.io.IOException

Closes the underlying FileChannel.

This method must not be called for BufferedFileChannel instances that were created by subChannel(int).

Throws:
java.io.IOException - if any error occurs while closing the FileChannel.
See Also:
finish()

buffer

public boolean buffer(int n)
               throws java.io.IOException

Buffers n bytes, if the underlying buffer is large enough. Does nothing, if the buffer is too small.

Parameters:
n - the number of bytes to buffer.
Returns:
true if the buffer is large enough to contain all n bytes, false if it is too small.
Throws:
java.io.IOException - if any error occurs while reading the file.

getFileName

public java.lang.String getFileName()
Returns the absolute file name of current file.

Returns:
the file name.

setByteOrder

public void setByteOrder(java.nio.ByteOrder order)
Modifies this channel's byte order.

Parameters:
order - The new byte order, either BIG_ENDIAN or LITTLE_ENDIAN