public class ISRandomAccessIO extends Object implements 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.
Modifier and Type | Field and Description |
---|---|
private byte[] |
buf |
private boolean |
complete |
private int |
inc |
private InputStream |
is
The InputStream that is wrapped
|
private int |
len |
private int |
maxsize |
private int |
pos |
Constructor and Description |
---|
ISRandomAccessIO(InputStream is)
Creates 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'.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this object for reading as well as the wrapped InputStream, if
not already closed.
|
void |
flush()
Does nothing since this class does not implement data output.
|
int |
getByteOrdering()
Returns the endianess (i.e., byte ordering) of multi-byte I/O
operations.
|
int |
getPos()
Returns the current position in the stream, which is the
position from where the next byte of data would be read.
|
private void |
growBuffer()
Grows the cache buffer by 'inc', upto a maximum of 'maxsize'.
|
int |
length()
Returns the length of the stream.
|
int |
read()
Reads a byte of data from the stream.
|
byte |
readByte()
Reads a signed byte (8 bit) from the input.
|
double |
readDouble()
Reads an IEEE double precision (i.e., 64 bit) floating-point number
from the input.
|
float |
readFloat()
Reads an IEEE single precision (i.e., 32 bit) floating-point number
from the input.
|
void |
readFully(byte[] b,
int off,
int n)
Reads 'len' bytes of data from this file into an array of bytes.
|
private void |
readInput()
Reads data from the wrapped InputStream and places it in the cache
buffer.
|
int |
readInt()
Reads a signed int (32 bit) from the input.
|
long |
readLong()
Reads a signed long (64 bit) from the input.
|
short |
readShort()
Reads a signed short (16 bit) from the input.
|
int |
readUnsignedByte()
Reads an unsigned byte (8 bit) from the input.
|
long |
readUnsignedInt()
Reads a unsigned int (32 bit) from the input.
|
int |
readUnsignedShort()
Reads an unsigned short (16 bit) from the input.
|
void |
seek(int off)
Moves the current position for the next read operation to
offset.
|
int |
skipBytes(int n)
Skips 'n' bytes from the input.
|
void |
write(int b)
Throws an IOException since this class does not implement data output.
|
void |
writeByte(int v)
Throws an IOException since this class does not implement data output.
|
void |
writeDouble(double v)
Throws an IOException since this class does not implement data output.
|
void |
writeFloat(float v)
Throws an IOException since this class does not implement data output.
|
void |
writeInt(int v)
Throws an IOException since this class does not implement data output.
|
void |
writeLong(long v)
Throws an IOException since this class does not implement data output.
|
void |
writeShort(int v)
Throws an IOException since this class does not implement data output.
|
private InputStream is
private int maxsize
private int inc
private byte[] buf
private int len
private int pos
private boolean complete
public ISRandomAccessIO(InputStream is, int size, int inc, int maxsize)
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.public ISRandomAccessIO(InputStream is)
is
- The input from where to get the data.private void growBuffer() throws IOException
IOException
- If the maximum cache size is reached or if not
enough memory is available to grow the buffer.private void readInput() throws IOException
IOException
- An I/O error occurred, out of meory to grow
cache or maximum cache size reached.public void close() throws IOException
close
in interface RandomAccessIO
IOException
- If an I/O error occurs while closing the
underlying InputStream.public int getPos() throws IOException
getPos
in interface RandomAccessIO
IOException
- If an I/O error occurred.public void seek(int off) throws IOException
seek
in interface RandomAccessIO
off
- The offset where to move to.EOFException
- If seeking beyond EOF and the data length is
known.IOException
- If an I/O error ocurred.public int length() throws IOException
length
in interface RandomAccessIO
IOException
- If an I/O error ocurred.public int read() throws IOException
read
in interface RandomAccessIO
EOFException
- If the end-of file was reached.IOException
- If an I/O error ocurred.public void readFully(byte[] b, int off, int n) throws IOException
readFully
in interface RandomAccessIO
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.EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public int getByteOrdering()
getByteOrdering
in interface BinaryDataInput
getByteOrdering
in interface BinaryDataOutput
EndianType
public byte readByte() throws IOException
readByte
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public int readUnsignedByte() throws IOException
readUnsignedByte
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public short readShort() throws IOException
readShort
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public int readUnsignedShort() throws IOException
readUnsignedShort
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public int readInt() throws IOException
readInt
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public long readUnsignedInt() throws IOException
readUnsignedInt
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public long readLong() throws IOException
readLong
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public float readFloat() throws IOException
readFloat
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public double readDouble() throws IOException
readDouble
in interface BinaryDataInput
EOFException
- If the end-of file was reached before
getting all the necessary data.IOException
- If an I/O error ocurred.public int skipBytes(int n) throws IOException
skipBytes
in interface BinaryDataInput
n
- The number of bytes to skipEOFException
- If the end-of file was reached before
all the bytes could be skipped.IOException
- If an I/O error ocurred.public void flush()
flush
in interface BinaryDataOutput
public void write(int b) throws IOException
write
in interface RandomAccessIO
b
- The byte to write. The lower 8 bits of b are
written.IOException
- If an I/O error ocurred.public void writeByte(int v) throws IOException
writeByte
in interface BinaryDataOutput
v
- The value to write to the outputIOException
- If an I/O error ocurred.public void writeShort(int v) throws IOException
writeShort
in interface BinaryDataOutput
v
- The value to write to the outputIOException
- If an I/O error ocurred.public void writeInt(int v) throws IOException
writeInt
in interface BinaryDataOutput
v
- The value to write to the outputIOException
- If an I/O error ocurred.public void writeLong(long v) throws IOException
writeLong
in interface BinaryDataOutput
v
- The value to write to the outputIOException
- If an I/O error ocurred.public void writeFloat(float v) throws IOException
writeFloat
in interface BinaryDataOutput
v
- The value to write to the outputIOException
- If an I/O error ocurred.public void writeDouble(double v) throws IOException
writeDouble
in interface BinaryDataOutput
v
- The value to write to the outputIOException
- If an I/O error ocurred.Copyright © 2017 Open Microscopy Environment