DataInput
interface provides
for reading bytes from a binary stream and
reconstructing from them data in any of
the Java primitive types. There is also
a
facility for reconstructing a String
from data in
modified UTF-8
format.
It is generally true of all the reading
routines in this interface that if end of
file is reached before the desired number
of bytes has been read, an EOFException
(which is a kind of IOException
)
is thrown. If any byte cannot be read for
any reason other than end of file, an IOException
other than EOFException
is
thrown. In particular, an IOException
may be thrown if the input stream has been
closed.
Implementations of the DataInput and DataOutput interfaces represent Unicode strings in a format that is a slight modification of UTF-8. (For information regarding the standard UTF-8 format, see section 3.9 Unicode Encoding Forms of The Unicode Standard, Version 4.0)
'\u0001'
to
'\u007F'
are represented by a single byte.
'\u0000'
and characters
in the range '\u0080'
to '\u07FF'
are
represented by a pair of bytes.
'\u0800'
to '\uFFFF'
are represented by three bytes.
Value | Byte | Bit Values | |||||||
---|---|---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ||
\u0001 to \u007F |
1 | 0 | bits 6-0 | ||||||
\u0000 ,\u0080 to \u07FF |
1 | 1 | 1 | 0 | bits 10-6 | ||||
2 | 1 | 0 | bits 5-0 | ||||||
\u0800 to \uFFFF |
1 | 1 | 1 | 1 | 0 | bits 15-12 | |||
2 | 1 | 0 | bits 11-6 | ||||||
3 | 1 | 0 | bits 5-0 |
The differences between this format and the standard UTF-8 format are the following:
'\u0000'
is encoded in 2-byte format
rather than 1-byte, so that the encoded strings never have
embedded nulls.
java.io.DataInputStream
, java.io.DataOutput
Modifier and Type | Method and Description |
---|---|
public boolean | Returns: theboolean value read.Reads one input byte and returns
|
public byte | |
public char | |
public double | |
public float | |
public void | readFully(byte[]
the buffer into which the data is read. b)Reads some bytes from an input
stream and stores them into the buffer
array |
public void | readFully(byte[]
the buffer into which the data is read. b, int an int specifying the offset in the data array off, int b .an int specifying the number of bytes to read. len)Reads |
public int | |
public String | Returns: the next line of text from the input stream, ornull if the end of file is
encountered before a byte can be read.Reads the next line of text from the input stream. |
public long | |
public short | |
public int | Returns: the unsigned 8-bit value read.Reads one input byte, zero-extends
it to type |
public int | Returns: the unsigned 16-bit value read.Reads two input bytes and returns
an |
public String | Returns: a Unicode string.Reads in a string that has been encoded using a modified UTF-8 format. |
public int | Returns: the number of bytes actually skipped.the number of bytes to be skipped. n)Makes an attempt to skip over
|
readBoolean | back to summary |
---|---|
public boolean readBoolean() throws IOException Reads one input byte and returns
|
readByte | back to summary |
---|---|
public byte readByte() throws IOException Reads and returns one input byte.
The byte is treated as a signed value in
the range
|
readChar | back to summary |
---|---|
public char readChar() throws IOException Reads two input bytes and returns a
This method
is suitable for reading bytes written by
the writeChar method of interface
DataOutput .
|
readDouble | back to summary |
---|---|
public double readDouble() throws IOException Reads eight input bytes and returns
a
|
readFloat | back to summary |
---|---|
public float readFloat() throws IOException Reads four input bytes and returns
a
|
readFully | back to summary |
---|---|
public void readFully(byte[] b) throws IOException Reads some bytes from an input
stream and stores them into the buffer
array This method blocks until one of the following conditions occurs:
If
|
readFully | back to summary |
---|---|
public void readFully(byte[] b, int off, int len) throws IOException Reads This method blocks until one of the following conditions occurs:
If
|
readInt | back to summary |
---|---|
public int readInt() throws IOException Reads four input bytes and returns an
This method is suitable
for reading bytes written by the writeInt
method of interface DataOutput .
|
readLine | back to summary |
---|---|
public String readLine() throws IOException Reads the next line of text from the input stream.
It reads successive bytes, converting
each byte separately into a character,
until it encounters a line terminator or
end of
file; the characters read are then
returned as a
If end of file is encountered
before even one byte can be read, then
|
readLong | back to summary |
---|---|
public long readLong() throws IOException Reads eight input bytes and returns
a
This method is suitable
for reading bytes written by the
|
readShort | back to summary |
---|---|
public short readShort() throws IOException Reads two input bytes and returns
a
This method
is suitable for reading the bytes written
by the writeShort method of
interface DataOutput .
|
readUnsignedByte | back to summary |
---|---|
public int readUnsignedByte() throws IOException Reads one input byte, zero-extends
it to type
|
readUnsignedShort | back to summary |
---|---|
public int readUnsignedShort() throws IOException Reads two input bytes and returns
an
This method is suitable for reading the bytes
written by the writeShort method
of interface DataOutput if
the argument to writeShort
was intended to be a value in the range
0 through 65535 .
|
readUTF | back to summary |
---|---|
public String readUTF() throws IOException Reads in a string that has been encoded using a
modified UTF-8
format.
The general contract of
First, two bytes are read and used to
construct an unsigned 16-bit integer in
exactly the manner of the
If the first byte of a group
matches the bit pattern
If the first byte
of a group matches the bit pattern
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:
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
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
The
|
skipBytes | back to summary |
---|---|
public int skipBytes(int n) throws IOException Makes an attempt to skip over
|