Package jj2000.j2k.io

Class 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:
    RandomAccessIO, BinaryDataOutput, BinaryDataInput, BEBufferedRandomAccessFile
    • 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 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 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.
      • 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:
        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:
        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