Package loci.common
Class NIOInputStream
java.lang.Object
java.io.InputStream
loci.common.NIOInputStream
- All Implemented Interfaces:
Closeable,DataInput,AutoCloseable
NIOInputStream provides methods for "intelligent" reading of files
and byte arrays.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ChannelThe file channel backed by the random access file.protected static final intBlock size to use when searching through the stream.protected FileThe file.protected StringThe file name.protected booleanEndianness of the stream.protected static final intMaximum number of bytes to search when searching through the stream.protected IRandomAccess -
Constructor Summary
ConstructorsConstructorDescriptionNIOInputStream(byte[] array) Constructs a random access stream around the given byte array.NIOInputStream(String filename) Constructs an NIOInputStream around the given file.NIOInputStream(IRandomAccess handle) Constructs a random access stream around the given handle. -
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.longbooleanlonglength()Gets the number of bytes in the file.voidmark(int readLimit) booleanvoidorder(boolean isLittleEndian) Sets the endianness of the stream.intread()Alias for readByte().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.booleanRead an input byte and return true if the byte is nonzero.bytereadByte()Read one byte and return it.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 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.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.voidsetExtend(int extend) Sets the number of bytes by which to extend the stream.intskipBytes(int n) Skip n bytes within the stream.Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skip, skipNBytes, transferTo
-
Field Details
-
DEFAULT_BLOCK_SIZE
protected static final int DEFAULT_BLOCK_SIZEBlock size to use when searching through the stream. This value should not exceed MAX_OVERHEAD!- 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
-
filename
The file name. -
file
The file. -
channel
The file channel backed by the random access file. -
isLittleEndian
protected boolean isLittleEndianEndianness of the stream.
-
-
Constructor Details
-
NIOInputStream
Constructs an NIOInputStream around the given file.- Parameters:
filename- the path to a readable file on disk- Throws:
IOException- if the path is invalid or unreadable
-
NIOInputStream
Constructs a random access stream around the given handle.- Parameters:
handle- theIRandomAccessto wrap in a stream
-
NIOInputStream
public NIOInputStream(byte[] array) Constructs a random access stream around the given byte array.- Parameters:
array- the byte array to wrap in a stream
-
-
Method Details
-
getInputStream
- Returns:
- the underlying InputStream.
-
setExtend
public void setExtend(int extend) Sets the number of bytes by which to extend the stream. This only applies to InputStream API methods.- Parameters:
extend- the number of bytes by which to extend the stream
-
seek
Seeks to the given offset within the stream.- Parameters:
pos- the offset to which to seek- Throws:
IOException- if the seek is not successful
-
read
Alias for readByte().- Specified by:
readin classInputStream- Throws:
IOException
-
length
Gets the number of bytes in the file.- Returns:
- the length of the stream in bytes
- Throws:
IOException- if the length cannot be retrieved
-
getFilePointer
public long getFilePointer()- Returns:
- the current (absolute) file pointer.
-
close
Closes the streams.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
order
public void order(boolean isLittleEndian) Sets the endianness of the stream.- Parameters:
isLittleEndian- true if the stream order is little-endian
-
isLittleEndian
public boolean isLittleEndian()- Returns:
- the endianness of the stream.
-
readString
Reads a string ending with one of the characters in the given string.- Parameters:
lastChars- string containing possible final characters for the returned string- Returns:
- the smallest string that ends in one of the characters in
lastChars - Throws:
IOException- If the maximum 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.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.
-
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
-
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 smallest string such that the last character is a null byte
- Throws:
IOException- if a null-terminated string is not found- See Also:
-
readString
Read a string of length n.- Parameters:
n- the number of bytes to read- Returns:
- a string representing the next
nbytes in the stream - Throws:
IOException- if there is an error during reading
-
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
-
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
-
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
-
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
-