public class LineNumberInputStream
extends FilterInputStream
| java.lang.Object | |||
| java.io.InputStream | |||
| java.io.FilterInputStream | |||
| java.io.LineNumberInputStream | |||
此类已在API级别1中弃用。
该类错误地假定字节充分表示字符。 从JDK 1.1开始,操作字符流的首选方法是通过新的字符流类,其中包括用于计算行号的类。
这个类是一个输入流过滤器,提供追踪当前行号的附加功能。
一行是以一个回车符( '\r' ),一个换行符( '\n' )或一个回车符紧跟一个换行符结尾的一系列字节。 在所有三种情况下,行终止字符作为单个换行符返回。
行号从 0开始,并在 read返回换行符时增加 1 。
也可以看看:
Inherited fields |
|---|
java.io.FilterInputStream
|
Public constructors |
|
|---|---|
LineNumberInputStream(InputStream in) 构造一个换行符输入流,从指定的输入流读取其输入。 |
|
公共方法(Public methods) |
|
|---|---|
int |
available() 返回可以从该输入流中读取而不被阻塞的字节数。 |
int |
getLineNumber() 返回当前行号。 |
void |
mark(int readlimit) 标记此输入流中的当前位置。 |
int |
read() 从此输入流中读取下一个字节的数据。 |
int |
read(byte[] b, int off, int len) 从这个输入流中最多读取 |
void |
reset() 将此流重新定位到上次在此输入流上调用 |
void |
setLineNumber(int lineNumber) 将行号设置为指定的参数。 |
long |
skip(long n) 跳过并丢弃来自此输入流的 |
继承方法(Inherited methods) |
|
|---|---|
java.io.FilterInputStream
|
|
java.io.InputStream
|
|
java.lang.Object
|
|
java.io.Closeable
|
|
java.lang.AutoCloseable
|
|
LineNumberInputStream (InputStream in)
构造一个换行符输入流,从指定的输入流读取其输入。
| 参数(Parameters) | |
|---|---|
in |
InputStream: the underlying input stream. |
int available ()
返回可以从该输入流中读取而不被阻塞的字节数。
请注意,如果底层输入流能够不阻塞地提供 k个输入字符,则 LineNumberInputStream可以保证只提供 k / 2个字符而不会阻塞,因为来自底层输入流的 k个字符可能由 k / 2对 '\r'和 '\n' ,它们被转换成只有 k / 2个 '\n'字符。
| 返回(Returns) | |
|---|---|
int |
the number of bytes that can be read from this input stream without blocking. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
也可以看看:
int getLineNumber ()
返回当前行号。
| 返回(Returns) | |
|---|---|
int |
the current line number. |
也可以看看:
void mark (int readlimit)
标记此输入流中的当前位置。 随后调用reset方法将此流重新定位到最后标记的位置,以便后续读取重新读取相同的字节。
mark方法 LineNumberInputStream记住私有变量中的当前行号,然后调用基础输入流的 mark方法。
| 参数(Parameters) | |
|---|---|
readlimit |
int: the maximum limit of bytes that can be read before the mark position becomes invalid. |
int read ()
从此输入流中读取下一个字节的数据。 值字节被返回作为int范围0到255 。 如果由于流的末尾已到达而没有可用字节,则返回值-1 。 此方法阻塞直到输入数据可用,流的末尾被检测到,或抛出异常。
read方法LineNumberInputStream调用底层输入流的read方法。 它检查输入中的回车符和换行符,并根据需要修改当前行号。 一个回车符或一个回车符后跟一个换行符都被转换成一个换行符。
| 返回(Returns) | |
|---|---|
int |
the next byte of data, or -1 if the end of this stream is reached. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
也可以看看:
int read (byte[] b,
int off,
int len)
最多可将len字节的数据从此输入流转换为字节数组。 此方法阻塞,直到有些输入可用。
该 read的方法 LineNumberInputStream反复调用 read的零个参数方法来填充字节数组英寸
| 参数(Parameters) | |
|---|---|
b |
byte: the buffer into which the data is read. |
off |
int: the start offset of the data. |
len |
int: the maximum number of bytes read. |
| 返回(Returns) | |
|---|---|
int |
the total number of bytes read into the buffer, or -1 if there is no more data because the end of this stream has been reached. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
也可以看看:
void reset ()
将此流重新定位到上次在此输入流上调用 mark方法时的位置。
该 reset的方法 LineNumberInputStream复位的行号是在该时间的行号 mark方法被调用,然后调用 reset基础输入流的方法。
流标记旨在用于需要稍微阅读以查看流中内容的情况。 这通常通过调用一些通用解析器来完成。 如果流属于解析器处理的类型,它就会快乐地跳出来。 如果流不是这种类型,那么解析器在失败时应抛出异常,如果它发生在readlimit字节内,则允许外部代码重置流并尝试另一个解析器。
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
void setLineNumber (int lineNumber)
将行号设置为指定的参数。
| 参数(Parameters) | |
|---|---|
lineNumber |
int: the new line number. |
也可以看看:
long skip (long n)
跳过并丢弃来自此输入流的n个字节的数据。 由于各种原因, skip方法可能跳过一些较小数量的字节,可能是0 。 返回跳过的实际字节数。 如果n为负数,则不跳过字节。
该 skip的方法 LineNumberInputStream创建一个字节数组,然后重复读入,直到 n字节已被读出或流的结尾已经到达。
| 参数(Parameters) | |
|---|---|
n |
long: the number of bytes to be skipped. |
| 返回(Returns) | |
|---|---|
long |
the actual number of bytes skipped. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
也可以看看: