Class BufferedRandomAccessFile
- java.lang.Object
-
- org.tinfour.io.BufferedRandomAccessFile
-
- All Implemented Interfaces:
Closeable,DataInput,DataOutput,AutoCloseable
public class BufferedRandomAccessFile extends Object implements Closeable, AutoCloseable, DataInput, DataOutput
Provides a wrapper around a Java Random Access File which supports buffered I/O, dramatically improving the performance for many kinds of random-access file read and write operations.Many of the methods in this class are based on Java's API DataInput and DataOutput interfaces.
Note: It is the intent of this implementation to ultimately be expanded to implement the full Java DataInput and DataOutput interfaces. Because Java defines these methods and accessing a file in big-endian byte order, methods with names such as readInt and writeInt would necessarily have to operate over data given in big-endian order. Thus those methods in the current implementation that operate on multi-byte data types (int, long, short, float, double) all begin with the prefix "le" for "little endian".
Note: At the present time, this implementation does not support operations that seek to positions past the length of the file. That is, of the length of the file is n, the seek(n) is supported but seek(n+1) is not. Thus, operations that would create files with "unwritten" or "unpopulated" blocks of disk space are not supported. Future development of this feature will depend on user interest and a clear definition of the appropriate behavior.
-
-
Constructor Summary
Constructors Constructor Description BufferedRandomAccessFile(File file, String mode)Opens the specified file for read or write access using the specified access mode following the conventions of the Java RandomnAccessFile class.BufferedRandomAccessFile(String fileName, String mode)Opens the specified file for read or write access using the specified access mode following the conventions of the Java RandomnAccessFile class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()Ensures that any pending output stored in the buffer is written to the underlying random access file.FilegetFile()Gets a reference to the file associated with this class.longgetFilePosition()Gets the position within the file at which the next write or read operation will be performed.longgetFileSize()Gets the size of the file in bytes.booleanisClosed()Indicates whether the file is closed.doubleleReadDouble()Reads an 8-byte IEEE-754 floating-point value given in little-endian order.floatleReadFloat()Reads a 4-byte IEEE-754 floating-point value given in little-endian order.voidleReadFloatArray(float[] array, int arrayOffset, int length)Read an array of floats accessing them in little-endian order.intleReadInt()Reads a 4-byte integer given in little-endian order.voidleReadIntArray(int[] array, int arrayOffset, int length)Reads an array of integers accessing them in little-endian order.longleReadLong()Reads an 8-byte integer given in little-endian order.shortleReadShort()Reads an 2-byte integer given in little-endian order.intleReadUnsignedShort()Reads an 2-byte integer given in little-endian order, treating it as an unsigned value.voidleWriteDouble(double v)Writes an 8-byte IEEE-754 floating-point value output in little-endian order.voidleWriteFloat(float v)Writes an 4-byte IEEE-754 floating-point value output in little-endian order.voidleWriteInt(int value)Writes an 4-byte integer given in little-endian order.voidleWriteLong(long v)Writes an 8-byte integer output in little-endian order.voidleWriteShort(int v)Writes an 2-byte integer given in little-endian order.voidprintDiagnostics(PrintStream ps)Prints current state data for file.StringreadASCII(int nBytesToRead)Reads the specified number of bytes from the source file treating them as ASCII characters and returning a valid, potentially zero-length string.intreadASCII(StringBuilder builder, int nBytesToRead)Reads the specified number of bytes from the source file treating them as ASCII characters and appending them to a string builder.booleanreadBoolean()Reads one input byte and returns true if that byte is nonzero, false if that byte is zero.bytereadByte()Reads a single byte from the file, interpreting it as a signed value in the range -128 through 127, inclusive.charreadChar()Reads two input bytes and returns a char value following the general specification of the Java DataInput interface.charreadCharASCII()Reads a single byte from the file, interpreting it as an ASCII character.doublereadDouble()Reads a 8-byte floating-point value in the big-endian order compatible with the Java DataInput interface.floatreadFloat()Reads a 4-byte floating-point value in the big-endian order compatible with the Java DataInput interface.voidreadFully(byte[] array)Reads enough bytes from the input file to fill the specified byte array.voidreadFully(byte[] array, int arrayOffset, int length)Reads the specified number of bytes from the input file into the provided array starting at the specified offset position.Bytes are signed values in the range -128 to 127.intreadInt()Reads a 4-byte integer value in the big-endian order compatible with the Java DataInput interface.StringreadLine()Reads the next linhe of text from the input following the general specifications of the DataInput interface.longreadLong()Reads a 8-byte integer value in the big-endian order compatible with the Java DataInput interface.shortreadShort()Reads a 2-byte (short) integer value in the big-endian order compatible with the Java DataInput interface.intreadUnsignedByte()Reads a single unsigned byte from the file, interpreting it as a signed value in the range 0 through 255, inclusive.intreadUnsignedShort()Reads 2-bytes as an unsignd integer value in the big-endian order compatible with the Java DataInput interface.StringreadUTF()Reads a series of UTF-8 characters following the specifications of the DataOutput interface.voidseek(long position)Sets the file position given as an offset the beginning of the file.intskipBytes(int n)Attempts to skip over n bytes.voidwrite(byte[] b)Writes an array of bytes to the outputvoidwrite(byte[] b, int offset, int length)voidwrite(int b)voidwriteASCII(String s, int nBytes)Write the content of the string as a sequence of characters in the range of the ASCII character set.voidwriteBoolean(boolean v)voidwriteByte(int value)voidwriteBytes(String s)voidwriteChar(int v)Writes a character using two output bytes following the general specification of the Java DataOutput interface.voidwriteChars(String s)voidwriteDouble(double v)voidwriteFloat(float v)voidwriteFully(byte[] array)Write the content of the specified arrayvoidwriteFully(byte[] array, int arrayOffset, int length)Write the specified number of bytes from the byte array starting at the indicated offset (array index).voidwriteInt(int value)Writes a 4 byte integer value to the output in a form consistent with the Java DataOutput interface.voidwriteLong(long v)voidwriteShort(int s)Writes two bytes to the output following the specifications of the Java DataOutput interface.voidwriteUTF(String s)Writes a series of UTF-8 characters following the specifications of the DataOutput interface.
-
-
-
Constructor Detail
-
BufferedRandomAccessFile
public BufferedRandomAccessFile(String fileName, String mode) throws IOException
Opens the specified file for read or write access using the specified access mode following the conventions of the Java RandomnAccessFile class. Since the BufferedRandomAccessFile is a wrapper around the Java class, application developers can expect this class to perform similar operations to the Java class.- Parameters:
fileName- a valid path or file name for opening a filemode- the access mode following the conventions of Java- Throws:
IOException- in the event of an I/O error
-
BufferedRandomAccessFile
public BufferedRandomAccessFile(File file, String mode) throws IOException
Opens the specified file for read or write access using the specified access mode following the conventions of the Java RandomnAccessFile class. Since the BufferedRandomAccessFile is a wrapper around the Java class, application developers can expect this class to perform similar operations to the Java class.- Parameters:
file- a valid file referencemode- the access mode following the conventions of Java- Throws:
IOException- in the event of an I/O error
-
-
Method Detail
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
isClosed
public boolean isClosed()
Indicates whether the file is closed.- Returns:
- true if the file is closed; otherwise, true
-
flush
public void flush() throws IOExceptionEnsures that any pending output stored in the buffer is written to the underlying random access file.- Throws:
IOException- if an I/O error occurs.
-
getFileSize
public long getFileSize()
Gets the size of the file in bytes.- Returns:
- a positive long integer value.
-
getFilePosition
public long getFilePosition()
Gets the position within the file at which the next write or read operation will be performed. The file position is measured in bytes and given as an offset from the first byte in the file (i.e. the first byte is at position zero).- Returns:
- a positive long integer value
-
getFile
public File getFile()
Gets a reference to the file associated with this class.- Returns:
- a valid file reference.
-
leReadDouble
public double leReadDouble() throws IOExceptionReads an 8-byte IEEE-754 floating-point value given in little-endian order.- Returns:
- a floating-point value, potentially NaN or INFINITY
- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leReadFloat
public float leReadFloat() throws IOExceptionReads a 4-byte IEEE-754 floating-point value given in little-endian order.- Returns:
- a floating-point value, potentially NaN or INFINITY
- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leReadInt
public int leReadInt() throws IOExceptionReads a 4-byte integer given in little-endian order.- Returns:
- a valid integer
- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leReadLong
public long leReadLong() throws IOExceptionReads an 8-byte integer given in little-endian order.- Returns:
- a valid integer
- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leReadShort
public short leReadShort() throws IOExceptionReads an 2-byte integer given in little-endian order.- Returns:
- a valid Java short
- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leReadUnsignedShort
public int leReadUnsignedShort() throws IOExceptionReads an 2-byte integer given in little-endian order, treating it as an unsigned value.- Returns:
- a Java int with a positive value
- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leWriteShort
public void leWriteShort(int v) throws IOExceptionWrites an 2-byte integer given in little-endian order.- Parameters:
v- an integral value.- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leWriteInt
public void leWriteInt(int value) throws IOExceptionWrites an 4-byte integer given in little-endian order.- Parameters:
value- an integral value.- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leWriteLong
public void leWriteLong(long v) throws IOExceptionWrites an 8-byte integer output in little-endian order.- Parameters:
v- an integral value.- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leWriteFloat
public void leWriteFloat(float v) throws IOExceptionWrites an 4-byte IEEE-754 floating-point value output in little-endian order.- Parameters:
v- an integral value.- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leWriteDouble
public void leWriteDouble(double v) throws IOExceptionWrites an 8-byte IEEE-754 floating-point value output in little-endian order.- Parameters:
v- an integral value.- Throws:
IOException- in the event of an unrecoverable I/O exception.
-
leReadIntArray
public void leReadIntArray(int[] array, int arrayOffset, int length) throws IOExceptionReads an array of integers accessing them in little-endian order.- Parameters:
array- a valid, non-zero sized arrayarrayOffset- the starting position within the array.length- the number of values to read.- Throws:
IOException- in the event of an I/O error.
-
leReadFloatArray
public void leReadFloatArray(float[] array, int arrayOffset, int length) throws IOExceptionRead an array of floats accessing them in little-endian order.- Parameters:
array- a valid, non-zero sized arrayarrayOffset- the starting position within the array.length- the number of values to read.- Throws:
IOException- in the event of an I/O error.
-
readBoolean
public boolean readBoolean() throws IOExceptionReads one input byte and returns true if that byte is nonzero, false if that byte is zero. This method is suitable for reading the byte written by the writeBoolean method of this class.- Specified by:
readBooleanin interfaceDataInput- Returns:
- the boolean value read from the source file.
- Throws:
IOException- if an I/O error occurs.
-
readByte
public byte readByte() throws IOExceptionReads a single byte from the file, interpreting it as a signed value in the range -128 through 127, inclusive. This method is suitable for reading the byte written by the writeByte method of this class.- Specified by:
readBytein interfaceDataInput- Returns:
- a signed by value.
- Throws:
IOException- if an I/O error occurs.
-
readCharASCII
public char readCharASCII() throws IOExceptionReads a single byte from the file, interpreting it as an ASCII character.- Returns:
- a character in the range 0 to 255.
- Throws:
IOException- if an I/O error occurs.
-
readFully
public void readFully(byte[] array) throws IOExceptionReads enough bytes from the input file to fill the specified byte array. Bytes are signed values in the range -128 to 127.- Specified by:
readFullyin interfaceDataInput- Parameters:
array- a valid array- Throws:
IOException- if an I/O error occurs, including an end-of-file condition.
-
readFully
public void readFully(byte[] array, int arrayOffset, int length) throws IOExceptionReads the specified number of bytes from the input file into the provided array starting at the specified offset position.Bytes are signed values in the range -128 to 127.- Specified by:
readFullyin interfaceDataInput- Parameters:
array- a valid arrayarrayOffset- the starting index in the arraylength- the number of bytes to read- Throws:
IOException- if an I/O error occurs, including an end-of-file condition.
-
readUnsignedByte
public int readUnsignedByte() throws IOExceptionReads a single unsigned byte from the file, interpreting it as a signed value in the range 0 through 255, inclusive. This method is suitable for reading the byte written by the writeByte method of this class.- Specified by:
readUnsignedBytein interfaceDataInput- Returns:
- a signed by value.
- Throws:
IOException- if an I/O error occurs.
-
readASCII
public int readASCII(StringBuilder builder, int nBytesToRead) throws IOException
Reads the specified number of bytes from the source file treating them as ASCII characters and appending them to a string builder. If a zero byte is detected, it is treated as a null terminator and the additional bytes will not be appended to the builder, but the file position will be advanced by the specified number of bytes.- Parameters:
builder- a valid StringBuilder instance, not necessarily empty.nBytesToRead- the number of bytes to read from the file- Returns:
- the length of the null-terminated string read from the file (not the length of the StringBuilder content).
- Throws:
IOException- if an I/O error occurs.
-
readASCII
public String readASCII(int nBytesToRead) throws IOException
Reads the specified number of bytes from the source file treating them as ASCII characters and returning a valid, potentially zero-length string. If a zero byte is detected in the input, it is treated as a null terminator and the additional bytes will not be appended to the string, but the file position will be advanced by the specified number of bytes.- Parameters:
nBytesToRead- the number of bytes to read from the file- Returns:
- the length of the null-terminated string read from the file (not the length of the StringBuilder content).
- Throws:
IOException- if an I/O error occurs.
-
seek
public void seek(long position) throws IOExceptionSets the file position given as an offset the beginning of the file. This setting gives the position at which the next read or write will occur.- Parameters:
position- the file-pointer offset position- Throws:
IOException- if the position is less than zero or an I/O error occurs.
-
skipBytes
public int skipBytes(int n) throws IOExceptionAttempts to skip over n bytes.- Specified by:
skipBytesin interfaceDataInput- Parameters:
n- the number of bytes to skip- Returns:
- the actual number of bytes skipped.
- Throws:
IOException- if an I/O error occurs.
-
writeByte
public void writeByte(int value) throws IOException- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeFully
public void writeFully(byte[] array) throws IOExceptionWrite the content of the specified array- Parameters:
array- a valid array- Throws:
IOException- in the event of an unhandled I/O exception.
-
writeFully
public void writeFully(byte[] array, int arrayOffset, int length) throws IOExceptionWrite the specified number of bytes from the byte array starting at the indicated offset (array index).- Parameters:
array- a valid arrayarrayOffset- the starting index into the array.length- the number of bytes to be written.- Throws:
IOException- in the event of an unhandled I/O exception.
-
write
public void write(int b) throws IOException- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
public void write(byte[] b) throws IOExceptionWrites an array of bytes to the output- Specified by:
writein interfaceDataOutput- Parameters:
b- a valid array of bytes- Throws:
IOException- in the event of an unexpected I/O condition
-
write
public void write(byte[] b, int offset, int length) throws IOException- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean v) throws IOException- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeBytes
public void writeBytes(String s) throws IOException
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeASCII
public void writeASCII(String s, int nBytes) throws IOException
Write the content of the string as a sequence of characters in the range of the ASCII character set. If the length of the output string exceeds the specified number of bytes, the string is truncated. If the length of the string is less than the specified number of bytes, zeroes are written to the output.- Parameters:
s- a valid string.nBytes- the number of bytes to be written to the file.- Throws:
IOException- in the event of an unhandled I/O exception.
-
writeShort
public void writeShort(int s) throws IOExceptionWrites two bytes to the output following the specifications of the Java DataOutput interface.- Specified by:
writeShortin interfaceDataOutput- Parameters:
s- an integer value. If the value is outside the range of a short integer (-32768 to 32767), only the two low-order bytes will be written.- Throws:
IOException- in the event of an I/O error
-
writeInt
public void writeInt(int value) throws IOExceptionWrites a 4 byte integer value to the output in a form consistent with the Java DataOutput interface.- Specified by:
writeIntin interfaceDataOutput- Parameters:
value- an integer value- Throws:
IOException- in the event of an I/O error
-
writeUTF
public void writeUTF(String s) throws IOException
Writes a series of UTF-8 characters following the specifications of the DataOutput interface. The first two bytes give a short integer indicating the length of the string specification, followed by the bytes comprising the UTF-8 encoded characters for the string. Note that some of the UTF-8 characters in the string may require more than a single byte for their representation.- Specified by:
writeUTFin interfaceDataOutput- Parameters:
s- a valid String- Throws:
IOException- in the event of an I/O error.
-
readDouble
public double readDouble() throws IOExceptionReads a 8-byte floating-point value in the big-endian order compatible with the Java DataInput interface.- Specified by:
readDoublein interfaceDataInput- Returns:
- if successful, a valid float value
- Throws:
IOException- in the event of an I/O error
-
readFloat
public float readFloat() throws IOExceptionReads a 4-byte floating-point value in the big-endian order compatible with the Java DataInput interface.- Specified by:
readFloatin interfaceDataInput- Returns:
- if successful, a valid float value
- Throws:
IOException- in the event of an I/O error
-
readInt
public int readInt() throws IOExceptionReads a 4-byte integer value in the big-endian order compatible with the Java DataInput interface.- Specified by:
readIntin interfaceDataInput- Returns:
- if successful, a valid integer value
- Throws:
IOException- in the event of an I/O error
-
readLong
public long readLong() throws IOExceptionReads a 8-byte integer value in the big-endian order compatible with the Java DataInput interface.- Specified by:
readLongin interfaceDataInput- Returns:
- if successful, a valid integer value
- Throws:
IOException- in the event of an I/O error
-
readShort
public short readShort() throws IOExceptionReads a 2-byte (short) integer value in the big-endian order compatible with the Java DataInput interface.- Specified by:
readShortin interfaceDataInput- Returns:
- if successful, a valid signed short value
- Throws:
IOException- in the event of an I/O error
-
readUnsignedShort
public int readUnsignedShort() throws IOExceptionReads 2-bytes as an unsignd integer value in the big-endian order compatible with the Java DataInput interface.- Specified by:
readUnsignedShortin interfaceDataInput- Returns:
- if successful, a valid signed short value
- Throws:
IOException- in the event of an I/O error
-
readUTF
public String readUTF() throws IOException
Reads a series of UTF-8 characters following the specifications of the DataOutput interface. The first two bytes give a short integer indicating the length of the string specification, followed by the bytes comprising the UTF-8 encoded characters for the string. Note that some of the UTF-8 characters in the string may require more than a single byte for their representation.- Specified by:
readUTFin interfaceDataInput- Returns:
- a valid, potentially empty string.
- Throws:
IOException- in the event of an I/O error.
-
readLine
public String readLine() throws IOException
Reads the next linhe of text from the input following the general specifications of the DataInput interface. Note that this method reads characters one byte at a time and does not fully implement the Unicode character set.- Specified by:
readLinein interfaceDataInput- Returns:
- if successful, a valid potentially empty string; if unsuccessful, a null.
- Throws:
IOException- in the event of an I/O error.
-
printDiagnostics
public void printDiagnostics(PrintStream ps)
Prints current state data for file. Intended for debugging and development.- Parameters:
ps- a valid PrintStream for writing the output (may be System.out).
-
readChar
public char readChar() throws IOExceptionReads two input bytes and returns a char value following the general specification of the Java DataInput interface.- Specified by:
readCharin interfaceDataInput- Returns:
- a valid char
- Throws:
IOException- in the event of an I/O error.
-
writeChar
public void writeChar(int v) throws IOExceptionWrites a character using two output bytes following the general specification of the Java DataOutput interface.- Specified by:
writeCharin interfaceDataOutput- Parameters:
v- a valid char; due to the two-byte limitation, not all UTF characters are supported- Throws:
IOException- in the event of an I/O error.
-
writeLong
public void writeLong(long v) throws IOException- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeFloat
public void writeFloat(float v) throws IOException- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeDouble
public void writeDouble(double v) throws IOException- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeChars
public void writeChars(String s) throws IOException
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
-