phelps.io
public class BufferedRandomAccessFile extends RandomAccessFile implements RandomAccess
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(File file, String mode, int bufsize) |
Method Summary | |
---|---|
void | close() |
void | flush() Flush unwritten data from buffer to file. |
long | getFilePointer() |
long | length() |
int | read(byte[] b) |
int | read(byte[] b, int off, int len) |
int | read() |
void | seek(long pos) |
void | setLength(long newLength) |
int | skipBytes(int n) |
void | write(byte[] b) |
void | write(byte[] b, int off, int len) |
void | write(int b) |
void | writeString16(String s)
Same as RandomAccessFile#writeChar(int) for each character of the String in sequence.
|
void | writeString8(String s)
Write a java.lang.String as a sequence of one byte per character, discarding the high byte.
|