com.ureach.detorres.util
Class BufferedRandomAccessFile

java.lang.Object
  extended by java.io.RandomAccessFile
      extended by com.ureach.detorres.util.BufferedRandomAccessFile
All Implemented Interfaces:
Closeable, DataInput, DataOutput

public class BufferedRandomAccessFile
extends RandomAccessFile

This class provides an unsynchronized, buffered RandomAccessFile. It is way, way, way faster (like 25 or 50 times faster) than RandomAccessFile even with a small buffer size of 1024 bytes. This class is from JavaWorld Java Tip 26.

Author:
Nick Zhang

Constructor Summary
BufferedRandomAccessFile(String fileName, String mode, int bufSize)
          Creates a new BufferedRandomAccessFile.
 
Method Summary
 long getFilePointer()
          Returns the virtual file pointer.
 String getNextLine()
          Replaces RandomAccessFile's readLine method which is final.
 int read()
          Returns the next byte in the file.
 int read(byte[] b, int off, int len)
          Reads a block of bytes into an array.
 void seek(long pos)
          Seeks to a particular byte in the file.
 
Methods inherited from class java.io.RandomAccessFile
close, getChannel, getFD, length, read, readBoolean, readByte, readChar, readDouble, readFloat, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF, setLength, skipBytes, write, write, write, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTF
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferedRandomAccessFile

public BufferedRandomAccessFile(String fileName,
                                String mode,
                                int bufSize)
                         throws IOException
Creates a new BufferedRandomAccessFile.

Parameters:
fileName -
mode -
bufSize -
Throws:
IOException
Method Detail

read

public final int read()
               throws IOException
Returns the next byte in the file.

Overrides:
read in class RandomAccessFile
Returns:
the next byte in the lower 8 bits
Throws:
IOException - on any read error

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Reads a block of bytes into an array.

Overrides:
read in class RandomAccessFile
Parameters:
b - the destination array
off - the offset into buf to start writing bytes
len - the number of bytes to read
Returns:
the number of bytes read
Throws:
IOException

getFilePointer

public long getFilePointer()
                    throws IOException
Returns the virtual file pointer.

Overrides:
getFilePointer in class RandomAccessFile
Returns:
the effective file pointer
Throws:
IOException

seek

public void seek(long pos)
          throws IOException
Seeks to a particular byte in the file.

Overrides:
seek in class RandomAccessFile
Parameters:
pos - the desired file position
Throws:
IOException

getNextLine

public final String getNextLine()
                         throws IOException
Replaces RandomAccessFile's readLine method which is final. Reads a line of text (ending in a NEWLINE).

Returns:
the line as a String
Throws:
IOException