该类允许应用创建一个输入流,从其中读取字节数组提供的字节。
应用也能从使用一个
StringBufferInputStream
的某字符串中读取字节。
类 java.io.ByteArrayInputStream
类 java.io.ByteArrayInputStream
java.lang.Object
|
+----java.io.InputStream
|
+----java.io.ByteArrayInputStream
- public class ByteArrayInputStream
- extends InputStream
- 出现于:
- JDK1.0
- 参见:
- StringBufferInputStream
变量索引
- buf
- 包含数据的字节数组。
- count
- 比输入缓冲区中最后有效字符大一的下标。
- mark
- 流中的当前标记位置。
- pos
- 从输入流缓冲中读入的下一个字符的下标。
构造子索引
- ByteArrayInputStream(byte[])
- 创建一个新字节数组输入流,它从指定字节数组中读取数据。
- ByteArrayInputStream(byte[], int, int)
- 创建一个新字节数组输入流,它从指定字节数组中读取数据。
方法索引
- available()
- 返回从该输入流中可无阻塞读取的字节数。
- mark(int)
- 设置流中的当前标记位置。
- markSupported()
- 若 ByteArrayInputStream 支持标记/复位,则测试。
- read()
- 从该输入流中读取下一个字节数据。
- read(byte[], int, int)
- 将该输入流中
len
个字节数据读入某个字节数组中。
- reset()
- 将缓冲区重置为标记位置。
- skip(long)
- 跳过输入流中
n
个字节数据。
变量
buf
protected byte buf[]
- 包含数据的字节数组。
pos
protected int pos
- 从输入流缓冲区中读入的下一个字符的下标。
mark
protected int mark
- 流中的当前标记位置。构造时 ByteArrayInputStreams 的缺省标记在零位置。用
mark()
方法可将其标记在另一个位置。
reset()
方法将当前缓冲区位置设置为该点。
count
protected int count
- 比输入缓冲区中最后有效字符大一的下标。
构造子
ByteArrayInputStream
public ByteArrayInputStream(byte buf[])
- 创建一个新字节数组输入流,它从指定字节数组中读取数据。该字节数组未被复制。
- 参数:
- buf - 输入缓冲区
ByteArrayInputStream
public ByteArrayInputStream(byte buf[],
int offset,
int length)
- 创建一个新字节数组输入流,它从指定字节数组中读取数据。达到
length
字符时将从字节数组的指示偏移量开始读取。
该字节数组未被复制。
- 参数:
- buf - 输入缓冲区
- offset - 缓冲区中将读取的第一个字节的偏移量
- len - 从缓冲区中待读取的最大字节数
方法
read
public synchronized int read()
- 从该输入流中读取下一个字节数据。返回的字节值是
0
到 255
范围内的一个 int
数。如果已读到流尾,无可读的字节时,返回 -1
。
不能阻塞 StringBufferInputStream
的 read
方法。
- 返回值:
- 下一个数据字节,若已读到流尾,则为
-1
。
- 覆盖:
- 类
InputStream 中的 read 。
read
public synchronized int read(byte b[],
int off,
int len)
- 将该输入流中
len
个字节数据读入某个字节数组中。不能阻塞该方法。
- 参数:
- b - 读取数据的缓冲区。
- off - 读取数据的起始偏移。
- len - 读取的最大字节数。
- 返回值:
- 读取缓存区的整个字节数,若读到流尾且无数据可读则为
-1
。
- 覆盖:
- 类 InputStream
中的
read 。
skip
public synchronized long skip(long n)
- 跳过输入流中
n
个字节数据。 若已读到输入流尾,则可能会跳过少数数据。
- 参数:
- n - 跳过的字节数。
- 返回值:
- 实际跳过的字节数。
- 覆盖:
- 类 InputStream 中的 skip 。
available
public synchronized int available()
- 返回可从该输入流中无阻塞读取的字节数。
ByteArrayInputStream
的可用
方法的返回值
count?- pos
,
它是从输入缓冲区待读的字节数。
- 返回值:
- 可从输入流中无阻塞读取的字节数。
- 覆盖:
- 类
InputStream 中的
available 。
markSupported
public boolean markSupported()
- 若 ByteArrayInputStream 支持标记/重置,则测试。
- 覆盖:
- 类 InputStream
中的 markSupported 。
mark
public void mark(int markpos)
- 设置流中的当前标记位置。构造时 ByteArrayInputStreams 缺省标记在零位置。用该方法可将其标记在另一个位置。
- 覆盖:
- 类
InputStream 中的
mark 。
reset
public synchronized void reset()
- 将缓冲区重置为标记的位置。除非标记了另一个位置,否则该标记位置即为开始位置。
- 覆盖:
- 类
InputStream 中的 reset 。