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.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
    Buffer of bytes containing the part of the file that is currently being accessed
    protected boolean
    Boolean keeping track of whether the byte buffer has been changed since it was read.
    protected int
     
    protected boolean
    Whether the end of the file is in the current buffer or not
    protected int
    The maximum number of bytes that can be read from the buffer
    protected int
    The current offset of the buffer (which will differ from the offset of the file)
    protected int
    The current position in the byte-buffer

    Fields inherited from interface jj2000.j2k.io.EndianType

    BIG_ENDIAN, LITTLE_ENDIAN
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor.
    protected
    BufferedRandomAccessFile(File file, String mode, int bufferSize)
    Constructor.
    protected
    Constructor.
    protected
    BufferedRandomAccessFile(String name, String mode, int bufferSize)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the buffered random access file
    final void
    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
    Returns the endianess (i.e., byte ordering) of the implementing class.
    int
    Returns the current offset in the file
    int
    Returns the current length of the stream, in bytes, taking into account any buffering.
    final int
    Reads an unsigned byte of data from the stream.
    final byte
    Reads a signed byte (i.e., 8 bit) from the input.
    final void
    readFully(byte[] b, int off, int len)
    Reads up to len bytes of data from this file into an array of bytes.
    protected final void
    readNewBuffer(int off)
    Reads a new buffer from the file.
    final int
    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.
    Returns a string of information about the file
    final void
    write(byte b)
    Writes a byte to the stream.
    final void
    write(byte[] b, int offset, int length)
    Writes aan array of bytes to the stream.
    final void
    write(int b)
    Writes a byte to the stream.
    final 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 Details

    • 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 Details

    • 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 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:
      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 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:
      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 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:
      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 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:
      IOException - If an I/O error ocurred.
  • Method Details

    • 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:
      close in interface RandomAccessIO
      Throws:
      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 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:
      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:
      seek in interface RandomAccessIO
      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:
      read in interface RandomAccessIO
      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:
      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:
      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:
      write in interface RandomAccessIO
      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 write
      length - 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:
      writeByte in interface BinaryDataOutput
      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:
      flush in interface BinaryDataOutput
      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:
      readByte in interface BinaryDataInput
      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:
      readUnsignedByte in interface BinaryDataInput
      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:
      getByteOrdering in interface BinaryDataInput
      Specified by:
      getByteOrdering in interface BinaryDataOutput
      Returns:
      Either EndianType.BIG_ENDIAN or EndianType.LITTLE_ENDIAN
      See Also:
    • 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:
      skipBytes in interface BinaryDataInput
      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

      public String toString()
      Returns a string of information about the file
      Overrides:
      toString in class Object