public class BufferedInputStream
extends FilterInputStream
| java.lang.Object | |||
| ↳ | java.io.InputStream | ||
| ↳ | java.io.FilterInputStream | ||
| ↳ | java.io.BufferedInputStream | ||
A BufferedInputStream为另一个输入流增加了功能,即缓冲输入和支持mark和reset方法的功能。 当创建BufferedInputStream会创建一个内部缓冲区阵列。 当流中的字节被读取或跳过时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次处理多个字节。 mark操作会记住输入流中的一个点,并且reset操作会导致自从最新的mark操作在从所包含的输入流中获取新字节之前重新读取后的所有字节读取。
Fields |
|
|---|---|
protected byte[] |
buf 存储数据的内部缓冲区数组。 |
protected int |
count 索引1大于缓冲区中最后一个有效字节的索引。 |
protected int |
marklimit 读取的最大的一个电话后,允许提前 |
protected int |
markpos 调用最后一个 |
protected int |
pos 缓冲区中的当前位置。 |
Inherited fields |
|---|
java.io.FilterInputStream
|
Public constructors |
|
|---|---|
BufferedInputStream(InputStream in) 创建一个 |
|
BufferedInputStream(InputStream in, int size) 使用指定的缓冲区大小创建 |
|
Public methods |
|
|---|---|
int |
available() 返回可从此输入流读取(或跳过)的字节数的估计值,而不会因为此输入流的下一次调用方法而被阻止。 |
void |
close() 关闭此输入流并释放与该流关联的所有系统资源。 |
void |
mark(int readlimit) 请参阅 |
boolean |
markSupported() 测试此输入流是否支持 |
int |
read() 参见 |
int |
read(byte[] b, int off, int len) 将该字节输入流中的字节读入指定的字节数组,从给定的偏移量开始。 |
void |
reset() 请参阅 |
long |
skip(long n) 参见 |
Inherited methods |
|
|---|---|
java.io.FilterInputStream
|
|
java.io.InputStream
|
|
java.lang.Object
|
|
java.io.Closeable
|
|
java.lang.AutoCloseable
|
|
int count
索引1大于缓冲区中最后一个有效字节的索引。 该值始终在0到buf.length ; 元素buf[0]到buf[count-1]包含从底层输入流获取的缓冲输入数据。
int marklimit
读取的最大的一个电话后,允许提前mark后续调用方法之前reset方法失败。 每当pos和markpos之间的差异超过marklimit ,可以通过将markpos设置为-1来降低该标记。
int markpos
调用最后一个 mark方法时的 pos字段的值。
该值始终在-1到pos 。 如果输入流中没有标记位置,则此字段为-1 。 如果输入流中存在标记位置,则buf[markpos]是在reset操作之后作为输入提供的第一个字节。 如果markpos不是-1 ,然后从位置的所有字节buf[markpos]通过buf[pos-1]必须保留在缓冲器阵列中(尽管它们可以被移动到缓冲器阵列中的另一个处,与适当的调整的值count , pos ,和markpos ); 除非pos与markpos之间的差异超过marklimit否则不得抛弃。
int pos
缓冲区中的当前位置。 这是从buf阵列读取的下一个字符的索引。
该值始终在0到count 。 如果它小于count ,那么buf[pos]是要作为输入提供的下一个字节; 如果它等于count ,则接下来的read或skip操作将需要从包含的输入流中读取更多字节。
也可以看看:
BufferedInputStream (InputStream in)
创建一个BufferedInputStream并保存其参数,即输入流in以供以后使用。 内部缓冲区阵列被创建并存储在buf 。
| Parameters | |
|---|---|
in |
InputStream: the underlying input stream. |
BufferedInputStream (InputStream in, int size)
使用指定的缓冲区大小创建BufferedInputStream ,并保存其参数(输入流in )以备后用。 创建一个长度为size的内部缓冲区数组并存储在buf 。
| Parameters | |
|---|---|
in |
InputStream: the underlying input stream. |
size |
int: the buffer size. |
| Throws | |
|---|---|
IllegalArgumentException |
if size <= 0. |
int available ()
返回可从此输入流读取(或跳过)的字节数的估计值,而不会因为此输入流的下一次调用方法而被阻止。 下一次调用可能是同一个线程或另一个线程。 单个读取或跳过这么多字节不会被阻塞,但可以读取或跳过更少的字节。
此方法返回缓冲区中剩余要读取的字节数( count - pos )和调用 in .available()的结果之和。
| Returns | |
|---|---|
int |
an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking. |
| Throws | |
|---|---|
IOException |
if this input stream has been closed by invoking its close() method, or an I/O error occurs. |
void close ()
关闭此输入流并释放与该流关联的所有系统资源。 一旦流被关闭,进一步的read(),available(),reset()或skip()调用将抛出一个IOException异常。 关闭以前关闭的流不起作用。
| Throws | |
|---|---|
IOException |
if an I/O error occurs. |
void mark (int readlimit)
请参阅 mark方法 InputStream的一般合同。
| Parameters | |
|---|---|
readlimit |
int: the maximum limit of bytes that can be read before the mark position becomes invalid. |
也可以看看:
boolean markSupported ()
测试此输入流是否支持mark和reset方法。 该markSupported方法BufferedInputStream回报true 。
| Returns | |
|---|---|
boolean |
a boolean indicating if this stream type supports the mark and reset methods. |
int read ()
请参阅 read方法 InputStream的总体合同。
| Returns | |
|---|---|
int |
the next byte of data, or -1 if the end of the stream is reached. |
| Throws | |
|---|---|
IOException |
if this input stream has been closed by invoking its close() method, or an I/O error occurs. |
也可以看看:
int read (byte[] b,
int off,
int len)
将该字节输入流中的字节读入指定的字节数组,从给定的偏移量开始。
该方法实现了类的相应InputStream方法的总体合同。 作为额外的便利,它通过重复调用底层流的readread方法尝试读取尽可能多的字节。 重复执行read直到下列条件之一read为止:
read method of the underlying stream returns -1, indicating end-of-file, or available method of the underlying stream returns zero, indicating that further input requests would block. read on the underlying stream returns
-1 to indicate end-of-file then this method returns
-1. Otherwise this method returns the number of bytes actually read.
鼓励这个类的子类,但不是必需的,试图以相同的方式读取尽可能多的字节。
| Parameters | |
|---|---|
b |
byte: destination buffer. |
off |
int: offset at which to start storing bytes. |
len |
int: maximum number of bytes to read. |
| Returns | |
|---|---|
int |
the number of bytes read, or -1 if the end of the stream has been reached. |
| Throws | |
|---|---|
IOException |
if this input stream has been closed by invoking its close() method, or an I/O error occurs. |
void reset ()
请参阅 reset方法 InputStream的一般合同。
如果markpos是-1 (未设置标记或标记已失效),则引发IOException 。 否则, pos设置为等于markpos 。
| Throws | |
|---|---|
IOException |
if this stream has not been marked or, if the mark has been invalidated, or the stream has been closed by invoking its close() method, or an I/O error occurs. |
也可以看看:
long skip (long n)
请参阅 skip方法 InputStream的一般合同。
| Parameters | |
|---|---|
n |
long: the number of bytes to be skipped. |
| Returns | |
|---|---|
long |
the actual number of bytes skipped. |
| Throws | |
|---|---|
IOException |
if the stream does not support seek, or if this input stream has been closed by invoking its close() method, or an I/O error occurs. |