Package loci.common
Class RandomAccessInputStream
java.lang.Object
java.io.InputStream
loci.common.RandomAccessInputStream
- All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable,Closeable,DataInput,AutoCloseable
public class RandomAccessInputStream
extends InputStream
implements DataInput, Closeable, com.esotericsoftware.kryo.KryoSerializable
Top-level class for reading from various data sources.
- Author:
- Melissa Linkert melissa at glencoesoftware.com, Curtis Rueden ctrueden at wisc.edu
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intBlock size to use when searching through the stream.protected Stringprotected StringThe file name.protected longprotected longprotected static final intMaximum size of the buffer used by the DataInputStream.protected static final intMaximum number of bytes to search when searching through the stream.protected IRandomAccess -
Constructor Summary
ConstructorsConstructorDescriptionRandomAccessInputStream(byte[] array) Constructs a random access stream around the given byte array.Constructs a hybrid RandomAccessFile/DataInputStream around the given file.RandomAccessInputStream(String file, int bufferSize) Constructs a hybrid RandomAccessFile/DataInputStream around the given file.RandomAccessInputStream(IRandomAccess handle) Constructs a random access stream around the given handle.RandomAccessInputStream(IRandomAccess handle, String file) Constructs a random access stream around the given handle, and with the associated file path. -
Method Summary
Modifier and TypeMethodDescriptionintvoidclose()Closes the streams.findString(boolean saveString, int blockSize, String... terminators) Reads or skips a string ending with one of the given terminating substrings, using the specified block size for buffering.findString(boolean saveString, String... terminators) Reads or skips a string ending with one of the given terminating substrings.findString(int blockSize, String... terminators) Reads a string ending with one of the given terminating substrings, using the specified block size for buffering.findString(String... terminators) Reads a string ending with one of the given terminating substrings.longbooleanChecks if the current position is on a byte boundary, that is the next bit in the byte array is the first bit in a byte.booleanGets the endianness of the stream.longlength()voidmark(int readLimit) booleanvoidorder(boolean little) Sets the endianness of the stream.intread()intread(byte[] array) Read bytes from the stream into the given array.intread(byte[] array, int offset, int n) Read n bytes from the stream into the given array at the specified offset.voidread(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input in) intread(ByteBuffer buf) Read bytes from the stream into the given buffer.intread(ByteBuffer buf, int offset, int n) Read n bytes from the stream into the given buffer at the specified offset.intreadBits(int bitsToRead) Returns an int value representing the value of the bits read from the byte array, from the current position.booleanRead an input byte and return true if the byte is nonzero.bytereadByte()Read one byte and return it.readByteToString(int n) Reads a byte array of the given length byte by byte.charreadChar()Read an input char.Read a string of arbitrary length, terminated by a null char.doubleRead eight bytes and return a double value.floatRead four bytes and return a float value.voidreadFully(byte[] array) Read bytes from the stream into the given array.voidreadFully(byte[] array, int offset, int n) Read n bytes from the stream into the given array at the specified offset.intreadInt()Read four input bytes and return an int value.readLine()Read the next line of text from the input stream.longreadLong()Read eight input bytes and return a long value.shortRead two input bytes and return a short value.readString(int n) Read a string of up to length n.readString(String lastChars) Reads a string ending with one of the characters in the given string.intRead an input byte and zero extend it appropriately.longRead four input bytes and return an unsigned value.intRead two bytes and return an int in the range 0 through 65535.readUTF()Read a string that has been encoded using a modified UTF-8 format.voidreset()voidseek(long pos) Seeks to the given offset within the stream.voidsetEncoding(String encoding) Sets the native encoding of the stream.voidsetLength(long newLength) Sets the length of the stream.voidskipBits(long bits) Skips a number of bits in the BitBuffer.intskipBytes(int n) Skip n bytes within the stream.longskipBytes(long n) Skip n bytes within the stream.voidwrite(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output out) Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skip, skipNBytes, transferTo
-
Field Details
-
MAX_OVERHEAD
protected static final int MAX_OVERHEADMaximum size of the buffer used by the DataInputStream.- See Also:
-
DEFAULT_BLOCK_SIZE
protected static final int DEFAULT_BLOCK_SIZEBlock size to use when searching through the stream.- See Also:
-
MAX_SEARCH_SIZE
protected static final int MAX_SEARCH_SIZEMaximum number of bytes to search when searching through the stream.- See Also:
-
raf
-
file
The file name. -
length
protected long length -
markedPos
protected long markedPos -
encoding
-
-
Constructor Details
-
RandomAccessInputStream
Constructs a hybrid RandomAccessFile/DataInputStream around the given file.- Parameters:
file- a name that can be passed toLocation.getHandle(String)- Throws:
IOException- if the name is invalid
-
RandomAccessInputStream
Constructs a hybrid RandomAccessFile/DataInputStream around the given file.- Parameters:
file- a name that can be passed toLocation.getHandle(String)bufferSize- the size of the caching buffer in bytes- Throws:
IOException- if the name is invalid
-
RandomAccessInputStream
Constructs a random access stream around the given handle.- Parameters:
handle- theIRandomAccessto be wrapped- Throws:
IOException- if the handle is invalid
-
RandomAccessInputStream
Constructs a random access stream around the given handle, and with the associated file path.- Parameters:
handle- theIRandomAccessto be wrappedfile- the name associated with the handle. Can be null.- Throws:
IOException- if the handle is invalid
-
RandomAccessInputStream
Constructs a random access stream around the given byte array.- Parameters:
array- the byte array to be wrapped viaByteArrayHandle- Throws:
IOException- if theByteArrayHandlecannot be created
-
-
Method Details
-
setEncoding
Sets the native encoding of the stream.- Parameters:
encoding- the name of a standard charset to use when working with strings- See Also:
-
seek
Seeks to the given offset within the stream.- Parameters:
pos- the new byte offset- Throws:
IOException- if the seek fails
-
length
- Returns:
- the number of bytes in the file.
- Throws:
IOException- if the length cannot be retrieved
-
setLength
Sets the length of the stream. The new length must be less than the real length of the stream. This allows us to work with a truncated view of a file, without modifying the file itself. Passing in a negative value will reset the length to the stream's real length.- Parameters:
newLength- the new stream length as defined above- Throws:
IOException- if the original stream length cannot be retrieved
-
getFilePointer
- Returns:
- the current (absolute) file pointer.
- Throws:
IOException- if the current pointer cannot be retrieved
-
close
Closes the streams.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
order
public void order(boolean little) Sets the endianness of the stream.- Parameters:
little- true if the stream ordering should be little-endian
-
isLittleEndian
public boolean isLittleEndian()Gets the endianness of the stream.- Returns:
- true if the stream ordering is little-endian
-
readString
Reads a string ending with one of the characters in the given string.- Parameters:
lastChars- each character is a possible terminator- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found.
- Throws:
IOException- If the maximum search length (512 MB) is exceeded.- See Also:
-
findString
Reads a string ending with one of the given terminating substrings.- Parameters:
terminators- The strings for which to search.- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found.
- Throws:
IOException- If the maximum search length (512 MB) is exceeded.
-
findString
Reads or skips a string ending with one of the given terminating substrings.- Parameters:
saveString- Whether to collect the string from the current file pointer to the terminating bytes, and return it. If false, returns null.terminators- The strings for which to search.- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found, or null if saveString flag is unset.
- Throws:
IOException- If saveString flag is set and the maximum search length (512 MB) is exceeded.
-
findString
Reads a string ending with one of the given terminating substrings, using the specified block size for buffering.- Parameters:
blockSize- The block size to use when reading bytes in chunks.terminators- The strings for which to search.- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found.
- Throws:
IOException- If the maximum search length (512 MB) is exceeded.
-
findString
public String findString(boolean saveString, int blockSize, String... terminators) throws IOException Reads or skips a string ending with one of the given terminating substrings, using the specified block size for buffering.- Parameters:
saveString- Whether to collect the string from the current file pointer to the terminating bytes, and return it. If false, returns null.blockSize- The block size to use when reading bytes in chunks.terminators- The strings for which to search.- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found, or null if saveString flag is unset.
- Throws:
IOException- If saveString flag is set and the maximum search length (512 MB) is exceeded.
-
skipBits
Skips a number of bits in the BitBuffer.- Parameters:
bits- Number of bits to skip- Throws:
IllegalArgumentException- if bits is negativeIOException- if an error occurs while skipping
-
readBits
Returns an int value representing the value of the bits read from the byte array, from the current position. Bits are extracted from the "left side" or high side of the byte.The current position is modified by this call.
Bits are pushed into the int from the right, endianness is not considered by the method on its own. So, if 5 bits were read from the buffer "10101", the int would be the integer representation of 000...0010101 on the target machine.
In general, this also means the result will be positive unless a full 32 bits are read.
Requesting more than 32 bits is allowed, but only up to 32 bits worth of data will be returned (the last 32 bits read).
- Parameters:
bitsToRead- the number of bits to read from the bit buffer- Returns:
- the value of the bits read
- Throws:
IllegalArgumentException- if bits is negativeIOException- if an error occurs while skipping
-
isBitOnByteBoundary
public boolean isBitOnByteBoundary()Checks if the current position is on a byte boundary, that is the next bit in the byte array is the first bit in a byte.- Returns:
- true if bit is on byte boundary, false otherwise.
-
readBoolean
Read an input byte and return true if the byte is nonzero.- Specified by:
readBooleanin interfaceDataInput- Throws:
IOException
-
readByte
Read one byte and return it.- Specified by:
readBytein interfaceDataInput- Throws:
IOException
-
readChar
Read an input char.- Specified by:
readCharin interfaceDataInput- Throws:
IOException
-
readDouble
Read eight bytes and return a double value.- Specified by:
readDoublein interfaceDataInput- Throws:
IOException
-
readFloat
Read four bytes and return a float value.- Specified by:
readFloatin interfaceDataInput- Throws:
IOException
-
readInt
Read four input bytes and return an int value.- Specified by:
readIntin interfaceDataInput- Throws:
IOException
-
readUnsignedInt
Read four input bytes and return an unsigned value.- Returns:
- the next 4 bytes in the stream as a long
- Throws:
IOException- if there is an error during reading
-
readLine
Read the next line of text from the input stream.- Specified by:
readLinein interfaceDataInput- Throws:
IOException
-
readCString
Read a string of arbitrary length, terminated by a null char.- Returns:
- the shortest null-terminated string from the current pointer
- Throws:
IOException- if there is an error during reading
-
readByteToString
Reads a byte array of the given length byte by byte. Returns a string using the set encoding.- Parameters:
n- The length of the array.- Returns:
- See above
- Throws:
IOException- Thrown if an error occurred while reading the data.- See Also:
-
readString
Read a string of up to length n.- Parameters:
n- the number of bytes to read- Returns:
- a string representing the read bytes, using the default encoding
- Throws:
IOException- if an error occurred during reading- See Also:
-
readLong
Read eight input bytes and return a long value.- Specified by:
readLongin interfaceDataInput- Throws:
IOException
-
readShort
Read two input bytes and return a short value.- Specified by:
readShortin interfaceDataInput- Throws:
IOException
-
readUnsignedByte
Read an input byte and zero extend it appropriately.- Specified by:
readUnsignedBytein interfaceDataInput- Throws:
IOException
-
readUnsignedShort
Read two bytes and return an int in the range 0 through 65535.- Specified by:
readUnsignedShortin interfaceDataInput- Throws:
IOException
-
readUTF
Read a string that has been encoded using a modified UTF-8 format.- Specified by:
readUTFin interfaceDataInput- Throws:
IOException
-
skipBytes
Skip n bytes within the stream.- Specified by:
skipBytesin interfaceDataInput- Throws:
IOException
-
skipBytes
Skip n bytes within the stream.- Throws:
IOException
-
read
Read bytes from the stream into the given array.- Overrides:
readin classInputStream- Throws:
IOException
-
read
Read n bytes from the stream into the given array at the specified offset.- Overrides:
readin classInputStream- Throws:
IOException
-
read
Read bytes from the stream into the given buffer.- Parameters:
buf- theByteBufferto fill.buf.capacity()determines the number of bytes to read- Returns:
- the number of bytes read
- Throws:
IOException- if an error occurred during reading
-
read
Read n bytes from the stream into the given buffer at the specified offset.- Parameters:
buf- theByteBufferto filloffset- the offset to the first byte in the buffern- the number of bytes to read- Returns:
- the number of bytes actually read
- Throws:
IOException- if an error occurred during reading
-
readFully
Read bytes from the stream into the given array.- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readFully
Read n bytes from the stream into the given array at the specified offset.- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
mark
public void mark(int readLimit) - Overrides:
markin classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classInputStream
-
reset
- Overrides:
resetin classInputStream- Throws:
IOException
-
read
public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input in) - Specified by:
readin interfacecom.esotericsoftware.kryo.KryoSerializable
-
write
public void write(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output out) - Specified by:
writein interfacecom.esotericsoftware.kryo.KryoSerializable
-