public abstract class BufferedRandomAccessFile extends java.lang.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 |
protected boolean |
isEOFInBuffer
Whether the end of the file is in the current buffer or not
|
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
|
BIG_ENDIAN, LITTLE_ENDIAN
Modifier | Constructor and Description |
---|---|
protected |
BufferedRandomAccessFile(java.io.File file,
java.lang.String mode)
Constructor.
|
protected |
BufferedRandomAccessFile(java.io.File file,
java.lang.String mode,
int bufferSize)
Constructor.
|
protected |
BufferedRandomAccessFile(java.lang.String name,
java.lang.String mode)
Constructor.
|
protected |
BufferedRandomAccessFile(java.lang.String name,
java.lang.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.
|
java.lang.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
protected byte[] byteBuffer
protected boolean byteBufferChanged
protected int offset
protected int pos
protected int maxByte
protected boolean isEOFInBuffer
protected int byteOrdering
protected BufferedRandomAccessFile(java.io.File file, java.lang.String mode, int bufferSize) throws java.io.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 bufferjava.io.IOException
- If an I/O error ocurred.protected BufferedRandomAccessFile(java.io.File file, java.lang.String mode) throws java.io.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).java.io.IOException
- If an I/O error ocurred.protected BufferedRandomAccessFile(java.lang.String name, java.lang.String mode, int bufferSize) throws java.io.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 bufferjava.io.IOException
- If an I/O error ocurred.protected BufferedRandomAccessFile(java.lang.String name, java.lang.String mode) throws java.io.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).java.io.IOException
- If an I/O error ocurred.protected final void readNewBuffer(int off) throws java.io.IOException
off
- The offset where to move to.java.io.IOException
- If an I/O error ocurred.public void close() throws java.io.IOException
close
in interface RandomAccessIO
java.io.IOException
- If an I/O error ocurred.public int getPos()
getPos
in interface RandomAccessIO
public int length() throws java.io.IOException
length
in interface RandomAccessIO
java.io.IOException
- If an I/O error ocurred.public void seek(int off) throws java.io.IOException
seek
in interface RandomAccessIO
off
- The offset where to move to.java.io.EOFException
- If in read-only and seeking beyond EOF.java.io.IOException
- If an I/O error ocurred.public final int read() throws java.io.IOException, java.io.EOFException
read
in interface RandomAccessIO
java.io.IOException
- If an I/O error ocurred.java.io.EOFException
- If the end of file was reachedpublic final void readFully(byte[] b, int off, int len) throws java.io.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.java.io.EOFException
- If the end-of file was reached before
getting all the necessary data.java.io.IOException
- If an I/O error ocurred.public final void write(int b) throws java.io.IOException
write
in interface RandomAccessIO
b
- The byte to write. The lower 8 bits of b are
written.java.io.IOException
- If an I/O error ocurred.public final void write(byte b) throws java.io.IOException
b
- The byte to write.java.io.IOException
- If an I/O error ocurred.public final void write(byte[] b, int offset, int length) throws java.io.IOException
b
- The array of bytes to write.offset
- The first byte in b to writelength
- The number of bytes from b to writejava.io.IOException
- If an I/O error ocurred.public final void writeByte(int v) throws java.io.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 outputjava.io.IOException
- If an I/O error ocurred.public final void flush() throws java.io.IOException
flush
in interface BinaryDataOutput
java.io.IOException
- If an I/O error ocurred.public final byte readByte() throws java.io.EOFException, java.io.IOException
readByte
in interface BinaryDataInput
java.io.EOFException
- If the end-of file was reached before
getting all the necessary data.java.io.IOException
- If an I/O error ocurred.public final int readUnsignedByte() throws java.io.EOFException, java.io.IOException
readUnsignedByte
in interface BinaryDataInput
java.io.EOFException
- If the end-of file was reached before
getting all the necessary data.java.io.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 java.io.EOFException, java.io.IOException
skipBytes
in interface BinaryDataInput
n
- The number of bytes to skipjava.io.EOFException
- If the end-of file was reached before
all the bytes could be skipped.java.io.IOException
- If an I/O error ocurred.public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2014 Open Microscopy Environment