public class ByteArrayInputStream extends InputStream
ByteArrayInputStream包含一个内部缓冲区,其中包含可以从流中读取的字节。
内部计数器跟踪read方法要提供的下一个字节。
关闭ByteArrayInputStream没有任何效果。 在关闭流之后,可以调用此类中的方法,而不生成IOException 。
StringBufferInputStream
| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
buf
由数据流的创建者提供的字节数组。
|
protected int |
count
索引一大于输入流缓冲区中的最后一个有效字符。
|
protected int |
mark
流中当前标记的位置。
|
protected int |
pos
从输入流缓冲区读取的下一个字符的索引。
|
| Constructor and Description |
|---|
ByteArrayInputStream(byte[] buf)
创建一个
ByteArrayInputStream ,使其使用
buf作为其缓冲区数组。
|
ByteArrayInputStream(byte[] buf, int offset, int length)
创建
ByteArrayInputStream使用
buf作为其缓冲器阵列。
|
| Modifier and Type | Method and Description |
|---|---|
int |
available()
返回可从此输入流读取(或跳过)的剩余字节数。
|
void |
close()
关闭
ByteArrayInputStream没有任何效果。
|
void |
mark(int readAheadLimit)
设置流中当前标记的位置。
|
boolean |
markSupported()
测试
InputStream是否支持标记/复位。
|
int |
read()
从该输入流读取下一个数据字节。
|
int |
read(byte[] b, int off, int len)
将
len字节的数据读入此输入流中的字节数组。
|
void |
reset()
将缓冲区重置为标记位置。
|
long |
skip(long n)
从此输入流跳过
n个字节的输入。
|
readprotected byte[] buf
buf[0]至buf[count-1]是唯一可以从流中读取的字节;
元素buf[pos]是要读取的下一个字节。
protected int pos
count的值。
要从输入流缓冲区读取的下一个字节将为buf[pos] 。
protected int mark
mark()方法标记在缓冲区内的另一个位置。
目前的缓冲区位置由reset()方法设置。
如果没有设置标记,则标记的值是传递给构造函数的偏移量(如果没有提供偏移量,则为0)。
protected int count
buf的长度。
它大于buf中可以从输入流缓冲区读取的最后一个字节的位置。
public ByteArrayInputStream(byte[] buf)
ByteArrayInputStream ,使其使用buf作为其缓冲区数组。
缓冲区数组不被复制。
的初始值pos是0和的初始值count是长度buf 。
buf - 输入缓冲区。
public ByteArrayInputStream(byte[] buf,
int offset,
int length)
ByteArrayInputStream使用buf作为其缓冲器阵列。
的初始值pos是offset和的初始值count是的最小offset+length和buf.length 。
缓冲区数组不被复制。
缓冲区的标记设置为指定的偏移量。
buf - 输入缓冲区。
offset - 要读取的第一个字节的缓冲区中的偏移量。
length - 从缓冲区读取的最大字节数。
public int read()
int ,范围为0至255 。
如果没有字节可用,因为流已经到达,则返回值-1 。
这个read方法无法阻止。
read在
InputStream
-1 。
public int read(byte[] b,
int off,
int len)
len字节的数据到一个字节数组。
如果pos等于count ,则返回-1以指示文件结束。
否则,读取的字节数为k等于len和count-pos中的较小count-pos 。
如果k为正,则字节buf[pos]通过buf[pos+k-1]被复制到b[off]通过b[off+k-1]中所执行的方式System.arraycopy 。
值k被添加到pos并返回k 。
这个read方法不能阻止。
read在
InputStream
b - 读取数据的缓冲区。
off - 目标数组中的起始偏移量
b
len - 读取的最大字节数。
-1 。
NullPointerException - 如果
b是
null 。
IndexOutOfBoundsException - 如果
off为负数,则
len为负数,或
len大于
b.length - off
InputStream.read()
public long skip(long n)
n个字节的输入。
如果达到输入流的结尾,则可能会跳过更少的字节。
要跳过的字节的实际数字为k等于n和count-pos中的较小count-pos 。
值k被添加到pos ,返回k 。
skip在
InputStream
n - 要跳过的字节数。
public int available()
返回的值为count - pos ,这是从输入缓冲区读取的剩余字节数。
available在类别
InputStream
public boolean markSupported()
InputStream是否支持标记/复位。
该markSupported的方法ByteArrayInputStream总是返回true 。
markSupported在
InputStream
true如果这个流实例支持标记和重置方法;
false否则。
InputStream.mark(int) ,
InputStream.reset()
public void mark(int readAheadLimit)
如果没有设置标记,则标记的值是传递给构造函数的偏移量(如果没有提供偏移量,则为0)。
注意:这个班的readAheadLimit没有意义。
mark在类别
InputStream
readAheadLimit - 标记位置无效之前可以读取的最大字节数限制。
InputStream.reset()
public void reset()
reset在
InputStream
InputStream.mark(int) , IOException
public void close()
throws IOException
close在界面
Closeable
close在界面
AutoCloseable
close在
InputStream
IOException - 如果发生I / O错误。
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.