adaptj.util
Class BufferedFileReader

java.lang.Object
  |
  +--adaptj.util.BufferedFileReader
All Implemented Interfaces:
java.io.DataInput

public class BufferedFileReader
extends java.lang.Object
implements java.io.DataInput

A utility class based on RandomAccessFile, but with a buffer. This class is solely used for data input (no output). The major advantage of the RandomAccessFile is efficient implementation of seek(), which allows to seek a position in the file for reading. This is required by the AEFReader class, which needs to reset the file position after it is done parsing the input file.

Author:
Bruno Dufour
See Also:
RandomAccessFile

Field Summary
protected  byte[] buff
           
protected  int buffSize
           
static int DEFAULT_BUFFER_SIZE
           
protected  java.io.RandomAccessFile raFile
           
 
Constructor Summary
BufferedFileReader(java.io.File file)
          Constructs a new BufferedFileReader with the default buffer size for the file specified by file.
BufferedFileReader(java.io.File file, int bufferSize)
          Constructs a new BufferedFileReader with the specified buffer size for the file specified by file.
BufferedFileReader(java.lang.String name)
          Constructs a new BufferedFileReader with the default buffer size for the file specified by name.
BufferedFileReader(java.lang.String name, int bufferSize)
          Constructs a new BufferedFileReader with the specified buffer size for the file specified by name.
 
Method Summary
 void close()
          Closes this BufferedFileReader and releases any system resources associated with it.
 long getFilePointer()
          Returns the current offset in this file.
 long length()
          Returns the length of this file.
 int read()
          Reads a byte of data from this file.
 int read(byte[] b)
          Reads up to b.length bytes of data from this file into an array of bytes.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from this file into an array of bytes.
 boolean readBoolean()
          Reads a boolean from this file.
 byte readByte()
          Reads a signed eight-bit value from this file.
 char readChar()
          Reads a Unicode character from this file.
 double readDouble()
          Reads a double from this file.
 float readFloat()
          Reads a float from this file.
 void readFully(byte[] b)
           
 void readFully(byte[] b, int off, int len)
           
 int readInt()
          Reads a signed 32-bit integer from this file.
 java.lang.String readLine()
          Reads the next line of text from this file.
 long readLong()
          Reads a signed 64-bit integer from this file.
 short readShort()
          Reads a signed 16-bit number from this file.
 int readUnsignedByte()
          Reads an unsigned eight-bit number from this file.
 int readUnsignedShort()
          Reads an unsigned 16-bit number from this file.
 java.lang.String readUTF()
          Reads in a string from this file.
 void seek(long pos)
          Sets the file-pointer offset, measured from the beginning of this file, at which the next read occurs.
 int skipBytes(int n)
          Attempts to skip over n bytes of input discarding the skipped bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buff

protected byte[] buff

raFile

protected java.io.RandomAccessFile raFile

buffSize

protected int buffSize

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

BufferedFileReader

public BufferedFileReader(java.lang.String name)
                   throws java.io.IOException
Constructs a new BufferedFileReader with the default buffer size for the file specified by name.

Parameters:
name - the name of the file to open for reading.

BufferedFileReader

public BufferedFileReader(java.lang.String name,
                          int bufferSize)
                   throws java.io.IOException
Constructs a new BufferedFileReader with the specified buffer size for the file specified by name.

Parameters:
name - the name of the file to open for reading.
bufferSize - the size of the buffer (in bytes) that will be used

BufferedFileReader

public BufferedFileReader(java.io.File file)
                   throws java.io.IOException
Constructs a new BufferedFileReader with the default buffer size for the file specified by file.

Parameters:
file - a File object representating file to open for reading.

BufferedFileReader

public BufferedFileReader(java.io.File file,
                          int bufferSize)
                   throws java.io.IOException
Constructs a new BufferedFileReader with the specified buffer size for the file specified by file.

Parameters:
file - a File object representating file to open for reading.
bufferSize - the size of the buffer (in bytes) that will be used
Method Detail

read

public int read()
         throws java.io.IOException
Reads a byte of data from this file. The byte is returned as an integer in the range 0 to 255 (0x00-0x0ff). This method blocks if no input is yet available. If there is at least 1 byte available in the buffer, this byte will be returned. Otherwise, a call to this method result in the buffer being filled (as much as possible).

Returns:
the next byte of data, or -1 if the end of the file has been reached.
Throws:
java.io.IOException - if an I/O error occurs. Not thrown if end-of-file has been reached.

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.

Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the maximum number of bytes read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read(byte[] b)
         throws java.io.IOException
Reads up to b.length bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.

Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of this file has been reached.
Throws:
java.io.IOException - if an I/O error occurs.

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Reads a boolean from this file. This method reads a single byte from the file, starting at the current file pointer. A value of 0 represents false. Any other value represents true. This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.

Specified by:
readBoolean in interface java.io.DataInput
Returns:
the boolean value read.
Throws:
java.io.EOFException - if this file has reached the end.
java.io.IOException - if an I/O error occurs.

readByte

public byte readByte()
              throws java.io.IOException
Reads a signed eight-bit value from this file. This method reads a byte from the file, starting from the current file pointer. If the byte read is b, where 0 <= b <= 255, then the result is:
     (byte)(b)
 

This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.

Specified by:
readByte in interface java.io.DataInput
Returns:
the next byte of this file as a signed eight-bit byte.
Throws:
java.io.EOFException - if this file has reached the end.
java.io.IOException - if an I/O error occurs.

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Reads an unsigned eight-bit number from this file. This method reads a byte from this file, starting at the current file pointer, and returns that byte.

This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
the next byte of this file, interpreted as an unsigned eight-bit number.
Throws:
java.io.EOFException - if this file has reached the end.
java.io.IOException - if an I/O error occurs.

readChar

public char readChar()
              throws java.io.IOException
Reads a Unicode character from this file. This method reads two bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1 and b2, where 0 <= b1, b2 <= 255, then the result is equal to:
     (char)((b1 << 8) | b2)
 

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readChar in interface java.io.DataInput
Returns:
the next two bytes of this file as a Unicode character.
Throws:
java.io.EOFException - if this file reaches the end before reading two bytes.
java.io.IOException - if an I/O error occurs.

readDouble

public double readDouble()
                  throws java.io.IOException
Reads a double from this file. This method reads a long value, starting at the current file pointer, as if by the readLong method and then converts that long to a double using the longBitsToDouble method in class Double.

This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readDouble in interface java.io.DataInput
Returns:
the next eight bytes of this file, interpreted as a double.
Throws:
java.io.EOFException - if this file reaches the end before reading eight bytes.
java.io.IOException - if an I/O error occurs.
See Also:
RandomAccessFile.readLong(), Double.longBitsToDouble(long)

readFloat

public float readFloat()
                throws java.io.IOException
Reads a float from this file. This method reads an int value, starting at the current file pointer, as if by the readInt method and then converts that int to a float using the intBitsToFloat method in class Float.

This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readFloat in interface java.io.DataInput
Returns:
the next four bytes of this file, interpreted as a float.
Throws:
java.io.EOFException - if this file reaches the end before reading four bytes.
java.io.IOException - if an I/O error occurs.
See Also:
RandomAccessFile.readInt(), Float.intBitsToFloat(int)

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput
java.io.IOException

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to:
     (b1 << 24) | (b2 << 16) + (b3 << 8) + b4
 

This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readInt in interface java.io.DataInput
Returns:
the next four bytes of this file, interpreted as an int.
Throws:
java.io.EOFException - if this file reaches the end before reading four bytes.
java.io.IOException - if an I/O error occurs.

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Reads the next line of text from this file. This method successively reads bytes from the file, starting at the current file pointer, until it reaches a line terminator or the end of the file. Each byte is converted into a character by taking the byte's value for the lower eight bits of the character and setting the high eight bits of the character to zero. This method does not, therefore, support the full Unicode character set.

A line of text is terminated by a carriage-return character ('\r'), a newline character ('\n'), a carriage-return character immediately followed by a newline character, or the end of the file. Line-terminating characters are discarded and are not included as part of the string returned.

This method blocks until a newline character is read, a carriage return and the byte following it are read (to see if it is a newline), the end of the file is reached, or an exception is thrown.

Specified by:
readLine in interface java.io.DataInput
Returns:
the next line of text from this file, or null if end of file is encountered before even one byte is read.
Throws:
java.io.IOException - if an I/O error occurs.

readLong

public long readLong()
              throws java.io.IOException
Reads a signed 64-bit integer from this file. This method reads eight bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, b4, b5, b6, b7, and b8, where:
     0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
 

then the result is equal to:

     ((long)b1 << 56) + ((long)b2 << 48)
     + ((long)b3 << 40) + ((long)b4 << 32)
     + ((long)b5 << 24) + ((long)b6 << 16)
     + ((long)b7 << 8) + b8
 

This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readLong in interface java.io.DataInput
Returns:
the next eight bytes of this file, interpreted as a long.
Throws:
java.io.EOFException - if this file reaches the end before reading eight bytes.
java.io.IOException - if an I/O error occurs.

readShort

public short readShort()
                throws java.io.IOException
Reads a signed 16-bit number from this file. The method reads two bytes from this file, starting at the current file pointer. If the two bytes read, in order, are b1 and b2, where each of the two values is between 0 and 255, inclusive, then the result is equal to:
     (short)((b1 << 8) | b2)
 

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readShort in interface java.io.DataInput
Returns:
the next two bytes of this file, interpreted as a signed 16-bit number.
Throws:
java.io.EOFException - if this file reaches the end before reading two bytes.
java.io.IOException - if an I/O error occurs.

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Reads an unsigned 16-bit number from this file. This method reads two bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1 and b2, where 0 <= b1, b2 <= 255, then the result is equal to:
     (b1 << 8) | b2
 

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
the next two bytes of this file, interpreted as an unsigned 16-bit integer.
Throws:
java.io.EOFException - if this file reaches the end before reading two bytes.
java.io.IOException - if an I/O error occurs.

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Reads in a string from this file. The string has been encoded using a modified UTF-8 format.

The first two bytes are read, starting from the current file pointer, as if by readUnsignedShort. This value gives the number of following bytes that are in the encoded string, not the length of the resulting string. The following bytes are then interpreted as bytes encoding characters in the UTF-8 format and are converted into characters.

This method blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readUTF in interface java.io.DataInput
Returns:
a Unicode string.
Throws:
java.io.EOFException - if this file reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.
java.io.UTFDataFormatException - if the bytes do not represent valid UTF-8 encoding of a Unicode string.
See Also:
RandomAccessFile.readUnsignedShort()

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Attempts to skip over n bytes of input discarding the skipped bytes.

This method may skip over some smaller number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. This method never throws an EOFException. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

Specified by:
skipBytes in interface java.io.DataInput
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if an I/O error occurs.

getFilePointer

public long getFilePointer()
                    throws java.io.IOException
Returns the current offset in this file.

Returns:
the offset from the beginning of the file, in bytes, at which the next read occurs. The buffering does not affect the value obtained from this method.
Throws:
java.io.IOException - if an I/O error occurs.

seek

public void seek(long pos)
          throws java.io.IOException
Sets the file-pointer offset, measured from the beginning of this file, at which the next read occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The buffering does not influence the value obtained from this method.

Parameters:
pos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
Throws:
java.io.IOException - if pos is less than 0 or if an I/O error occurs.

close

public void close()
           throws java.io.IOException
Closes this BufferedFileReader and releases any system resources associated with it. A closed BufferedFileReader cannot perform input operations and cannot be reopened.

Throws:
java.io.IOException - if an I/O error occurs.

length

public long length()
            throws java.io.IOException
Returns the length of this file.

Returns:
the length of this file, measured in bytes.
Throws:
java.io.IOException - if an I/O error occurs.