com.continuent.tungsten.common.io
Class BufferedFileDataInput

java.lang.Object
  extended by com.continuent.tungsten.common.io.BufferedFileDataInput

public class BufferedFileDataInput
extends java.lang.Object

Merges the capabilities of the following stream classes into a single class: FileInputStream, BufferedInputStream, and DataInputStream. This allows us to manage buffered data reads from files efficiently.

Author:
Robert Hodges

Constructor Summary
BufferedFileDataInput(java.io.File file)
          Creates instance with default buffer size.
BufferedFileDataInput(java.io.File file, int size)
          Creates instance positioned on start of file.
 
Method Summary
 long available()
          Query the stream directly for the number of bytes available for immediate read without blocking.
 void close()
          Close and release all resources.
 long getOffset()
          Returns the current offset position.
 void mark(int readLimit)
          Mark stream to read up to limit.
 byte readByte()
          Reads a single byte.
 void readFully(byte[] bytes)
          Reads a full byte array completely.
 void readFully(byte[] bytes, int start, int len)
          Reads a full byte array completely.
 int readInt()
          Read a single integer.
 long readLong()
          Reads a single long.
 short readShort()
          Reads a single short.
 void reset()
          Reset stream back to last mark.
 void seek(long seekBytes)
          Seek to a specific offset in the file.
 long skip(long bytes)
          Skip requested number of bytes.
 java.lang.String toString()
          Print contents of the reader.
 long waitAvailable(int requested, int waitMillis)
          Waits for a specified number of bytes to be available for a non-blocking read.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BufferedFileDataInput

public BufferedFileDataInput(java.io.File file,
                             int size)
                      throws java.io.FileNotFoundException,
                             java.io.IOException,
                             java.lang.InterruptedException
Creates instance positioned on start of file.

Parameters:
file - File from which to read
size - Size of buffer for buffered I/O
Throws:
java.io.FileNotFoundException
java.io.IOException
java.lang.InterruptedException

BufferedFileDataInput

public BufferedFileDataInput(java.io.File file)
                      throws java.io.FileNotFoundException,
                             java.io.IOException,
                             java.lang.InterruptedException
Creates instance with default buffer size.

Throws:
java.io.FileNotFoundException
java.io.IOException
java.lang.InterruptedException
Method Detail

getOffset

public long getOffset()
Returns the current offset position.


available

public long available()
               throws java.io.IOException,
                      java.lang.InterruptedException
Query the stream directly for the number of bytes available for immediate read without blocking. This operation may result in a file system metadata call. To find out if a specific number of bytes are known to be available use waitForAvailable().

Returns:
Number of bytes available for non-blocking read
Throws:
java.io.IOException
java.lang.InterruptedException

waitAvailable

public long waitAvailable(int requested,
                          int waitMillis)
                   throws java.io.IOException,
                          java.lang.InterruptedException
Waits for a specified number of bytes to be available for a non-blocking read.

Parameters:
requested - Number of bytes to read
waitMillis - Milliseconds to wait before timeout
Returns:
Number of bytes available for non-blocking read
Throws:
java.io.IOException - Thrown if there is a problem checking for available bytes
java.lang.InterruptedException - Thrown if we are interrupted while waiting

mark

public void mark(int readLimit)
Mark stream to read up to limit.

Parameters:
readLimit - Number of bytes that may be read before resetting

reset

public void reset()
           throws java.io.IOException,
                  java.lang.InterruptedException
Reset stream back to last mark.

Throws:
java.io.IOException - Thrown if mark has been invalidated or not set
java.lang.InterruptedException - Thrown if we are interrupted

skip

public long skip(long bytes)
          throws java.io.IOException
Skip requested number of bytes.

Parameters:
bytes - Number of bytes to skip
Returns:
Number of bytes actually skipped
Throws:
java.io.IOException - Thrown if seek not supported or other error

seek

public void seek(long seekBytes)
          throws java.io.FileNotFoundException,
                 java.io.IOException,
                 java.lang.InterruptedException
Seek to a specific offset in the file.

Parameters:
seekBytes - Number of bytes from start of file
Throws:
java.io.IOException - Thrown if offset cannot be found
java.io.FileNotFoundException - Thrown if file is not found
java.lang.InterruptedException - Thrown if thread is interrupted

readByte

public byte readByte()
              throws java.io.IOException
Reads a single byte.

Throws:
java.io.IOException

readShort

public short readShort()
                throws java.io.IOException
Reads a single short.

Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Read a single integer.

Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Reads a single long.

Throws:
java.io.IOException

readFully

public void readFully(byte[] bytes)
               throws java.io.IOException
Reads a full byte array completely.

Throws:
java.io.IOException - Thrown if full byte array cannot be read

readFully

public void readFully(byte[] bytes,
                      int start,
                      int len)
               throws java.io.IOException
Reads a full byte array completely.

Parameters:
bytes - Buffer into which to read
start - Starting byte position
len - Number of bytes to read
Throws:
java.io.IOException - Thrown if data cannot be read

close

public void close()
Close and release all resources.


toString

public java.lang.String toString()
Print contents of the reader.

Overrides:
toString in class java.lang.Object