public class PushbackInputStream extends FilterInputStream
PushbackInputStream将功能添加到另一个输入流,即可以“推回”或“未读”一个字节。
这在代码片段方便读取由特定字节值分隔的无限数量的数据字节的情况下非常有用;
读取终止字节后,代码段可以“未读”,以便输入流上的下一个读操作将重新读取被推回的字节。
例如,表示构成标识符的字符的字节可以被表示操作符的字节终止;
一个只能读取一个标识符的工作方法可以读取,直到它看到操作员,然后将操作员推回以重新读取。
| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
buf
推回缓冲区。
|
protected int |
pos
推回缓冲区中的位置,从该位置读取下一个字节。
|
in| Constructor and Description |
|---|
PushbackInputStream(InputStream in)
创建一个
PushbackInputStream并保存其参数,输入流
in供以后使用。
|
PushbackInputStream(InputStream in, int size)
使用
PushbackInputStream的推回缓冲区创建
size ,并保存其参数,输入流
in供以后使用。
|
| Modifier and Type | Method and Description |
|---|---|
int |
available()
返回从该输入流中可以读取(或跳过)的字节数的估计值,而不会被下一次调用此输入流的方法阻塞。
|
void |
close()
关闭此输入流并释放与流相关联的任何系统资源。
|
void |
mark(int readlimit)
标记此输入流中的当前位置。
|
boolean |
markSupported()
测试这个输入流是否支持
mark和
reset方法,而不是。
|
int |
read()
从该输入流读取下一个数据字节。
|
int |
read(byte[] b, int off, int len)
从该输入流读取最多
len字节的数据为字节数组。
|
void |
reset()
将此流重新定位到最后在此输入流上调用
mark方法时的位置。
|
long |
skip(long n)
跳过并丢弃来自此输入流的
n字节的数据。
|
void |
unread(byte[] b)
将一个字节数组复制回推回缓冲区的前端。
|
void |
unread(byte[] b, int off, int len)
通过将字节数组复制到推回缓冲区的前端来推回一部分数组。
|
void |
unread(int b)
通过将其复制到推回缓冲区的前端来推回一个字节。
|
readprotected byte[] buf
protected int pos
pos等于buf.length ;
当缓冲区已满时, pos等于零。
public PushbackInputStream(InputStream in, int size)
PushbackInputStream的推回缓冲区的size ,并保存其参数,输入流in供以后使用。
最初没有推回字节(字段pushBack被初始化为-1 )。
in - 读取字节的输入流。
size - 推回缓冲区的大小。
IllegalArgumentException - 如果
size <= 0
public PushbackInputStream(InputStream in)
PushbackInputStream并保存其参数,输入流in供以后使用。
最初没有推回字节(字段pushBack被初始化为-1 )。
in - 读取字节的输入流。
public int read()
throws IOException
int返回,范围为0至255 。
如果没有字节可用,因为流已经到达,则返回值-1 。
该方法阻塞直到输入数据可用,检测到流的结尾,或抛出异常。
该方法返回最近推送的字节(如果有的话),否则调用其底层输入流的read方法,并返回该方法返回的任何值。
read在类别
FilterInputStream
-1如果已经达到流的末尾)。
IOException - 如果此输入流已通过调用其
close()方法已关闭,或发生I / O错误。
InputStream.read()
public int read(byte[] b,
int off,
int len)
throws IOException
len个字节的数据到字节数组。
该方法首先读取任何推回的字节;
之后,如果已读取少于len个字节,则从底层输入流读取。
如果len不为零,则该方法将阻塞,直到输入的至少1个字节可用;
否则,不会读取字节,并返回0 。
read在类别
FilterInputStream
b - 读取数据的缓冲区。
off - 目标数组中的起始偏移量
b
len - 读取的最大字节数。
-1 。
NullPointerException - 如果
b是
null 。
IndexOutOfBoundsException - 如果
off为负,则
len为负数,或
len大于
b.length - off
IOException - 如果此输入流已通过调用其
close()方法关闭,或发生I / O错误。
InputStream.read(byte[], int, int)
public void unread(int b)
throws IOException
(byte)b 。
b -
int低位字节推回的值为
int 。
IOException - 如果该缓冲区中的字节没有足够的空间,或者通过调用其
close()方法已关闭此输入流。
public void unread(byte[] b,
int off,
int len)
throws IOException
b[off] ,之后的字节将具有值b[off+1]等等。
b - 要推回的字节数组。
off - 数据的起始偏移量。
len - 要推回的字节数。
IOException - 如果在推送缓冲区中没有足够的空间用于指定的字节数,或者通过调用其
close()方法已关闭此输入流。
public void unread(byte[] b)
throws IOException
b[0] ,之后的字节将具有值b[1]等等。
b - 要推回的字节数组
IOException - 如果
IOException缓冲区中没有足够的空间用于指定的字节数,或者通过调用其
close()方法已关闭此输入流。
public int available()
throws IOException
available在
FilterInputStream
IOException - 如果此输入流已通过调用其
close()方法关闭,或发生I / O错误。
FilterInputStream.in ,
InputStream.available()
public long skip(long n)
throws IOException
n字节的数据。
由于各种原因, skip方法可能会跳过某些较小数量的字节,可能为零。
如果n为负,则不跳过任何字节。
该skip方法的PushbackInputStream在推回缓冲区中的字节第一跳过,如果有的话。 然后,如果需要跳过更多的字节,它将调用底层输入流的skip方法。 返回实际跳过的字节数。
skip在类别
FilterInputStream
n - 要跳过的字节数。
IOException - 如果流不支持查询,或流已通过调用其
close()方法关闭,或发生I / O错误。
FilterInputStream.in ,
InputStream.skip(long n)
public boolean markSupported()
mark和
reset方法,而不是。
markSupported在
FilterInputStream
false ,因为这个类不支持
mark和
reset方法。
InputStream.mark(int) ,
InputStream.reset()
public void mark(int readlimit)
该mark的方法PushbackInputStream什么都不做。
mark在类别
FilterInputStream
readlimit - 标记位置无效之前可以读取的最大字节数限制。
InputStream.reset()
public void reset()
throws IOException
mark方法时的位置。
该方法reset类PushbackInputStream什么也不做只是抛出一个IOException 。
reset在
FilterInputStream
IOException - 如果这个方法被调用。
InputStream.mark(int) , IOException
public void close()
throws IOException
close在界面
Closeable
close在接口
AutoCloseable
close在类别
FilterInputStream
IOException - 如果发生I / O错误。
FilterInputStream.in
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.