phelps.io

Class BufferedRandomAccessFile

public class BufferedRandomAccessFile extends RandomAccessFile implements RandomAccess

Buffers both read and write access to java.io.RandomAccessFile, which enormously speeds I/O. Also consider Java 1.4's java.nio. This is class is useful for old code and with libraries, as one can simply make a new BuffeadedRandomAccessFile() instance and pass to existing code wherever a RandomAccessFile is expected, without rewriting that code. This class has been used extensively and tested extensively with long sequence of random file operations. Nevertheless: NO WARRENTY.

WARNING! This class is not an exact drop-in replacement for RandomAccessFile. Do not use the two methods inherited from RandomAccessFile: RandomAccessFile#writeBytes(String s) and RandomAccessFile#writeChars(String s). They are not compatible with buffered use. Instead use the methods writeString8 and writeString16, respectively. (Also, java.lang.Strings can also be written as UTF-8 with RandomAccessFile#writeUTF(String)).

The technical reason for the warning is that the methods themselves are final and ultimately write with the private, and hence non-overrideable, method writeBytes(byte[] b, int off, int len). Other methods all other write methods use write()/write(byte[])/write(byte[],int,int) for low-level byte writing, and since those methods are overriden in this class, all is well. This class could be written with the exact same interface as java.io.RandomAccessFile (by nesting rather than subclassing). However this would require all code using the class to be recompiled, which is often not an option, especially in third-party code. A small change by Sun would make those two methods valid in this class as well -- vote for it.

JavaWorld Tip 26 implements a read-only version.

Version: $Revision: 1.20 $ $Date: 2003/06/01 08:08:19 $

Constructor Summary
BufferedRandomAccessFile(File file, String mode)
BufferedRandomAccessFile(String name, String mode)
BufferedRandomAccessFile(String name, String mode, int bufsize)
BufferedRandomAccessFile(File file, String mode, int bufsize)
Method Summary
voidclose()
voidflush()
Flush unwritten data from buffer to file.
longgetFilePointer()
longlength()
intread(byte[] b)
intread(byte[] b, int off, int len)
intread()
voidseek(long pos)
voidsetLength(long newLength)
voidwrite(byte[] b)
voidwrite(byte[] b, int off, int len)
voidwrite(int b)
voidwriteString16(String s)
Same as RandomAccessFile#writeChar(int) for each character of the String in sequence.
voidwriteString8(String s)
Write a java.lang.String as a sequence of one byte per character, discarding the high byte.

Constructor Detail

BufferedRandomAccessFile

public BufferedRandomAccessFile(File file, String mode)

BufferedRandomAccessFile

public BufferedRandomAccessFile(String name, String mode)

BufferedRandomAccessFile

public BufferedRandomAccessFile(String name, String mode, int bufsize)

BufferedRandomAccessFile

public BufferedRandomAccessFile(File file, String mode, int bufsize)

Method Detail

close

public void close()

flush

public void flush()
Flush unwritten data from buffer to file. Buffer and file pointer are unchanged.

getFilePointer

public long getFilePointer()

length

public long length()

read

public int read(byte[] b)

read

public int read(byte[] b, int off, int len)

read

public int read()

seek

public void seek(long pos)

setLength

public void setLength(long newLength)

write

public void write(byte[] b)

write

public void write(byte[] b, int off, int len)

write

public void write(int b)

writeString16

public void writeString16(String s)
Same as RandomAccessFile#writeChar(int) for each character of the String in sequence. Like RandomAccessFile#writeChars(String), which for technical reasons must not be used.

writeString8

public void writeString8(String s)
Write a java.lang.String as a sequence of one byte per character, discarding the high byte. Like BufferedRandomAccessFile, which for technical reasons must not be used.