public interface DataInput
| java.io.DataInput |
| |
DataInput接口提供从二进制流中读取字节,并从它们中重建任何Java基本类型中的数据。 还有,为了重建设施String从数据modified UTF-8格式。
在这个接口中的所有阅读例程通常都是如此,如果在读取所需数量的字节之前达到文件结尾,则引发EOFException (这是一种IOException )。 如果任何字节因文件结尾IOException其他原因而无法读取,则会抛出IOException以外的EOFException 。 特别是,如果输入流已关闭, IOException可能会引发IOException 。
DataInput和DataOutput接口的实现表示Unicode字符串,其格式是对UTF-8的轻微修改。 (有关标准UTF-8格式的信息,请参阅Unicode标准4.0版的 Unicode编码格式部分)。 请注意,在下表中,最重要的位出现在最左侧的列中。
'\u0001'至 '\u007F'范围内的所有字符都由一个字节表示:
Bit Values Byte 1
0 bits 6-0
空字符 '\u0000'和范围 '\u0080'到 '\u07FF'中的字符由一对字节表示:
Bit Values Byte 1
1 1 0 bits 10-6 Byte 2
1 0 bits 5-0
char values in the range
'\u0800' to
'\uFFFF' are represented by three bytes:
Bit Values Byte 1
1 1 1 0 bits 15-12 Byte 2
1 0 bits 11-6 Byte 3
1 0 bits 5-0
这种格式与标准UTF-8格式的区别如下:
'\u0000' is encoded in 2-byte format rather than 1-byte, so that the encoded strings never have embedded nulls. 也可以看看:
公共方法(Public methods) |
|
|---|---|
abstract boolean |
readBoolean() 读取一个输入字节,并返回 |
abstract byte |
readByte() 读取并返回一个输入字节。 |
abstract char |
readChar() 读取两个输入字节并返回 |
abstract double |
readDouble() 读取8个输入字节并返回 |
abstract float |
readFloat() 读取四个输入字节并返回 |
abstract void |
readFully(byte[] b) 从输入流中读取一些字节并将它们存储到缓冲区阵列 |
abstract void |
readFully(byte[] b, int off, int len) 从输入流中读取 |
abstract int |
readInt() 读取四个输入字节并返回值 |
abstract String |
readLine() 从输入流中读取下一行文本。 |
abstract long |
readLong() 读取8个输入字节并返回 |
abstract short |
readShort() 读取两个输入字节并返回 |
abstract String |
readUTF() 读取已使用 modified UTF-8格式编码的字符串。 |
abstract int |
readUnsignedByte() 读取一个输入字节,将其零延伸为键入 |
abstract int |
readUnsignedShort() 读取两个输入字节并返回范围 |
abstract int |
skipBytes(int n) 尝试跳过输入流中的 |
boolean readBoolean ()
读取一个输入字节并返回true如果该字节不为零), false如果该字节为零)。 此方法适用于读取writeBoolean接口DataOutput方法写入的字节。
| 返回(Returns) | |
|---|---|
boolean |
the boolean value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
byte readByte ()
读取并返回一个输入字节。 该字节被视为-128到127范围内的有符号值。 该方法适用于读取writeByte接口DataOutput的writeByte方法写入的字节。
| 返回(Returns) | |
|---|---|
byte |
the 8-bit value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
char readChar ()
读取两个输入字节并返回一个char值。 假设a是第一个读取的字节, b是第二个字节。 返回的值是:
(char)((a << 8) | (b & 0xff))
This method is suitable for reading bytes written by the
writeChar method of interface
DataOutput.
| 返回(Returns) | |
|---|---|
char |
the char value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
double readDouble ()
读取八个输入字节并返回一个double值。 它通过首先构建一个执行此long在完全相同的方式值readlong方法,那么这个变换long值与double在该方法的完全相同的方式Double.longBitsToDouble 。 该方法适用于读取writeDouble接口DataOutput方法写入的字节。
| 返回(Returns) | |
|---|---|
double |
the double value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
float readFloat ()
读取四个输入字节并返回一个float值。 它由第一构造一个执行此int在完全相同的方式值readInt方法,那么这个变换int值与float在该方法的完全相同的方式Float.intBitsToFloat 。 该方法适用于读取接口DataOutput的writeFloat方法写入的字节。
| 返回(Returns) | |
|---|---|
float |
the float value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
void readFully (byte[] b)
从输入流中读取一些字节并将它们存储到缓冲区阵列b 。 读取的字节数等于b的长度。
此方法阻塞,直到发生以下情况之一:
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. 如果b是null ,则引发NullPointerException 。 如果b.length为零,则不读取任何字节。 否则,读取的第一个字节存储到元素b[0] ,下一个存储到b[1] ,依此类推。 如果从此方法抛出异常,则可能是b中的一些但不是全部字节已使用来自输入流的数据更新。
| 参数(Parameters) | |
|---|---|
b |
byte: the buffer into which the data is read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
void readFully (byte[] b,
int off,
int len)
从输入流中读取 len字节。
此方法阻塞,直到发生以下情况之一:
len bytes of input data are available, in which case a normal return is made. EOFException is thrown. IOException other than EOFException is thrown. 如果b是null ,则引发NullPointerException 。 如果off为负,或者len为负,或者off+len比数组的长度大b ,那么IndexOutOfBoundsException被抛出。 如果len为零,则不读取字节。 否则,读取的第一个字节存储到元素b[off] ,下一个存储到b[off+1] ,依此类推。 读取的字节数最多等于len 。
| 参数(Parameters) | |
|---|---|
b |
byte: the buffer into which the data is read. |
off |
int: an int specifying the offset into the data. |
len |
int: an int specifying the number of bytes to read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
int readInt ()
读取四个输入字节并返回值int 。 假设a-d是第一个到第四个字节的读取。 返回的值是:
(((a & 0xff) << 24) | ((b & 0xff) << 16) |
((c & 0xff) << 8) | (d & 0xff))
This method is suitable for reading bytes written by the
writeInt method of interface
DataOutput.
| 返回(Returns) | |
|---|---|
int |
the int value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
String readLine ()
从输入流中读取下一行文本。 它读取连续的字节,将每个字节分别转换为字符,直至遇到行结束符或文件结束; 读取的字符String返回。 请注意,由于此方法处理字节,因此它不支持完整Unicode字符集的输入。
如果在读取一个字节之前遇到文件结尾,则返回null 。 否则,通过零扩展将每个读取的字节转换为char类型。 如果遇到字符'\n' ,它将被丢弃并停止阅读。 如果遇到字符'\r' ,则会丢弃该字符,并且如果后续字节转换为字符'\n' ,则该字符'\n'被丢弃; 然后阅读停止。 如果在遇到任何字符'\n'和'\r'之前遇到文件结尾,则读取停止。 一旦读数停止,返回的String包含所有读取的字符并且不会被丢弃,依次进行。 请注意,此字符串中的每个字符的值都小于\u0100 ,即(char)256 。
| 返回(Returns) | |
|---|---|
String |
the next line of text from the input stream, or null if the end of file is encountered before a byte can be read. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
long readLong ()
读取八个输入字节并返回一个long值。 假设a-h是第一个到第八个字节的读取。 返回的值是:
(((long)(a & 0xff) << 56) |
((long)(b & 0xff) << 48) |
((long)(c & 0xff) << 40) |
((long)(d & 0xff) << 32) |
((long)(e & 0xff) << 24) |
((long)(f & 0xff) << 16) |
((long)(g & 0xff) << 8) |
((long)(h & 0xff)))
该方法适用于读取由接口 DataOutput的 writeLong方法编写的字节。
| 返回(Returns) | |
|---|---|
long |
the long value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
short readShort ()
读取两个输入字节并返回short值。 设a为第一个字节, b为第二个字节。 返回的值是:
(short)((a << 8) | (b & 0xff))
This method is suitable for reading the bytes written by the
writeShort method of interface
DataOutput.
| 返回(Returns) | |
|---|---|
short |
the 16-bit value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
String readUTF ()
读取已使用modified UTF-8格式编码的字符串。 readUTF的一般合约是读取以修改的UTF-8格式编码的Unicode字符串的表示; 这串字符然后以String返回。
首先,读取两个字节并用于构造无符号的16位整数,其方式与readUnsignedShort方法完全相同。 该整数值称为UTF长度,并指定要读取的附加字节数。 这些字节然后通过分组考虑将它们转换为字符。 每个组的长度根据组的第一个字节的值计算。 一个组后面的字节(如果有的话)是下一个组的第一个字节。
如果一个组的第一个字节与位模式0xxxxxxx (其中x表示“可能是0或1 ”) 1 ,那么该组只包含该字节。 该字节被零扩展以形成一个字符。
如果一个组的第一个字节与位模式110xxxxx相匹配,那么该组由该字节a和第二个字节b 。 如果没有字节b (因为字节a是要读取的最后一个字节),或者如果字节b与位模式10xxxxxx不匹配,则会引发UTFDataFormatException 。 否则,该组将转换为字符:
(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.
如果在整个过程中随时遇到文件结尾,则会引发 EOFException 。
在通过该过程将每个组转换为字符后,按照与输入流中相应的组读取顺序相同的顺序收集字符以形成返回的 String 。
接口 DataOutput的 writeUTF方法可用于写入适合于通过该方法读取的数据。
| 返回(Returns) | |
|---|---|
String |
a Unicode string. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
UTFDataFormatException |
if the bytes do not represent a valid modified UTF-8 encoding of a string. |
int readUnsignedByte ()
读取一个输入字节,将其零扩展为int ,并返回结果,因此结果在0到255 。 此方法适用于读取由写入的字节writeByte接口的方法DataOutput ,如果参数writeByte的用意是在范围内的值0通过255 。
| 返回(Returns) | |
|---|---|
int |
the unsigned 8-bit value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
int readUnsignedShort ()
读取两个输入字节并返回0到65535范围内的int值。 设a为第一个字节, b为第二个字节。 返回的值是:
(((a & 0xff) << 8) | (b & 0xff))
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.
| 返回(Returns) | |
|---|---|
int |
the unsigned 16-bit value read. |
| 抛出异常(Throws) | |
|---|---|
EOFException |
if this stream reaches the end before reading all the bytes. |
IOException |
if an I/O error occurs. |
int skipBytes (int n)
尝试跳过输入流中的n个字节的数据,丢弃跳过的字节。 但是,它可能跳过一些较小的字节数,可能为零。 这可能是由于任何一种情况造成的。 在n字节被跳过之前达到文件n只是一种可能性。 此方法从不抛出EOFException 。 返回跳过的实际字节数。
| 参数(Parameters) | |
|---|---|
n |
int: the number of bytes to be skipped. |
| 返回(Returns) | |
|---|---|
int |
the number of bytes actually skipped. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |