public class IstiDataInputStream
extends java.io.FilterInputStream
implements java.io.DataInput
Constructor and Description |
---|
IstiDataInputStream(java.io.InputStream in)
Create an IstiDataInputStream that reads from the specified InputStream.
|
IstiDataInputStream(java.io.InputStream in,
boolean little)
Create an IstiDataInputStream that reads from the specified InputStream.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns the number of bytes that can be read from this input
stream without blocking.
|
void |
close()
Closes this input stream and releases any system resources
associated with the stream.
|
boolean |
isBigEndian()
Determines if the default is big-endian.
|
boolean |
isLittleEndian()
Determines if the default is little-endian.
|
void |
mark(int readlimit)
Marks the current position in this input stream.
|
boolean |
markSupported()
Tests if this input stream supports the
mark
and reset methods. |
int |
read()
Reads the next byte of data from this input stream.
|
int |
read(byte[] b)
Reads up to
byte.length bytes of data from this
input stream into an array of bytes. |
int |
read(byte[] b,
int off,
int len)
Reads up to
len bytes of data from this input stream
into an array of bytes. |
boolean |
readBoolean()
Reads one input byte and returns
true if that byte is nonzero,
false if that byte is zero. |
byte |
readByte()
Reads and returns one input byte.
|
byte[] |
readByteArray(int length)
Reads an array of bytes.
|
char |
readChar()
Reads a 16 bit char.
|
char[] |
readCharArray(int length)
Reads an array of chars.
|
double |
readDouble()
Reads a 64 bit double.
|
double[] |
readDoubleArray(int length)
Reads an array of doubles.
|
float |
readFloat()
Reads a 32 bit float.
|
float[] |
readFloatArray(int length)
Reads an array of floats.
|
void |
readFully(byte[] b)
Reads some bytes from an input
stream and stores them into the buffer
array
b . |
void |
readFully(byte[] b,
int off,
int len)
Reads
len
bytes from
an input stream. |
int |
readInt()
Reads a 32 bit int.
|
int[] |
readIntArray(int length)
Reads an array of ints.
|
java.lang.String |
readLine()
Read a line of text.
|
long |
readLong()
Reads a 64 bit long.
|
long[] |
readLongArray(int length)
Reads an array of longs.
|
short |
readShort()
Reads a 16 bit short.
|
short[] |
readShortArray(int length)
Reads an array of shorts.
|
int |
readUnsignedByte()
Reads one input byte, zero-extends
it to type
int , and returns
the result, which is therefore in the range
0
through 255 . |
int[] |
readUnsignedByteArray(int length)
Reads an array of unsigned bytes.
|
int |
readUnsignedShort()
Reads an unsigned 16 bit short.
|
int[] |
readUnsignedShortArray(int length)
Reads an array of unsigned shorts.
|
java.lang.String |
readUTF()
Reads in a string that has been encoded using a modified UTF-8 format.
|
static java.lang.String |
readUTF(java.io.DataInput in)
Reads from the
stream
in a representation
of a Unicode character string encoded in
Java modified UTF-8 format; this string
of characters is then returned as a String . |
void |
reset()
Repositions this stream to the position at the time the
mark method was last called on this input stream. |
void |
setBigEndian(boolean big)
Sets the default to big-endian or little-endian.
|
void |
setLittleEndian(boolean little)
Sets the default to little-endian or big-endian.
|
long |
skip(long n)
Skips over and discards
n bytes of data from the
input stream. |
int |
skipBytes(int n)
Makes an attempt to skip over
n bytes
of data from the input
stream, discarding the skipped bytes. |
public IstiDataInputStream(java.io.InputStream in) throws java.io.IOException, java.io.StreamCorruptedException
in
- the underlying InputStream
from which to readjava.io.StreamCorruptedException
- The version or magic number are
incorrect.java.io.IOException
- An exception occurred in the underlying stream.public IstiDataInputStream(java.io.InputStream in, boolean little) throws java.io.IOException, java.io.StreamCorruptedException
in
- the underlying InputStream
from which to readlittle
- if true default is little-endian otherwise it is big-endian.java.io.StreamCorruptedException
- The version or magic number are
incorrect.java.io.IOException
- An exception occurred in the underlying stream.public boolean isBigEndian()
public boolean isLittleEndian()
public void setBigEndian(boolean big)
big
- if true default is big-endian otherwise it is little-endian.public void setLittleEndian(boolean little)
little
- if true default is little-endian otherwise it is big-endian.public int read() throws java.io.IOException
int
in the range
0
to 255
. If no byte is available
because the end of the stream has been reached, the value
-1
is returned. This method blocks until input data
is available, the end of the stream is detected, or an exception
is thrown.read
in class java.io.FilterInputStream
-1
if the end of the
stream is reached.java.io.IOException
- if an I/O error occurs.FilterInputStream.in
public int read(byte[] b) throws java.io.IOException
byte.length
bytes of data from this
input stream into an array of bytes. This method blocks until some
input is available.read
in class java.io.FilterInputStream
b
- the buffer into which the data is read.-1
if there is no more data because the end of
the stream has been reached.java.io.IOException
- if an I/O error occurs.FilterInputStream.read(byte[], int, int)
public int read(byte[] b, int off, int len) throws java.io.IOException
len
bytes of data from this input stream
into an array of bytes. This method blocks until some input is
available.read
in class java.io.FilterInputStream
b
- the buffer into which the data is read.off
- the start offset of the data.len
- the maximum number of bytes read.-1
if there is no more data because the end of
the stream has been reached.java.io.IOException
- if an I/O error occurs.FilterInputStream.in
public long skip(long n) throws java.io.IOException
n
bytes of data from the
input stream. The skip
method may, for a variety of
reasons, end up skipping over some smaller number of bytes,
possibly 0
. The actual number of bytes skipped is
returned.skip
in class java.io.FilterInputStream
n
- the number of bytes to be skipped.java.io.IOException
- if an I/O error occurs.public int available() throws java.io.IOException
available
in class java.io.FilterInputStream
java.io.IOException
- if an I/O error occurs.FilterInputStream.in
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.FilterInputStream
java.io.IOException
- if an I/O error occurs.FilterInputStream.in
public void mark(int readlimit)
reset
method repositions this stream at
the last marked position so that subsequent reads re-read the same bytes.
The readlimit
argument tells this input stream to
allow that many bytes to be read before the mark position gets
invalidated.
mark
in class java.io.FilterInputStream
readlimit
- the maximum limit of bytes that can be read before
the mark position becomes invalid.FilterInputStream.in
,
FilterInputStream.reset()
public void reset() throws java.io.IOException
mark
method was last called on this input stream.
Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails. If this happens within readlimit bytes, it allows the outer code to reset the stream and try another parser.
reset
in class java.io.FilterInputStream
java.io.IOException
- if the stream has not been marked or if the
mark has been invalidated.FilterInputStream.in
,
FilterInputStream.mark(int)
public boolean markSupported()
mark
and reset
methods.markSupported
in class java.io.FilterInputStream
true
if this stream type supports the
mark
and reset
method;
false
otherwise.FilterInputStream.in
,
InputStream.mark(int)
,
InputStream.reset()
public void readFully(byte[] b) throws java.io.IOException
b
. The number of bytes
read is equal
to the length of b
.
This method blocks until one of the following conditions occurs:
b.length
bytes of input data are available, in which
case a normal return is made.
EOFException
is thrown.
IOException
other
than EOFException
is thrown.
If b
is null
,
a NullPointerException
is thrown.
If b.length
is zero, then
no bytes are read. Otherwise, the first
byte read is stored into element b[0]
,
the next one into b[1]
, and
so on.
If an exception is thrown from
this method, then it may be that some but
not all bytes of b
have been
updated with data from the input stream.
readFully
in interface java.io.DataInput
b
- the buffer into which the data is read.java.io.EOFException
- if this stream reaches the end before reading
all the bytes.java.io.IOException
- if an I/O error occurs.public void readFully(byte[] b, int off, int len) throws java.io.IOException
len
bytes from
an input stream.
This method blocks until one of the following conditions occurs:
len
bytes
of input data are available, in which case
a normal return is made.
EOFException
is thrown.
IOException
other
than EOFException
is thrown.
If b
is null
,
a NullPointerException
is thrown.
If off
is negative, or len
is negative, or off+len
is
greater than the length of the array b
,
then an IndexOutOfBoundsException
is thrown.
If len
is zero,
then no bytes are read. Otherwise, the first
byte read is stored into element b[off]
,
the next one into b[off+1]
,
and so on. The number of bytes read is,
at most, equal to len
.
readFully
in interface java.io.DataInput
b
- the buffer into which the data is read.off
- an int specifying the offset into the data.len
- an int specifying the number of bytes to read.java.io.EOFException
- if this stream reaches the end before reading
all the bytes.java.io.IOException
- if an I/O error occurs.public int skipBytes(int n) throws java.io.IOException
n
bytes
of data from the input
stream, discarding the skipped bytes. However,
it may skip
over some smaller number of
bytes, possibly zero. This may result from
any of a
number of conditions; reaching
end of file before n
bytes
have been skipped is
only one possibility.
This method never throws an EOFException
.
The actual
number of bytes skipped is returned.skipBytes
in interface java.io.DataInput
n
- the number of bytes to be skipped.n
.java.io.EOFException
- if this stream reaches the end before skipping
all the bytes.java.io.IOException
- if an I/O error occurs.public boolean readBoolean() throws java.io.IOException
true
if that byte is nonzero,
false
if that byte is zero.
This method is suitable for reading
the byte written by the writeBoolean
method of interface DataOutput
.readBoolean
in interface java.io.DataInput
boolean
value read.java.io.EOFException
- if this stream reaches the end before reading
all the bytes.java.io.IOException
- if an I/O error occurs.public byte readByte() throws java.io.IOException
-128
through 127
,
inclusive.
This method is suitable for
reading the byte written by the writeByte
method of interface DataOutput
.readByte
in interface java.io.DataInput
java.io.EOFException
- if this stream reaches the end before reading
all the bytes.java.io.IOException
- if an I/O error occurs.public int readUnsignedByte() throws java.io.IOException
int
, and returns
the result, which is therefore in the range
0
through 255
.
This method is suitable for reading
the byte written by the writeByte
method of interface DataOutput
if the argument to writeByte
was intended to be a value in the range
0
through 255
.readUnsignedByte
in interface java.io.DataInput
java.io.EOFException
- if this stream reaches the end before reading
all the bytes.java.io.IOException
- if an I/O error occurs.public short readShort() throws java.io.IOException
readShort
in interface java.io.DataInput
java.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public int readUnsignedShort() throws java.io.IOException
readUnsignedShort
in interface java.io.DataInput
java.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public char readChar() throws java.io.IOException
readChar
in interface java.io.DataInput
java.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public int readInt() throws java.io.IOException
readInt
in interface java.io.DataInput
java.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public long readLong() throws java.io.IOException
readLong
in interface java.io.DataInput
java.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public float readFloat() throws java.io.IOException
readFloat
in interface java.io.DataInput
java.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public double readDouble() throws java.io.IOException
readDouble
in interface java.io.DataInput
java.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public java.lang.String readLine() throws java.io.IOException
readLine
in interface java.io.DataInput
java.io.IOException
- If an I/O error occurspublic java.lang.String readUTF() throws java.io.IOException
readUTF
is that it reads a representation of a Unicode
character string encoded in Java modified
UTF-8 format; this string of characters
is then returned as a String
.
First, two bytes are read and used to
construct an unsigned 16-bit integer in
exactly the manner of the readUnsignedShort
method . This integer value is called the
UTF length and specifies the number
of additional bytes to be read. These bytes
are then converted to characters by considering
them in groups. The length of each group
is computed from the value of the first
byte of the group. The byte following a
group, if any, is the first byte of the
next group.
If the first byte of a group
matches the bit pattern 0xxxxxxx
(where x
means "may be 0
or 1
"), then the group consists
of just that byte. The byte is zero-extended
to form a character.
If the first byte
of a group matches the bit pattern 110xxxxx
,
then the group consists of that byte a
and a second byte b
. If there
is no byte b
(because byte
a
was the last of the bytes
to be read), or if byte b
does
not match the bit pattern 10xxxxxx
,
then a UTFDataFormatException
is thrown. Otherwise, the group is converted
to the character:
(char)(((a& 0x1F) << 6) | (b & 0x3F))
If the first byte of a group
matches the bit pattern 1110xxxx
,
then the group consists of that byte a
and two more bytes b
and c
.
If there is no byte c
(because
byte a
was one of the last
two of the bytes to be read), or either
byte b
or byte c
does not match the bit pattern 10xxxxxx
,
then a UTFDataFormatException
is thrown. Otherwise, the group is converted
to the character:
(char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
If the first byte of a group matches the
pattern 1111xxxx
or the pattern
10xxxxxx
, then a UTFDataFormatException
is thrown.
If end of file is encountered
at any time during this entire process,
then an EOFException
is thrown.
After every group has been converted to
a character by this process, the characters
are gathered, in the same order in which
their corresponding groups were read from
the input stream, to form a String
,
which is returned.
The writeUTF
method of interface DataOutput
may be used to write data that is suitable
for reading by this method.
readUTF
in interface java.io.DataInput
java.io.EOFException
- if this stream reaches the end
before reading all the bytes.java.io.IOException
- if an I/O error occurs.java.io.UTFDataFormatException
- if the bytes do not represent a
valid UTF-8 encoding of a string.public static final java.lang.String readUTF(java.io.DataInput in) throws java.io.IOException
in
a representation
of a Unicode character string encoded in
Java modified UTF-8 format; this string
of characters is then returned as a String
.
The details of the modified UTF-8 representation
are exactly the same as for the readUTF
method of DataInput
.in
- a data input stream.java.io.EOFException
- if the input stream reaches the end
before all the bytes.java.io.IOException
- if an I/O error occurs.java.io.UTFDataFormatException
- if the bytes do not represent a
valid UTF-8 encoding of a Unicode string.DataInputStream.readUnsignedShort()
public byte[] readByteArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public int[] readUnsignedByteArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public short[] readShortArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public int[] readUnsignedShortArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public char[] readCharArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public int[] readIntArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public long[] readLongArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public float[] readFloatArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.public double[] readDoubleArray(int length) throws java.io.IOException
length
- array lengthjava.io.EOFException
- If end of file is reached.java.io.IOException
- If other I/O error has occurred.