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 java.lang.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.

See Also:
RandomAccessIO, BinaryDataOutput, BinaryDataInput, BEBufferedRandomAccessFile

Field Summary
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  java.lang.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  java.io.RandomAccessFile theFile
          The RandomAccessFile associated with the buffer
 
Fields inherited from interface jj2000.j2k.io.EndianType
BIG_ENDIAN, LITTLE_ENDIAN
 
Constructor Summary
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.
 
Method Summary
 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.
 
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

fileName

private java.lang.String fileName
The name of the current file


isReadOnly

private boolean isReadOnly
Whether the opened file is read only or not (defined by the constructor arguments)


theFile

private java.io.RandomAccessFile theFile
The RandomAccessFile associated with the buffer


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(java.io.File file,
                                   java.lang.String mode,
                                   int bufferSize)
                            throws java.io.IOException
Constructor. Always needs a size for the buffer.

Parameters:
file - The file associated with the buffer
mode - "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:
java.io.IOException - If an I/O error ocurred.

BufferedRandomAccessFile

protected BufferedRandomAccessFile(java.io.File file,
                                   java.lang.String mode)
                            throws java.io.IOException
Constructor. Uses the default value for the byte-buffer size (512 bytes).

Parameters:
file - The file associated with the buffer
mode - "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:
java.io.IOException - If an I/O error ocurred.

BufferedRandomAccessFile

protected BufferedRandomAccessFile(java.lang.String name,
                                   java.lang.String mode,
                                   int bufferSize)
                            throws java.io.IOException
Constructor. Always needs a size for the buffer.

Parameters:
name - The name of the file associated with the buffer
mode - "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:
java.io.IOException - If an I/O error ocurred.

BufferedRandomAccessFile

protected BufferedRandomAccessFile(java.lang.String name,
                                   java.lang.String mode)
                            throws java.io.IOException
Constructor. Uses the default value for the byte-buffer size (512 bytes).

Parameters:
name - The name of the file associated with the buffer
mode - "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:
java.io.IOException - If an I/O error ocurred.
Method Detail

readNewBuffer

protected final void readNewBuffer(int off)
                            throws java.io.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:
java.io.IOException - If an I/O error ocurred.

close

public void close()
           throws java.io.IOException
Closes the buffered random access file

Specified by:
close in interface RandomAccessIO
Throws:
java.io.IOException - If an I/O error ocurred.

getPos

public int getPos()
Returns the current offset in the file

Specified by:
getPos in interface RandomAccessIO
Returns:
The offset of the current position, in bytes.

length

public int length()
           throws java.io.IOException
Returns the current length of the stream, in bytes, taking into account any buffering.

Specified by:
length in interface RandomAccessIO
Returns:
The length of the stream, in bytes.
Throws:
java.io.IOException - If an I/O error ocurred.

seek

public void seek(int off)
          throws java.io.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:
seek in interface RandomAccessIO
Parameters:
off - The offset where to move to.
Throws:
java.io.EOFException - If in read-only and seeking beyond EOF.
java.io.IOException - If an I/O error ocurred.

read

public final int read()
               throws java.io.IOException,
                      java.io.EOFException
Reads an unsigned byte of data from the stream. Prior to reading, the stream is realigned at the byte level.

Specified by:
read in interface RandomAccessIO
Returns:
The byte read.
Throws:
java.io.IOException - If an I/O error ocurred.
java.io.EOFException - If the end of file was reached

readFully

public final void readFully(byte[] b,
                            int off,
                            int len)
                     throws java.io.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:
readFully in interface RandomAccessIO
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:
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.

write

public final void write(int b)
                 throws java.io.IOException
Writes a byte to the stream. Prior to writing, the stream is realigned at the byte level.

Specified by:
write in interface RandomAccessIO
Parameters:
b - The byte to write. The lower 8 bits of b are written.
Throws:
java.io.IOException - If an I/O error ocurred.

write

public final void write(byte b)
                 throws java.io.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:
java.io.IOException - If an I/O error ocurred.

write

public final void write(byte[] b,
                        int offset,
                        int length)
                 throws java.io.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 write
length - The number of bytes from b to write
Throws:
java.io.IOException - If an I/O error ocurred.

writeByte

public final void writeByte(int v)
                     throws java.io.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:
writeByte in interface BinaryDataOutput
Parameters:
v - The value to write to the output
Throws:
java.io.IOException - If an I/O error ocurred.

flush

public final void flush()
                 throws java.io.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:
flush in interface BinaryDataOutput
Throws:
java.io.IOException - If an I/O error ocurred.

readByte

public final byte readByte()
                    throws java.io.EOFException,
                           java.io.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:
readByte in interface BinaryDataInput
Returns:
The next byte-aligned signed byte (8 bit) from the input.
Throws:
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.

readUnsignedByte

public final int readUnsignedByte()
                           throws java.io.EOFException,
                                  java.io.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:
readUnsignedByte in interface BinaryDataInput
Returns:
The next byte-aligned unsigned byte (8 bit) from the input, as an int.
Throws:
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.

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:
getByteOrdering in interface BinaryDataInput
Returns:
Either EndianType.BIG_ENDIAN or EndianType.LITTLE_ENDIAN
See Also:
EndianType

skipBytes

public int skipBytes(int n)
              throws java.io.EOFException,
                     java.io.IOException
Skips n bytes from the input. Prior to skipping, the input should be realigned at the byte level.

Specified by:
skipBytes in interface BinaryDataInput
Parameters:
n - The number of bytes to skip
Throws:
java.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.

toString

public java.lang.String toString()
Returns a string of information about the file

Overrides:
toString in class java.lang.Object