public abstract class BufferedRandomAccessFile extends Object implements RandomAccessIO, EndianType
BufferedRandomAccessFile (BRAF for short) is a RandomAccessFile containing an extra buffer. When the BRAF is accessed, it checks if the requested part of the file is in the buffer or not. If that is the case, the read/write is done on the buffer. If not, the file is uppdated to reflect the current status of the buffer and the file is then accessed for a new buffer containing the requested byte/bit.
Modifier and Type | Field and Description |
---|---|
protected byte[] |
byteBuffer
Buffer of bytes containing the part of the file that is currently being
accessed
|
protected boolean |
byteBufferChanged
Boolean keeping track of whether the byte buffer has been changed since
it was read.
|
protected int |
byteOrdering |
private String |
fileName
The name of the current file
|
protected boolean |
isEOFInBuffer
Whether the end of the file is in the current buffer or not
|
private boolean |
isReadOnly
Whether the opened file is read only or not (defined by the constructor
arguments)
|
protected int |
maxByte
The maximum number of bytes that can be read from the buffer
|
protected int |
offset
The current offset of the buffer (which will differ from the offset of
the file)
|
protected int |
pos
The current position in the byte-buffer
|
private RandomAccessFile |
theFile
The RandomAccessFile associated with the buffer
|
BIG_ENDIAN, LITTLE_ENDIAN
Modifier | Constructor and Description |
---|---|
protected |
BufferedRandomAccessFile(File file,
String mode)
Constructor.
|
protected |
BufferedRandomAccessFile(File file,
String mode,
int bufferSize)
Constructor.
|
protected |
BufferedRandomAccessFile(String name,
String mode)
Constructor.
|
protected |
BufferedRandomAccessFile(String name,
String mode,
int bufferSize)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the buffered random access file
|
void |
flush()
Any data that has been buffered must be written (including
buffering at the bit level), and the stream should be realigned
at the byte level.
|
int |
getByteOrdering()
Returns the endianess (i.e., byte ordering) of the implementing
class.
|
int |
getPos()
Returns the current offset in the file
|
int |
length()
Returns the current length of the stream, in bytes, taking into
account any buffering.
|
int |
read()
Reads an unsigned byte of data from the stream.
|
byte |
readByte()
Reads a signed byte (i.e., 8 bit) from the input.
|
void |
readFully(byte[] b,
int off,
int len)
Reads up to len bytes of data from this file into an array of
bytes.
|
protected void |
readNewBuffer(int off)
Reads a new buffer from the file.
|
int |
readUnsignedByte()
Reads an unsigned byte (i.e., 8 bit) from the input.
|
void |
seek(int off)
Moves the current position to the given offset at which the
next read or write occurs.
|
int |
skipBytes(int n)
Skips n bytes from the input.
|
String |
toString()
Returns a string of information about the file
|
void |
write(byte b)
Writes a byte to the stream.
|
void |
write(byte[] b,
int offset,
int length)
Writes aan array of bytes to the stream.
|
void |
write(int b)
Writes a byte to the stream.
|
void |
writeByte(int v)
Writes the byte value of v (i.e., 8 least
significant bits) to the output.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
readDouble, readFloat, readInt, readLong, readShort, readUnsignedInt, readUnsignedShort
writeDouble, writeFloat, writeInt, writeLong, writeShort
private String fileName
private boolean isReadOnly
private RandomAccessFile theFile
protected byte[] byteBuffer
protected boolean byteBufferChanged
protected int offset
protected int pos
protected int maxByte
protected boolean isEOFInBuffer
protected int byteOrdering
protected BufferedRandomAccessFile(File file, String mode, int bufferSize) throws IOException
file
- The file associated with the buffermode
- "r" for read, "rw" or "rw+" for read and write mode ("rw+"
opens the file for update whereas "rw" removes it
before. So the 2 modes are different only if the file
already exists).bufferSize
- The number of bytes to bufferIOException
- If an I/O error ocurred.protected BufferedRandomAccessFile(File file, String mode) throws IOException
file
- The file associated with the buffermode
- "r" for read, "rw" or "rw+" for read and write mode
("rw+" opens the file for update whereas "rw" removes
it before. So the 2 modes are different only if the
file already exists).IOException
- If an I/O error ocurred.protected BufferedRandomAccessFile(String name, String mode, int bufferSize) throws IOException
name
- The name of the file associated with the buffermode
- "r" for read, "rw" or "rw+" for read and write mode
("rw+" opens the file for update whereas "rw" removes
it before. So the 2 modes are different only if the
file already exists).bufferSize
- The number of bytes to bufferIOException
- If an I/O error ocurred.protected BufferedRandomAccessFile(String name, String mode) throws IOException
name
- The name of the file associated with the buffermode
- "r" for read, "rw" or "rw+" for read and write mode
("rw+" opens the file for update whereas "rw" removes
it before. So the 2 modes are different only if the
file already exists).IOException
- If an I/O error ocurred.protected final void readNewBuffer(int off) throws IOException
off
- The offset where to move to.IOException
- If an I/O error ocurred.public void close() throws IOException
close
in interface RandomAccessIO
IOException
- If an I/O error ocurred.public int getPos()
getPos
in interface RandomAccessIO
public int length() throws IOException
length
in interface RandomAccessIO
IOException
- If an I/O error ocurred.public void seek(int off) throws IOException
seek
in interface RandomAccessIO
off
- The offset where to move to.EOFException
- If in read-only and seeking beyond EOF.IOException
- If an I/O error ocurred.public final int read() throws IOException, EOFException
read
in interface RandomAccessIO
IOException
- If an I/O error ocurred.EOFException
- If the end of file was reachedpublic final void readFully(byte[] b, int off, int len) throws IOException
readFully
in interface RandomAccessIO
b
- The buffer into which the data is to be read. It must be long
enough.off
- The index in 'b' where to place the first byte read.len
- The number of bytes to read.EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public final void write(int b) throws IOException
write
in interface RandomAccessIO
b
- The byte to write. The lower 8 bits of b are
written.IOException
- If an I/O error ocurred.public final void write(byte b) throws IOException
b
- The byte to write.IOException
- If an I/O error ocurred.public final void write(byte[] b, int offset, int length) throws IOException
b
- The array of bytes to write.offset
- The first byte in b to writelength
- The number of bytes from b to writeIOException
- If an I/O error ocurred.public final void writeByte(int v) throws IOException
Signed or unsigned data can be written. To write a signed value just pass the byte value as an argument. To write unsigned data pass the int value as an argument (it will be automatically casted, and only the 8 least significant bits will be written).
writeByte
in interface BinaryDataOutput
v
- The value to write to the outputIOException
- If an I/O error ocurred.public final void flush() throws IOException
flush
in interface BinaryDataOutput
IOException
- If an I/O error ocurred.public final byte readByte() throws EOFException, IOException
readByte
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public final int readUnsignedByte() throws EOFException, IOException
readUnsignedByte
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public int getByteOrdering()
getByteOrdering
in interface BinaryDataInput
getByteOrdering
in interface BinaryDataOutput
EndianType
public int skipBytes(int n) throws EOFException, IOException
skipBytes
in interface BinaryDataInput
n
- The number of bytes to skipEOFException
- If the end-of file was reached before
all the bytes could be skipped.IOException
- If an I/O error ocurred.Copyright © 2017 Open Microscopy Environment