C# Class CSJ2K.j2k.util.ISRandomAccessIO

This class implements a wrapper to turn an InputStream into a RandomAccessIO. To provide random access, the input data from the InputStream is cached in an in-memory buffer. The in-memory buffer size can be limited to a specified size. The data is read into the cache on a as needed basis, blocking only when necessary.

The cache grows automatically as necessary. However, if the data length is known prior to the creation of a ISRandomAccessIO object, it is best to specify that as the initial in-memory buffer size. That will minimize data copying and multiple allocation.

Multi-byte data is read in big-endian order. The in-memory buffer storage is released when 'close()' is called. This class can only be used for data input, not output. The wrapped InputStream is closed when all the input data is cached or when 'close()' is called.

If an out of memory condition is encountered when growing the in-memory buffer an IOException is thrown instead of an OutOfMemoryError. The exception message is "Out of memory to cache input data".

This class is intended for use as a "quick and dirty" way to give network connectivity to RandomAccessIO based classes. It is not intended as an efficient means of implementing network connectivity. Doing such requires reimplementing the RandomAccessIO based classes to directly use network connections.

This class does not use temporary files as buffers, because that would preclude the use in unsigned applets.

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

Public Methods

Method Description
ISRandomAccessIO ( System is_Renamed ) : System

Creates a new RandomAccessIO wrapper for the given InputStream 'is'. The internal cache buffer size and increment is to to 256 kB. The maximum buffer size is set to Integer.MAX_VALUE (2 GB).

ISRandomAccessIO ( System is_Renamed, int size, int inc, int maxsize ) : System

Creates a new RandomAccessIO wrapper for the given InputStream 'is'. The internal cache buffer will have size 'size' and will increment by 'inc' each time it is needed. The maximum buffer size is limited to 'maxsize'.

close ( ) : void

Closes this object for reading as well as the wrapped InputStream, if not already closed. The memory used by the cache is released.

flush ( ) : void

Does nothing since this class does not implement data output.

length ( ) : int

Returns the length of the stream. This will cause all the data to be read. This method will block until all the data is read, which can be lengthy across the network.

read ( ) : byte
readByte ( ) : byte

Reads a byte of data from the stream.

readDouble ( ) : double

Reads an IEEE double precision (i.e., 64 bit) floating-point number from the input.

readFloat ( ) : float

Reads an IEEE single precision (i.e., 32 bit) floating-point number from the input.

readFully ( byte b, int off, int n ) : void

Reads '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

Reads a signed int (32 bit) from the input.

readLong ( ) : long

Reads a signed long (64 bit) from the input.

readShort ( ) : short

Reads a signed short (16 bit) from the input.

readUnsignedByte ( ) : byte

Reads an unsigned byte (8 bit) from the input.

readUnsignedInt ( ) : long

Reads a unsigned int (32 bit) from the input.

readUnsignedShort ( ) : int

Reads an unsigned short (16 bit) from the input.

seek ( int off ) : void

Moves the current position for the next read operation to offset. The offset is measured from the beginning of the stream. If the offset is set beyond the currently cached data, the missing data will be read only when a read operation is performed. Setting the offset beyond the end of the data will cause an EOFException only if the data length is currently known, otherwise an IOException will occur when a read operation is attempted at that position.

skipBytes ( int n ) : int

Skips 'n' bytes from the input.

write ( byte b ) : void

Throws an IOException since this class does not implement data output.

writeByte ( int v ) : void

Throws an IOException since this class does not implement data output.

writeDouble ( double v ) : void

Throws an IOException since this class does not implement data output.

writeFloat ( float v ) : void

Throws an IOException since this class does not implement data output.

writeInt ( int v ) : void

Throws an IOException since this class does not implement data output.

writeLong ( long v ) : void

Throws an IOException since this class does not implement data output.

writeShort ( int v ) : void

Throws an IOException since this class does not implement data output.

Private Methods

Method Description
growBuffer ( ) : void

Grows the cache buffer by 'inc', upto a maximum of 'maxsize'. The buffer size will be increased by at least one byte, if no exception is thrown.

readInput ( ) : void

Reads data from the wrapped InputStream and places it in the cache buffer. Reads all input data that will not cause it to block, but at least on byte is read (even if it blocks), unless EOF is reached. This method can not be called if EOF has been already reached (i.e. 'complete' is true). The wrapped InputStream is closed if the EOF is reached.

Method Details

ISRandomAccessIO() public method

Creates a new RandomAccessIO wrapper for the given InputStream 'is'. The internal cache buffer size and increment is to to 256 kB. The maximum buffer size is set to Integer.MAX_VALUE (2 GB).
public ISRandomAccessIO ( System is_Renamed ) : System
is_Renamed System
return System

ISRandomAccessIO() public method

Creates a new RandomAccessIO wrapper for the given InputStream 'is'. The internal cache buffer will have size 'size' and will increment by 'inc' each time it is needed. The maximum buffer size is limited to 'maxsize'.
public ISRandomAccessIO ( System is_Renamed, int size, int inc, int maxsize ) : System
is_Renamed System
size int The initial size for the cache buffer, in bytes. /// ///
inc int The size increment for the cache buffer, in bytes. /// ///
maxsize int The maximum size for the cache buffer, in bytes. /// ///
return System

close() public method

Closes this object for reading as well as the wrapped InputStream, if not already closed. The memory used by the cache is released.
If an I/O error occurs while closing the /// underlying InputStream. /// ///
public close ( ) : void
return void

flush() public method

Does nothing since this class does not implement data output.
public flush ( ) : void
return void

length() public method

Returns the length of the stream. This will cause all the data to be read. This method will block until all the data is read, which can be lengthy across the network.
If an I/O error ocurred. /// ///
public length ( ) : int
return int

read() public method

public read ( ) : byte
return byte

readByte() public method

Reads a byte of data from the stream.
If the end-of file was reached. /// /// If an I/O error ocurred. /// ///
public readByte ( ) : byte
return byte

readDouble() public method

Reads an IEEE double precision (i.e., 64 bit) floating-point number from the input.
If the end-of file was reached before getting /// all the necessary data. /// /// If an I/O error ocurred. /// ///
public readDouble ( ) : double
return double

readFloat() public method

Reads an IEEE single precision (i.e., 32 bit) floating-point number from the input.
If the end-of file was reached before getting /// all the necessary data. /// /// If an I/O error ocurred. /// ///
public readFloat ( ) : float
return float

readFully() public method

Reads '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 n ) : 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. /// ///
n int
return void

readInt() public method

Reads a signed int (32 bit) from the input.
If the end-of file was reached before getting /// all the necessary data. /// /// If an I/O error ocurred. /// ///
public readInt ( ) : int
return int

readLong() public method

Reads a signed long (64 bit) from the input.
If the end-of file was reached before getting /// all the necessary data. /// /// If an I/O error ocurred. /// ///
public readLong ( ) : long
return long

readShort() public method

Reads a signed short (16 bit) from the input.
If the end-of file was reached before getting /// all the necessary data. /// /// If an I/O error ocurred. /// ///
public readShort ( ) : short
return short

readUnsignedByte() public method

Reads an unsigned byte (8 bit) from the input.
If the end-of file was reached before getting /// all the necessary data. /// /// If an I/O error ocurred. /// ///
public readUnsignedByte ( ) : byte
return byte

readUnsignedInt() public method

Reads a unsigned int (32 bit) from the input.
If the end-of file was reached before getting /// all the necessary data. /// /// If an I/O error ocurred. /// ///
public readUnsignedInt ( ) : long
return long

readUnsignedShort() public method

Reads an unsigned short (16 bit) from the input.
If the end-of file was reached before getting /// all the necessary data. /// /// If an I/O error ocurred. /// ///
public readUnsignedShort ( ) : int
return int

seek() public method

Moves the current position for the next read operation to offset. The offset is measured from the beginning of the stream. If the offset is set beyond the currently cached data, the missing data will be read only when a read operation is performed. Setting the offset beyond the end of the data will cause an EOFException only if the data length is currently known, otherwise an IOException will occur when a read operation is attempted at that position.
If seeking beyond EOF and the data length is /// known. /// /// 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.
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

Throws an IOException since this class does not implement data output.
public write ( byte b ) : void
b byte
return void

writeByte() public method

Throws an IOException since this class does not implement data output.
public writeByte ( int v ) : void
v int
return void

writeDouble() public method

Throws an IOException since this class does not implement data output.
public writeDouble ( double v ) : void
v double
return void

writeFloat() public method

Throws an IOException since this class does not implement data output.
public writeFloat ( float v ) : void
v float
return void

writeInt() public method

Throws an IOException since this class does not implement data output.
public writeInt ( int v ) : void
v int
return void

writeLong() public method

Throws an IOException since this class does not implement data output.
public writeLong ( long v ) : void
v long
return void

writeShort() public method

Throws an IOException since this class does not implement data output.
public writeShort ( int v ) : void
v int
return void