Package jj2000.j2k.io
Class BufferedRandomAccessFile
java.lang.Object
jj2000.j2k.io.BufferedRandomAccessFile
- All Implemented Interfaces:
BinaryDataInput,BinaryDataOutput,EndianType,RandomAccessIO
- Direct Known Subclasses:
BEBufferedRandomAccessFile
This class defines a Buffered Random Access File. It implements the
BinaryDataInput and BinaryDataOutput interfaces so that
binary data input/output can be performed. This class is abstract since no
assumption is done about the byte ordering type (little Endian, big
Endian). So subclasses will have to implement methods like
readShort(), writeShort(), readFloat(), ...
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]Buffer of bytes containing the part of the file that is currently being accessedprotected booleanBoolean keeping track of whether the byte buffer has been changed since it was read.protected intprotected booleanWhether the end of the file is in the current buffer or notprotected intThe maximum number of bytes that can be read from the bufferprotected intThe current offset of the buffer (which will differ from the offset of the file)protected intThe current position in the byte-bufferFields inherited from interface jj2000.j2k.io.EndianType
BIG_ENDIAN, LITTLE_ENDIAN -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBufferedRandomAccessFile(File file, String mode) Constructor.protectedBufferedRandomAccessFile(File file, String mode, int bufferSize) Constructor.protectedBufferedRandomAccessFile(String name, String mode) Constructor.protectedBufferedRandomAccessFile(String name, String mode, int bufferSize) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the buffered random access filefinal voidflush()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.intReturns the endianess (i.e., byte ordering) of the implementing class.intgetPos()Returns the current offset in the fileintlength()Returns the current length of the stream, in bytes, taking into account any buffering.final intread()Reads an unsigned byte of data from the stream.final bytereadByte()Reads a signed byte (i.e., 8 bit) from the input.final voidreadFully(byte[] b, int off, int len) Reads up to len bytes of data from this file into an array of bytes.protected final voidreadNewBuffer(int off) Reads a new buffer from the file.final intReads an unsigned byte (i.e., 8 bit) from the input.voidseek(int off) Moves the current position to the given offset at which the next read or write occurs.intskipBytes(int n) Skips n bytes from the input.toString()Returns a string of information about the filefinal voidwrite(byte b) Writes a byte to the stream.final voidwrite(byte[] b, int offset, int length) Writes aan array of bytes to the stream.final voidwrite(int b) Writes a byte to the stream.final voidwriteByte(int v) Writes the byte value of v (i.e., 8 least significant bits) to the output.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface jj2000.j2k.io.BinaryDataInput
readDouble, readFloat, readInt, readLong, readShort, readUnsignedInt, readUnsignedShortMethods inherited from interface jj2000.j2k.io.BinaryDataOutput
writeDouble, writeFloat, writeInt, writeLong, writeShort
-
Field Details
-
byteBuffer
protected byte[] byteBufferBuffer of bytes containing the part of the file that is currently being accessed -
byteBufferChanged
protected boolean byteBufferChangedBoolean keeping track of whether the byte buffer has been changed since it was read. -
offset
protected int offsetThe current offset of the buffer (which will differ from the offset of the file) -
pos
protected int posThe current position in the byte-buffer -
maxByte
protected int maxByteThe maximum number of bytes that can be read from the buffer -
isEOFInBuffer
protected boolean isEOFInBufferWhether the end of the file is in the current buffer or not -
byteOrdering
protected int byteOrdering
-
-
Constructor Details
-
BufferedRandomAccessFile
Constructor. Always needs a size for the buffer.- Parameters:
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 buffer- Throws:
IOException- If an I/O error ocurred.
-
BufferedRandomAccessFile
Constructor. Uses the default value for the byte-buffer size (512 bytes).- Parameters:
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).- Throws:
IOException- If an I/O error ocurred.
-
BufferedRandomAccessFile
Constructor. Always needs a size for the buffer.- Parameters:
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 buffer- Throws:
IOException- If an I/O error ocurred.
-
BufferedRandomAccessFile
Constructor. Uses the default value for the byte-buffer size (512 bytes).- Parameters:
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).- Throws:
IOException- If an I/O error ocurred.
-
-
Method Details
-
readNewBuffer
Reads a new buffer from the file. If there has been any changes made since the buffer was read, the buffer is first written to the file.- Parameters:
off- The offset where to move to.- Throws:
IOException- If an I/O error ocurred.
-
close
Closes the buffered random access file- Specified by:
closein interfaceRandomAccessIO- Throws:
IOException- If an I/O error ocurred.
-
getPos
public int getPos()Returns the current offset in the file- Specified by:
getPosin interfaceRandomAccessIO- Returns:
- The offset of the current position, in bytes.
-
length
Returns the current length of the stream, in bytes, taking into account any buffering.- Specified by:
lengthin interfaceRandomAccessIO- Returns:
- The length of the stream, in bytes.
- Throws:
IOException- If an I/O error ocurred.
-
seek
Moves the current position to the given offset at which the next read or write occurs. The offset is measured from the beginning of the stream.- Specified by:
seekin interfaceRandomAccessIO- Parameters:
off- The offset where to move to.- Throws:
EOFException- If in read-only and seeking beyond EOF.IOException- If an I/O error ocurred.
-
read
Reads an unsigned byte of data from the stream. Prior to reading, the stream is realigned at the byte level.- Specified by:
readin interfaceRandomAccessIO- Returns:
- The byte read.
- Throws:
IOException- If an I/O error ocurred.EOFException- If the end of file was reached
-
readFully
Reads up to len bytes of data from this file into an array of bytes. This method reads repeatedly from the stream until all the bytes are read. This method blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown.- Specified by:
readFullyin interfaceRandomAccessIO- Parameters:
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.- Throws:
EOFException- If the end-of file was reached before getting all the necessary data.IOException- If an I/O error ocurred.
-
write
Writes a byte to the stream. Prior to writing, the stream is realigned at the byte level.- Specified by:
writein interfaceRandomAccessIO- Parameters:
b- The byte to write. The lower 8 bits of b are written.- Throws:
IOException- If an I/O error ocurred.
-
write
Writes a byte to the stream. Prior to writing, the stream is realigned at the byte level.- Parameters:
b- The byte to write.- Throws:
IOException- If an I/O error ocurred.
-
write
Writes aan array of bytes to the stream. Prior to writing, the stream is realigned at the byte level.- Parameters:
b- The array of bytes to write.offset- The first byte in b to writelength- The number of bytes from b to write- Throws:
IOException- If an I/O error ocurred.
-
writeByte
Writes the byte value of v (i.e., 8 least significant bits) to the output. Prior to writing, the output should be realigned at the byte level.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).
- Specified by:
writeBytein interfaceBinaryDataOutput- Parameters:
v- The value to write to the output- Throws:
IOException- If an I/O error ocurred.
-
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.- Specified by:
flushin interfaceBinaryDataOutput- Throws:
IOException- If an I/O error ocurred.
-
readByte
Reads a signed byte (i.e., 8 bit) from the input. Prior to reading, the input should be realigned at the byte level.- Specified by:
readBytein interfaceBinaryDataInput- Returns:
- The next byte-aligned signed byte (8 bit) from the input.
- Throws:
EOFException- If the end-of file was reached before getting all the necessary data.IOException- If an I/O error ocurred.
-
readUnsignedByte
Reads an unsigned byte (i.e., 8 bit) from the input. It is returned as an int since Java does not have an unsigned byte type. Prior to reading, the input should be realigned at the byte level.- Specified by:
readUnsignedBytein interfaceBinaryDataInput- Returns:
- The next byte-aligned unsigned byte (8 bit) from the input, as an int.
- Throws:
EOFException- If the end-of file was reached before getting all the necessary data.IOException- If an I/O error ocurred.
-
getByteOrdering
public int getByteOrdering()Returns the endianess (i.e., byte ordering) of the implementing class. Note that an implementing class may implement only one type of endianness or both, which would be decided at creation time.- Specified by:
getByteOrderingin interfaceBinaryDataInput- Specified by:
getByteOrderingin interfaceBinaryDataOutput- Returns:
- Either EndianType.BIG_ENDIAN or EndianType.LITTLE_ENDIAN
- See Also:
-
skipBytes
Skips n bytes from the input. Prior to skipping, the input should be realigned at the byte level.- Specified by:
skipBytesin interfaceBinaryDataInput- Parameters:
n- The number of bytes to skip- Throws:
EOFException- If the end-of file was reached before all the bytes could be skipped.IOException- If an I/O error ocurred.
-
toString
Returns a string of information about the file
-