public class BufferedRandomAccessFile extends Object implements Closeable, AutoCloseable, DataInput, DataOutput
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 and 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.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
flush()
Ensures that any pending output stored in the buffer is written to the
underlying random access file.
|
File |
getFile()
Gets a reference to the file associated with this class.
|
long |
getFilePosition()
Gets the position within the file at which the next write or read operation
will be performed.
|
long |
getFileSize()
Gets the size of the file in bytes.
|
boolean |
isClosed()
Indicates whether the file is closed.
|
double |
leReadDouble() |
float |
leReadFloat() |
void |
leReadFloatArray(float[] array,
int arrayOffset,
int length)
Read an array of floats accessing them in little-endian order.
|
int |
leReadInt() |
void |
leReadIntArray(int[] array,
int arrayOffset,
int length)
Reads an array of integers accessing them in little-endian order.
|
long |
leReadLong() |
short |
leReadShort() |
void |
leReadShortArray(short[] array,
int arrayOffset,
int length)
Reads an array of integers accessing them in little-endian order.
|
int |
leReadUnsignedShort() |
String |
leReadUTF()
Reads a series of UTF-8 characters based on the little-endian byte order
The first two bytes give a short integer indicating
the length of the string specification in bytes.
|
void |
leWriteDouble(double v) |
void |
leWriteFloat(float v) |
void |
leWriteInt(int value) |
void |
leWriteLong(long v) |
void |
leWriteShort(int v) |
void |
leWriteUTF(String s)
Writes a series of UTF-8 characters using the little-endian byte order.
|
void |
printDiagnostics(PrintStream ps)
Prints current state data for file.
|
String |
readASCII(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.
|
int |
readASCII(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.
|
boolean |
readBoolean()
Reads one input byte and returns true if that byte is nonzero, false if
that byte is zero.
|
byte |
readByte()
Reads a single byte from the file, interpreting it as a signed value in the
range -128 through 127, inclusive.
|
char |
readChar()
Reads two input bytes and returns a char value following the general
specification of the Java DataInput interface.
|
char |
readCharASCII()
Reads a single byte from the file, interpreting it as an ASCII character.
|
double |
readDouble()
Reads a 8-byte floating-point value in the big-endian order compatible with
the Java DataInput interface.
|
float |
readFloat()
Reads a 4-byte floating-point value in the big-endian order compatible with
the Java DataInput interface.
|
void |
readFully(byte[] array)
Reads enough bytes from the input file to fill the specified byte array.
|
void |
readFully(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.
|
int |
readInt()
Reads a 4-byte integer value in the big-endian order compatible with the
Java DataInput interface.
|
String |
readLine()
Reads the next line of text from the input following the general
specifications of the DataInput interface.
|
long |
readLong()
Reads a 8-byte integer value in the big-endian order compatible with the
Java DataInput interface.
|
short |
readShort()
Reads a 2-byte (short) integer value in the big-endian order compatible
with the Java DataInput interface.
|
int |
readUnsignedByte()
Reads a single unsigned byte from the file, interpreting it as a signed
value in the range 0 through 255, inclusive.
|
int |
readUnsignedShort()
Reads 2-bytes as an unsigned integer value in the big-endian order
compatible with the Java DataInput interface.
|
String |
readUTF()
Reads a series of UTF-8 characters following the specifications of the
DataOutput interface.
|
void |
seek(long position)
Sets the file position given as an offset the beginning of the file.
|
int |
skipBytes(int n)
Attempts to skip over n bytes.
|
void |
write(byte[] b)
Writes an array of bytes to the output
|
void |
write(byte[] b,
int offset,
int length) |
void |
write(int b) |
void |
writeASCII(String s,
int nBytes) |
void |
writeBoolean(boolean v) |
void |
writeByte(int value) |
void |
writeBytes(String s) |
void |
writeChar(int v)
Writes a character using two output bytes following the general
specification of the Java DataOutput interface.
|
void |
writeChars(String s) |
void |
writeDouble(double v) |
void |
writeFloat(float v) |
void |
writeFully(byte[] array) |
void |
writeFully(byte[] array,
int arrayOffset,
int length) |
void |
writeInt(int value)
Writes a 4 byte integer value to the output in a form consistent with the
Java DataOutput interface.
|
void |
writeLong(long v) |
void |
writeShort(int s)
Writes two bytes to the output following the specifications of the Java
DataOutput interface.
|
void |
writeUTF(String s)
Writes a series of UTF-8 characters following the specifications of the
DataOutput interface.
|
public BufferedRandomAccessFile(String fileName, String mode) throws IOException
fileName
- a valid path or file name for opening a filemode
- the access mode following the conventions of JavaIOException
- in the event of an I/O errorpublic BufferedRandomAccessFile(File file, String mode) throws IOException
file
- a valid file referencemode
- the access mode following the conventions of JavaIOException
- in the event of an I/O errorpublic void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public boolean isClosed()
public void flush() throws IOException
IOException
- if an I/O error occurs.public long getFileSize()
public long getFilePosition()
public File getFile()
public double leReadDouble() throws IOException
IOException
public float leReadFloat() throws IOException
IOException
public int leReadInt() throws IOException
IOException
public long leReadLong() throws IOException
IOException
public short leReadShort() throws IOException
IOException
public int leReadUnsignedShort() throws IOException
IOException
public void leWriteShort(int v) throws IOException
IOException
public void leWriteInt(int value) throws IOException
IOException
public void leWriteLong(long v) throws IOException
IOException
public void leWriteFloat(float v) throws IOException
IOException
public void leWriteDouble(double v) throws IOException
IOException
public void leReadIntArray(int[] array, int arrayOffset, int length) throws IOException
array
- a valid, non-zero sized arrayarrayOffset
- the starting position within the array.length
- the number of values to read.IOException
- in the event of an I/O error.public void leReadFloatArray(float[] array, int arrayOffset, int length) throws IOException
array
- a valid, non-zero sized arrayarrayOffset
- the starting position within the array.length
- the number of values to read.IOException
- in the event of an I/O error.public void leReadShortArray(short[] array, int arrayOffset, int length) throws IOException
array
- a valid, non-zero sized arrayarrayOffset
- the starting position within the array.length
- the number of values to read.IOException
- in the event of an I/O error.public String leReadUTF() throws IOException
IOException
- in the event of an I/O error.public void leWriteUTF(String s) throws IOException
s
- a valid StringIOException
- in the event of an I/O error.public boolean readBoolean() throws IOException
readBoolean
in interface DataInput
IOException
- if an I/O error occurs.public byte readByte() throws IOException
readByte
in interface DataInput
IOException
- if an I/O error occurs.public char readCharASCII() throws IOException
IOException
- if an I/O error occurs.public void readFully(byte[] array) throws IOException
readFully
in interface DataInput
array
- a valid arrayIOException
- if an I/O error occurs, including an end-of-file
condition.public void readFully(byte[] array, int arrayOffset, int length) throws IOException
readFully
in interface DataInput
array
- a valid arrayarrayOffset
- the starting index in the arraylength
- the number of bytes to readIOException
- if an I/O error occurs, including an end-of-file
condition.public int readUnsignedByte() throws IOException
readUnsignedByte
in interface DataInput
IOException
- if an I/O error occurs.public int readASCII(StringBuilder builder, int nBytesToRead) throws IOException
builder
- a valid StringBuilder instance, not necessarily empty.nBytesToRead
- the number of bytes to read from the fileIOException
- if an I/O error occurs.public String readASCII(int nBytesToRead) throws IOException
nBytesToRead
- the number of bytes to read from the fileIOException
- if an I/O error occurs.public void seek(long position) throws IOException
position
- the file-pointer offset positionIOException
- if the position is less than zero or an I/O error
occurs.public int skipBytes(int n) throws IOException
skipBytes
in interface DataInput
n
- the number of bytes to skipIOException
- if an I/O error occurs.public void writeByte(int value) throws IOException
writeByte
in interface DataOutput
IOException
public void writeFully(byte[] array) throws IOException
IOException
public void writeFully(byte[] array, int arrayOffset, int length) throws IOException
IOException
public void write(int b) throws IOException
write
in interface DataOutput
IOException
public void write(byte[] b) throws IOException
write
in interface DataOutput
b
- a valid array of bytesIOException
- in the event of an unexpected I/O conditionpublic void write(byte[] b, int offset, int length) throws IOException
write
in interface DataOutput
IOException
public void writeBoolean(boolean v) throws IOException
writeBoolean
in interface DataOutput
IOException
public void writeBytes(String s) throws IOException
writeBytes
in interface DataOutput
IOException
public void writeASCII(String s, int nBytes) throws IOException
IOException
public void writeShort(int s) throws IOException
writeShort
in interface DataOutput
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.IOException
- in the event of an I/O errorpublic void writeInt(int value) throws IOException
writeInt
in interface DataOutput
value
- an integer valueIOException
- in the event of an I/O errorpublic void writeUTF(String s) throws IOException
writeUTF
in interface DataOutput
s
- a valid StringIOException
- in the event of an I/O error.public double readDouble() throws IOException
readDouble
in interface DataInput
IOException
- in the event of an I/O errorpublic float readFloat() throws IOException
readFloat
in interface DataInput
IOException
- in the event of an I/O errorpublic int readInt() throws IOException
readInt
in interface DataInput
IOException
- in the event of an I/O errorpublic long readLong() throws IOException
readLong
in interface DataInput
IOException
- in the event of an I/O errorpublic short readShort() throws IOException
readShort
in interface DataInput
IOException
- in the event of an I/O errorpublic int readUnsignedShort() throws IOException
readUnsignedShort
in interface DataInput
IOException
- in the event of an I/O errorpublic String readUTF() throws IOException
readUTF
in interface DataInput
IOException
- in the event of an I/O error.public String readLine() throws IOException
readLine
in interface DataInput
IOException
- in the event of an I/O error.public void printDiagnostics(PrintStream ps)
ps
- a valid PrintStream for writing the output (may be
System.out).public char readChar() throws IOException
readChar
in interface DataInput
IOException
- in the event of an I/O error.public void writeChar(int v) throws IOException
writeChar
in interface DataOutput
v
- a valid char; due to the two-byte limitation, not all UTF
characters are supportedIOException
- in the event of an I/O error.public void writeLong(long v) throws IOException
writeLong
in interface DataOutput
IOException
public void writeFloat(float v) throws IOException
writeFloat
in interface DataOutput
IOException
public void writeDouble(double v) throws IOException
writeDouble
in interface DataOutput
IOException
public void writeChars(String s) throws IOException
writeChars
in interface DataOutput
IOException
Copyright © 2022. All rights reserved.