C# Class CSJ2K.j2k.io.BufferedRandomAccessFile

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.

Inheritance: RandomAccessIO, EndianType
Show file Open project: cureos/csj2k Class Usage Examples

Protected Properties

Property Type Description
byteBuffer byte[]
byteBufferChanged bool
byte_Ordering int
isEOFInBuffer bool
maxByte int
offset int
position int

Public Methods

Method Description
ToString ( ) : System.String

Returns a string of information about the file

close ( ) : void

Closes the buffered random access file

flush ( ) : 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.

length ( ) : int

Returns the current length of the stream, in bytes, taking into account any buffering.

read ( ) : byte
readByte ( ) : byte

Reads an unsigned byte of data from the stream. Prior to reading, the stream is realigned at the byte level.

readDouble ( ) : double
readFloat ( ) : float
readFully ( byte b, int off, int len ) : void

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.

readInt ( ) : int
readLong ( ) : long
readShort ( ) : short
readUnsignedByte ( ) : byte
readUnsignedInt ( ) : long
readUnsignedShort ( ) : int
seek ( int off ) : void

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.

skipBytes ( int n ) : int

Skips n bytes from the input. Prior to skipping, the input should be realigned at the byte level.

write ( byte b ) : void

Writes a byte to the stream. Prior to writing, the stream is realigned at the byte level.

write ( byte b, int offset, int length ) : void

Writes aan array of bytes to the stream. Prior to writing, the stream is realigned at the byte level.

write ( int b ) : void

Writes a byte to the stream. Prior to writing, the stream is realigned at the byte level.

writeByte ( int v ) : void

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).

writeDouble ( double param1 ) : void
writeFloat ( float param1 ) : void
writeInt ( int param1 ) : void
writeLong ( long param1 ) : void
writeShort ( int param1 ) : void

Protected Methods

Method Description
BufferedRandomAccessFile ( IFileInfo file, System mode ) : System

Constructor. Uses the default value for the byte-buffer size (512 bytes).

BufferedRandomAccessFile ( IFileInfo file, System mode, int bufferSize ) : System

Constructor. Always needs a size for the buffer.

BufferedRandomAccessFile ( System.Stream stream, bool isReadOnly ) : System

Constructor. Uses the default value for the byte-buffer size (512 bytes).

BufferedRandomAccessFile ( System.Stream stream, bool isReadOnly, int bufferSize ) : System

Constructor. Always needs a size for the buffer.

BufferedRandomAccessFile ( System name, System mode ) : System

Constructor. Uses the default value for the byte-buffer size (512 bytes).

BufferedRandomAccessFile ( System name, System mode, int bufferSize ) : System

Constructor. Always needs a size for the buffer.

readNewBuffer ( int off ) : void

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.

Method Details

BufferedRandomAccessFile() protected method

Constructor. Uses the default value for the byte-buffer size (512 bytes).
If an I/O error ocurred. /// ///
protected BufferedRandomAccessFile ( IFileInfo file, System mode ) : System
file IFileInfo The file associated with the buffer /// ///
mode System "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). /// ///
return System

BufferedRandomAccessFile() protected method

Constructor. Always needs a size for the buffer.
If an I/O error ocurred. /// ///
protected BufferedRandomAccessFile ( IFileInfo file, System mode, int bufferSize ) : System
file IFileInfo The file associated with the buffer /// ///
mode System "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 int The number of bytes to buffer /// ///
return System

BufferedRandomAccessFile() protected method

Constructor. Uses the default value for the byte-buffer size (512 bytes).
If an I/O error ocurred. /// ///
protected BufferedRandomAccessFile ( System.Stream stream, bool isReadOnly ) : System
stream System.Stream The stream associated with the buffer /// ///
isReadOnly bool Indicates whether file is read-only or not.
return System

BufferedRandomAccessFile() protected method

Constructor. Always needs a size for the buffer.
If an I/O error ocurred. /// ///
protected BufferedRandomAccessFile ( System.Stream stream, bool isReadOnly, int bufferSize ) : System
stream System.Stream The stream associated with the buffer /// ///
isReadOnly bool Indicates whether file is read-only or not.
bufferSize int The number of bytes to buffer /// ///
return System

BufferedRandomAccessFile() protected method

Constructor. Uses the default value for the byte-buffer size (512 bytes).
If an I/O error ocurred. /// ///
protected BufferedRandomAccessFile ( System name, System mode ) : System
name System The name of the file associated with the buffer /// ///
mode System "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). /// ///
return System

BufferedRandomAccessFile() protected method

Constructor. Always needs a size for the buffer.
If an I/O error ocurred. /// ///
protected BufferedRandomAccessFile ( System name, System mode, int bufferSize ) : System
name System The name of the file associated with the buffer /// ///
mode System "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 int The number of bytes to buffer /// ///
return System

ToString() public method

Returns a string of information about the file
public ToString ( ) : System.String
return System.String

close() public method

Closes the buffered random access file
If an I/O error ocurred. /// ///
public close ( ) : void
return void

flush() public method

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.
If an I/O error ocurred. /// ///
public flush ( ) : void
return void

length() public method

Returns the current length of the stream, in bytes, taking into account any buffering.
If an I/O error ocurred. /// ///
public length ( ) : int
return int

read() public method

public read ( ) : byte
return byte

readByte() public method

Reads an unsigned byte of data from the stream. Prior to reading, the stream is realigned at the byte level.
If an I/O error ocurred. /// /// If the end of file was reached /// ///
public readByte ( ) : byte
return byte

readDouble() public abstract method

public abstract readDouble ( ) : double
return double

readFloat() public abstract method

public abstract readFloat ( ) : float
return float

readFully() public method

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.
If the end-of file was reached before /// getting all the necessary data. /// /// If an I/O error ocurred. /// ///
public readFully ( byte b, int off, int len ) : void
b byte The buffer into which the data is to be read. It must be long /// enough. /// ///
off int The index in 'b' where to place the first byte read. /// ///
len int The number of bytes to read. /// ///
return void

readInt() public abstract method

public abstract readInt ( ) : int
return int

readLong() public abstract method

public abstract readLong ( ) : long
return long

readNewBuffer() protected method

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.
If an I/O error ocurred. /// ///
protected readNewBuffer ( int off ) : void
off int The offset where to move to. /// ///
return void

readShort() public abstract method

public abstract readShort ( ) : short
return short

readUnsignedByte() public method

public readUnsignedByte ( ) : byte
return byte

readUnsignedInt() public abstract method

public abstract readUnsignedInt ( ) : long
return long

readUnsignedShort() public abstract method

public abstract readUnsignedShort ( ) : int
return int

seek() public method

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.
If in read-only and seeking beyond EOF. /// /// If an I/O error ocurred. /// ///
public seek ( int off ) : void
off int The offset where to move to. /// ///
return void

skipBytes() public method

Skips n bytes from the input. Prior to skipping, the input should be realigned at the byte level.
If the end-of file was reached before /// all the bytes could be skipped. /// /// If an I/O error ocurred. /// ///
public skipBytes ( int n ) : int
n int The number of bytes to skip /// ///
return int

write() public method

Writes a byte to the stream. Prior to writing, the stream is realigned at the byte level.
If an I/O error ocurred. /// ///
public write ( byte b ) : void
b byte The byte to write. /// ///
return void

write() public method

Writes aan array of bytes to the stream. Prior to writing, the stream is realigned at the byte level.
If an I/O error ocurred. /// ///
public write ( byte b, int offset, int length ) : void
b byte The array of bytes to write. /// ///
offset int The first byte in b to write /// ///
length int The number of bytes from b to write /// ///
return void

write() public method

Writes a byte to the stream. Prior to writing, the stream is realigned at the byte level.
If an I/O error ocurred. /// ///
public write ( int b ) : void
b int The byte to write. The lower 8 bits of b are /// written. /// ///
return void

writeByte() public method

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).

If an I/O error ocurred. /// ///
public writeByte ( int v ) : void
v int The value to write to the output /// ///
return void

writeDouble() public abstract method

public abstract writeDouble ( double param1 ) : void
param1 double
return void

writeFloat() public abstract method

public abstract writeFloat ( float param1 ) : void
param1 float
return void

writeInt() public abstract method

public abstract writeInt ( int param1 ) : void
param1 int
return void

writeLong() public abstract method

public abstract writeLong ( long param1 ) : void
param1 long
return void

writeShort() public abstract method

public abstract writeShort ( int param1 ) : void
param1 int
return void

Property Details

byteBuffer protected property

Buffer of bytes containing the part of the file that is currently being accessed
protected byte[] byteBuffer
return byte[]

byteBufferChanged protected property

Boolean keeping track of whether the byte buffer has been changed since it was read.
protected bool byteBufferChanged
return bool

byte_Ordering protected property

protected int byte_Ordering
return int

isEOFInBuffer protected property

Whether the end of the file is in the current buffer or not
protected bool isEOFInBuffer
return bool

maxByte protected property

The maximum number of bytes that can be read from the buffer
protected int maxByte
return int

offset protected property

The current offset of the buffer (which will differ from the offset of the file)
protected int offset
return int

position protected property

The current position in the byte-buffer
protected int position
return int