Class ISRandomAccessIO
- All Implemented Interfaces:
BinaryDataInput,BinaryDataOutput,RandomAccessIO
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.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new RandomAccessIO wrapper for the given InputStream 'is'.ISRandomAccessIO(InputStream is, int size, int inc, int maxsize) Creates a new RandomAccessIO wrapper for the given InputStream 'is'. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this object for reading as well as the wrapped InputStream, if not already closed.voidflush()Does nothing since this class does not implement data output.intReturns the endianess (i.e., byte ordering) of multi-byte I/O operations.intgetPos()Returns the current position in the stream, which is the position from where the next byte of data would be read.intlength()Returns the length of the stream.intread()Reads a byte of data from the stream.bytereadByte()Reads a signed byte (8 bit) from the input.doubleReads an IEEE double precision (i.e., 64 bit) floating-point number from the input.floatReads an IEEE single precision (i.e., 32 bit) floating-point number from the input.voidreadFully(byte[] b, int off, int n) Reads 'len' bytes of data from this file into an array of bytes.intreadInt()Reads a signed int (32 bit) from the input.longreadLong()Reads a signed long (64 bit) from the input.shortReads a signed short (16 bit) from the input.intReads an unsigned byte (8 bit) from the input.longReads a unsigned int (32 bit) from the input.intReads an unsigned short (16 bit) from the input.voidseek(int off) Moves the current position for the next read operation to offset.intskipBytes(int n) Skips 'n' bytes from the input.voidwrite(int b) Throws an IOException since this class does not implement data output.voidwriteByte(int v) Throws an IOException since this class does not implement data output.voidwriteDouble(double v) Throws an IOException since this class does not implement data output.voidwriteFloat(float v) Throws an IOException since this class does not implement data output.voidwriteInt(int v) Throws an IOException since this class does not implement data output.voidwriteLong(long v) Throws an IOException since this class does not implement data output.voidwriteShort(int v) Throws an IOException since this class does not implement data output.
-
Constructor Details
-
ISRandomAccessIO
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'.- Parameters:
is- The input from where to get the data.size- The initial size for the cache buffer, in bytes.inc- The size increment for the cache buffer, in bytes.maxsize- The maximum size for the cache buffer, in bytes.
-
ISRandomAccessIO
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).- Parameters:
is- The input from where to get the data.
-
-
Method Details
-
close
Closes this object for reading as well as the wrapped InputStream, if not already closed. The memory used by the cache is released.- Specified by:
closein interfaceRandomAccessIO- Throws:
IOException- If an I/O error occurs while closing the underlying InputStream.
-
getPos
Returns the current position in the stream, which is the position from where the next byte of data would be read. The first byte in the stream is in position 0.- Specified by:
getPosin interfaceRandomAccessIO- Returns:
- The offset of the current position, in bytes.
- Throws:
IOException- If an I/O error occurred.
-
seek
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.- Specified by:
seekin interfaceRandomAccessIO- Parameters:
off- The offset where to move to.- Throws:
EOFException- If seeking beyond EOF and the data length is known.IOException- If an I/O error ocurred.
-
length
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.- Specified by:
lengthin interfaceRandomAccessIO- Returns:
- The length of the stream, in bytes.
- Throws:
IOException- If an I/O error ocurred.
-
read
Reads a byte of data from the stream.- Specified by:
readin interfaceRandomAccessIO- Returns:
- The byte read, as an int in the range [0-255].
- Throws:
EOFException- If the end-of file was reached.IOException- If an I/O error ocurred.
-
readFully
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.- Specified by:
readFullyin interfaceRandomAccessIO- 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.n- 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.
-
getByteOrdering
public int getByteOrdering()Returns the endianess (i.e., byte ordering) of multi-byte I/O operations. Always EndianType.BIG_ENDIAN since this class implements only big-endian.- Specified by:
getByteOrderingin interfaceBinaryDataInput- Specified by:
getByteOrderingin interfaceBinaryDataOutput- Returns:
- Always EndianType.BIG_ENDIAN.
- See Also:
-
readByte
Reads a signed byte (8 bit) from the input.- Specified by:
readBytein interfaceBinaryDataInput- 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
Reads an unsigned byte (8 bit) from the input.- Specified by:
readUnsignedBytein interfaceBinaryDataInput- Returns:
- The next byte-aligned unsigned 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.
-
readShort
Reads a signed short (16 bit) from the input.- Specified by:
readShortin interfaceBinaryDataInput- Returns:
- The next byte-aligned signed short (16 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.
-
readUnsignedShort
Reads an unsigned short (16 bit) from the input.- Specified by:
readUnsignedShortin interfaceBinaryDataInput- Returns:
- The next byte-aligned unsigned short (16 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.
-
readInt
Reads a signed int (32 bit) from the input.- Specified by:
readIntin interfaceBinaryDataInput- Returns:
- The next byte-aligned signed int (32 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.
-
readUnsignedInt
Reads a unsigned int (32 bit) from the input.- Specified by:
readUnsignedIntin interfaceBinaryDataInput- Returns:
- The next byte-aligned unsigned int (32 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.
-
readLong
Reads a signed long (64 bit) from the input.- Specified by:
readLongin interfaceBinaryDataInput- Returns:
- The next byte-aligned signed long (64 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.
-
readFloat
Reads an IEEE single precision (i.e., 32 bit) floating-point number from the input.- Specified by:
readFloatin interfaceBinaryDataInput- Returns:
- The next byte-aligned IEEE float (32 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.
-
readDouble
Reads an IEEE double precision (i.e., 64 bit) floating-point number from the input.- Specified by:
readDoublein interfaceBinaryDataInput- Returns:
- The next byte-aligned IEEE double (64 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.
-
skipBytes
Skips 'n' bytes from the input.- Specified by:
skipBytesin interfaceBinaryDataInput- Parameters:
n- The number of bytes to skip- Returns:
- Always n.
- Throws:
EOFException- If the end-of file was reached before all the bytes could be skipped.IOException- If an I/O error ocurred.
-
flush
public void flush()Does nothing since this class does not implement data output.- Specified by:
flushin interfaceBinaryDataOutput
-
write
Throws an IOException since this class does not implement data output.- Specified by:
writein interfaceRandomAccessIO- Parameters:
b- The byte to write. The lower 8 bits of b are written.- Throws:
IOException- If an I/O error ocurred.
-
writeByte
Throws an IOException since this class does not implement data output.- Specified by:
writeBytein interfaceBinaryDataOutput- Parameters:
v- The value to write to the output- Throws:
IOException- If an I/O error ocurred.
-
writeShort
Throws an IOException since this class does not implement data output.- Specified by:
writeShortin interfaceBinaryDataOutput- Parameters:
v- The value to write to the output- Throws:
IOException- If an I/O error ocurred.
-
writeInt
Throws an IOException since this class does not implement data output.- Specified by:
writeIntin interfaceBinaryDataOutput- Parameters:
v- The value to write to the output- Throws:
IOException- If an I/O error ocurred.
-
writeLong
Throws an IOException since this class does not implement data output.- Specified by:
writeLongin interfaceBinaryDataOutput- Parameters:
v- The value to write to the output- Throws:
IOException- If an I/O error ocurred.
-
writeFloat
Throws an IOException since this class does not implement data output.- Specified by:
writeFloatin interfaceBinaryDataOutput- Parameters:
v- The value to write to the output- Throws:
IOException- If an I/O error ocurred.
-
writeDouble
Throws an IOException since this class does not implement data output.- Specified by:
writeDoublein interfaceBinaryDataOutput- Parameters:
v- The value to write to the output- Throws:
IOException- If an I/O error ocurred.
-