Package jj2000.j2k.io
Class BufferedRandomAccessFile
- java.lang.Object
-
- jj2000.j2k.io.BufferedRandomAccessFile
-
- All Implemented Interfaces:
BinaryDataInput,BinaryDataOutput,EndianType,RandomAccessIO
- Direct Known Subclasses:
BEBufferedRandomAccessFile
public abstract class BufferedRandomAccessFile extends Object implements RandomAccessIO, EndianType
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
Fields Modifier and Type Field Description protected byte[]byteBufferBuffer of bytes containing the part of the file that is currently being accessedprotected booleanbyteBufferChangedBoolean keeping track of whether the byte buffer has been changed since it was read.protected intbyteOrderingprotected booleanisEOFInBufferWhether the end of the file is in the current buffer or notprotected intmaxByteThe maximum number of bytes that can be read from the bufferprotected intoffsetThe current offset of the buffer (which will differ from the offset of the file)protected intposThe current position in the byte-buffer-
Fields inherited from interface jj2000.j2k.io.EndianType
BIG_ENDIAN, LITTLE_ENDIAN
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBufferedRandomAccessFile(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the buffered random access filevoidflush()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.intgetByteOrdering()Returns 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.intread()Reads an unsigned byte of data from the stream.bytereadByte()Reads a signed byte (i.e., 8 bit) from the input.voidreadFully(byte[] b, int off, int len)Reads up to len bytes of data from this file into an array of bytes.protected voidreadNewBuffer(int off)Reads a new buffer from the file.intreadUnsignedByte()Reads 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.StringtoString()Returns a string of information about the filevoidwrite(byte b)Writes a byte to the stream.voidwrite(byte[] b, int offset, int length)Writes aan array of bytes to the stream.voidwrite(int b)Writes a byte to the stream.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, wait
-
Methods inherited from interface jj2000.j2k.io.BinaryDataInput
readDouble, readFloat, readInt, readLong, readShort, readUnsignedInt, readUnsignedShort
-
Methods inherited from interface jj2000.j2k.io.BinaryDataOutput
writeDouble, writeFloat, writeInt, writeLong, writeShort
-
-
-
-
Field Detail
-
byteBuffer
protected byte[] byteBuffer
Buffer of bytes containing the part of the file that is currently being accessed
-
byteBufferChanged
protected boolean byteBufferChanged
Boolean keeping track of whether the byte buffer has been changed since it was read.
-
offset
protected int offset
The current offset of the buffer (which will differ from the offset of the file)
-
pos
protected int pos
The current position in the byte-buffer
-
maxByte
protected int maxByte
The maximum number of bytes that can be read from the buffer
-
isEOFInBuffer
protected boolean isEOFInBuffer
Whether the end of the file is in the current buffer or not
-
byteOrdering
protected int byteOrdering
-
-
Constructor Detail
-
BufferedRandomAccessFile
protected BufferedRandomAccessFile(File file, String mode, int bufferSize) throws IOException
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
protected BufferedRandomAccessFile(File file, String mode) throws IOException
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
protected BufferedRandomAccessFile(String name, String mode, int bufferSize) throws IOException
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
protected BufferedRandomAccessFile(String name, String mode) throws IOException
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 Detail
-
readNewBuffer
protected final void readNewBuffer(int off) throws IOException
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
public void close() throws IOException
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
public int length() throws IOException
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
public void seek(int off) throws IOException
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
public final int read() throws IOException, EOFException
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
public final void readFully(byte[] b, int off, int len) throws IOException
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
public final void write(int b) throws IOException
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
public final void write(byte b) throws IOException
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
public final void write(byte[] b, int offset, int length) throws IOException
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
public final void writeByte(int v) throws IOException
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
public final void flush() throws IOException
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
public final byte readByte() throws EOFException, IOException
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
public final int readUnsignedByte() throws EOFException, IOException
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:
EndianType
-
skipBytes
public int skipBytes(int n) throws EOFException, IOException
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.
-
-