ByteArrayInputStream的作用:
包含一个内部缓冲区,其中包含可以从流中读取的字节。 内部计数器跟踪由read
方法提供的下一个字节。关闭一个ByteArrayInputStream
没有任何效果。 该流中的方法可以在流关闭后调用,而不生成IOException
。意思就是说,比如文件流的处理对象的外存中的文件,而它的处理对象是内存中的缓冲区。它是从一个内存读到另一个内存方式。
Modifier and Type | Field | 描述 |
---|---|---|
protected byte[] | buf |
由数据流的创建者提供的字节数组。
|
protected int | count |
索引一大于输入流缓冲区中的最后一个有效字符。
|
protected int | mark |
流中当前标记的位置。
|
protected int | pos |
从输入流缓冲区读取的下一个字符的索引。
|
Constructor | 描述 |
---|---|
ByteArrayInputStream(byte[] buf) |
创建一个
ByteArrayInputStream ,使其使用
buf 作为其缓冲区数组。
|
ByteArrayInputStream(byte[] buf, int offset, int length) |
创建
ByteArrayInputStream 使用
buf 作为其缓冲器阵列。
|
Modifier and Type | 方法 | 描述 |
---|---|---|
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 字节输入。
|
注:
public ByteArrayInputStream(byte[] buf, int offset, int length)
创建
ByteArrayInputStream
使用
buf
作为其缓冲器阵列。
pos
是
offset
和的初始值,
count
是最小
offset+length
和
buf.length
。 缓冲区数组不被复制。 缓冲区的标记设置为指定的偏移量。
-
参数
-
buf
- 输入缓冲区。 -
offset
- 要读取的第一个字节的缓冲区中的偏移量。 -
length
- 从缓冲区读取的最大字节数。 -
import java.io.ByteArrayInputStream; import java.io.IOException; public class TestByteArrayInputStreamClass { public static void main(String[] args) throws IOException { //内存中的一个字节数组 byte []buf="11111".getBytes(); //创建该字节数组的输入流 ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(buf); //内存中的另一个数组 byte []pos=new byte[buf.length]; //通过字节数组输入流向该内存中输入字节 while (byteArrayInputStream.read(pos)!=-1); byteArrayInputStream.close(); System.out.println(new String(pos)); } }