Package loci.common
Class NIOInputStream
java.lang.Object
java.io.InputStream
loci.common.NIOInputStream
- All Implemented Interfaces:
java.io.Closeable
,java.io.DataInput
,java.lang.AutoCloseable
public class NIOInputStream
extends java.io.InputStream
implements java.io.DataInput
NIOInputStream provides methods for "intelligent" reading of files
and byte arrays.
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.channels.Channel
channel
The file channel backed by the random access file.protected static int
DEFAULT_BLOCK_SIZE
Block size to use when searching through the stream.protected java.io.File
file
The file.protected java.lang.String
filename
The file name.protected boolean
isLittleEndian
Endianness of the stream.protected static int
MAX_SEARCH_SIZE
Maximum number of bytes to search when searching through the stream.protected IRandomAccess
raf
-
Constructor Summary
Constructors Constructor Description NIOInputStream(byte[] array)
Constructs a random access stream around the given byte array.NIOInputStream(java.lang.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 Type Method Description int
available()
void
close()
Closes the streams.java.lang.String
findString(boolean saveString, int blockSize, java.lang.String... terminators)
Reads or skips a string ending with one of the given terminating substrings, using the specified block size for buffering.java.lang.String
findString(boolean saveString, java.lang.String... terminators)
Reads or skips a string ending with one of the given terminating substrings.java.lang.String
findString(int blockSize, java.lang.String... terminators)
Reads a string ending with one of the given terminating substrings, using the specified block size for buffering.java.lang.String
findString(java.lang.String... terminators)
Reads a string ending with one of the given terminating substrings.long
getFilePointer()
java.io.DataInputStream
getInputStream()
boolean
isLittleEndian()
long
length()
Gets the number of bytes in the file.void
mark(int readLimit)
boolean
markSupported()
void
order(boolean isLittleEndian)
Sets the endianness of the stream.int
read()
Alias for readByte().int
read(byte[] array)
Read bytes from the stream into the given array.int
read(byte[] array, int offset, int n)
Read n bytes from the stream into the given array at the specified offset.boolean
readBoolean()
Read an input byte and return true if the byte is nonzero.byte
readByte()
Read one byte and return it.char
readChar()
Read an input char.java.lang.String
readCString()
Read a string of arbitrary length, terminated by a null char.double
readDouble()
Read eight bytes and return a double value.float
readFloat()
Read four bytes and return a float value.void
readFully(byte[] array)
Read bytes from the stream into the given array.void
readFully(byte[] array, int offset, int n)
Read n bytes from the stream into the given array at the specified offset.int
readInt()
Read four input bytes and return an int value.java.lang.String
readLine()
Read the next line of text from the input stream.long
readLong()
Read eight input bytes and return a long value.short
readShort()
Read two input bytes and return a short value.java.lang.String
readString(int n)
Read a string of length n.java.lang.String
readString(java.lang.String lastChars)
Reads a string ending with one of the characters in the given string.int
readUnsignedByte()
Read an input byte and zero extend it appropriately.int
readUnsignedShort()
Read two bytes and return an int in the range 0 through 65535.java.lang.String
readUTF()
Read a string that has been encoded using a modified UTF-8 format.void
reset()
void
seek(long pos)
Seeks to the given offset within the stream.void
setExtend(int extend)
Sets the number of bytes by which to extend the stream.int
skipBytes(int n)
Skip n bytes within the stream.
-
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:
- Constant Field Values
-
MAX_SEARCH_SIZE
protected static final int MAX_SEARCH_SIZEMaximum number of bytes to search when searching through the stream.- See Also:
- Constant Field Values
-
raf
-
filename
protected java.lang.String filenameThe file name. -
file
protected java.io.File fileThe file. -
channel
protected java.nio.channels.Channel channelThe file channel backed by the random access file. -
isLittleEndian
protected boolean isLittleEndianEndianness of the stream.
-
-
Constructor Details
-
NIOInputStream
public NIOInputStream(java.lang.String filename) throws java.io.IOExceptionConstructs an NIOInputStream around the given file.- Parameters:
filename
- the path to a readable file on disk- Throws:
java.io.IOException
- if the path is invalid or unreadable
-
NIOInputStream
Constructs a random access stream around the given handle.- Parameters:
handle
- theIRandomAccess
to 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
public java.io.DataInputStream 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
public void seek(long pos) throws java.io.IOExceptionSeeks to the given offset within the stream.- Parameters:
pos
- the offset to which to seek- Throws:
java.io.IOException
- if the seek is not successful
-
read
public int read() throws java.io.IOExceptionAlias for readByte().- Specified by:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
length
public long length() throws java.io.IOExceptionGets the number of bytes in the file.- Returns:
- the length of the stream in bytes
- Throws:
java.io.IOException
- if the length cannot be retrieved
-
getFilePointer
public long getFilePointer()- Returns:
- the current (absolute) file pointer.
-
close
public void close() throws java.io.IOExceptionCloses the streams.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.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
public java.lang.String readString(java.lang.String lastChars) throws java.io.IOExceptionReads 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:
java.io.IOException
- If the maximum length (512 MB) is exceeded.- See Also:
findString(String...)
-
findString
public java.lang.String findString(java.lang.String... terminators) throws java.io.IOExceptionReads 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:
java.io.IOException
- If the maximum search length (512 MB) is exceeded.
-
findString
public java.lang.String findString(boolean saveString, java.lang.String... terminators) throws java.io.IOExceptionReads 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:
java.io.IOException
- If saveString flag is set and the maximum search length (512 MB) is exceeded.java.io.IOException
- If saveString flag is set and the maximum search length (512 MB) is exceeded.
-
findString
public java.lang.String findString(int blockSize, java.lang.String... terminators) throws java.io.IOExceptionReads 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:
java.io.IOException
- If the maximum search length (512 MB) is exceeded.
-
findString
public java.lang.String findString(boolean saveString, int blockSize, java.lang.String... terminators) throws java.io.IOExceptionReads 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:
java.io.IOException
- If saveString flag is set and the maximum search length (512 MB) is exceeded.
-
readBoolean
public boolean readBoolean() throws java.io.IOExceptionRead an input byte and return true if the byte is nonzero.- Specified by:
readBoolean
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readByte
public byte readByte() throws java.io.IOExceptionRead one byte and return it.- Specified by:
readByte
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readChar
public char readChar() throws java.io.IOExceptionRead an input char.- Specified by:
readChar
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readDouble
public double readDouble() throws java.io.IOExceptionRead eight bytes and return a double value.- Specified by:
readDouble
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOExceptionRead four bytes and return a float value.- Specified by:
readFloat
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOExceptionRead four input bytes and return an int value.- Specified by:
readInt
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readLine
public java.lang.String readLine() throws java.io.IOExceptionRead the next line of text from the input stream.- Specified by:
readLine
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readCString
public java.lang.String readCString() throws java.io.IOExceptionRead a string of arbitrary length, terminated by a null char.- Returns:
- the smallest string such that the last character is a null byte
- Throws:
java.io.IOException
- if a null-terminated string is not found- See Also:
findString(String...)
-
readString
public java.lang.String readString(int n) throws java.io.IOExceptionRead a string of length n.- Parameters:
n
- the number of bytes to read- Returns:
- a string representing the next
n
bytes in the stream - Throws:
java.io.IOException
- if there is an error during reading
-
readLong
public long readLong() throws java.io.IOExceptionRead eight input bytes and return a long value.- Specified by:
readLong
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readShort
public short readShort() throws java.io.IOExceptionRead two input bytes and return a short value.- Specified by:
readShort
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readUnsignedByte
public int readUnsignedByte() throws java.io.IOExceptionRead an input byte and zero extend it appropriately.- Specified by:
readUnsignedByte
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readUnsignedShort
public int readUnsignedShort() throws java.io.IOExceptionRead two bytes and return an int in the range 0 through 65535.- Specified by:
readUnsignedShort
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readUTF
public java.lang.String readUTF() throws java.io.IOExceptionRead a string that has been encoded using a modified UTF-8 format.- Specified by:
readUTF
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
skipBytes
public int skipBytes(int n) throws java.io.IOExceptionSkip n bytes within the stream.- Specified by:
skipBytes
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
read
public int read(byte[] array) throws java.io.IOExceptionRead bytes from the stream into the given array.- Overrides:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] array, int offset, int n) throws java.io.IOExceptionRead n bytes from the stream into the given array at the specified offset.- Overrides:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] array) throws java.io.IOExceptionRead bytes from the stream into the given array.- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] array, int offset, int n) throws java.io.IOExceptionRead n bytes from the stream into the given array at the specified offset.- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
available
public int available() throws java.io.IOException- Overrides:
available
in classjava.io.InputStream
- Throws:
java.io.IOException
-
mark
public void mark(int readLimit)- Overrides:
mark
in classjava.io.InputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupported
in classjava.io.InputStream
-
reset
public void reset() throws java.io.IOException- Overrides:
reset
in classjava.io.InputStream
- Throws:
java.io.IOException
-