public class ByteArrayInputStream
extends InputStream
| java.lang.Object | ||
| java.io.InputStream | ||
| java.io.ByteArrayInputStream | ||
A ByteArrayInputStream包含一个内部缓冲区,其中包含可能从流中读取的字节。 一个内部计数器跟踪由read方法提供的下一个字节。
关闭ByteArrayInputStream不起作用。 在关闭流之后可以调用此类中的方法,而不生成IOException 。
也可以看看:
Fields |
|
|---|---|
protected byte[] |
buf 流的创建者提供的字节数组。 |
protected int |
count 索引1大于输入流缓冲区中最后一个有效字符。 |
protected int |
mark 流中当前标记的位置。 |
protected int |
pos 从输入流缓冲区读取的下一个字符的索引。 |
Public constructors |
|
|---|---|
ByteArrayInputStream(byte[] buf) 创建一个 |
|
ByteArrayInputStream(byte[] buf, int offset, int length) 创建 |
|
公共方法(Public methods) |
|
|---|---|
int |
available() 返回可从此输入流中读取(或跳过)的剩余字节数。 |
void |
close() 关闭 ByteArrayInputStream不起作用。 |
void |
mark(int readAheadLimit) 设置流中当前标记的位置。 |
boolean |
markSupported() 测试此 |
int |
read() 从此输入流中读取下一个字节的数据。 |
int |
read(byte[] b, int off, int len) 最多可将 |
void |
reset() 将缓冲区重置到标记的位置。 |
long |
skip(long n) 从此输入流跳过 |
继承方法(Inherited methods) |
|
|---|---|
java.io.InputStream
|
|
java.lang.Object
|
|
java.io.Closeable
|
|
java.lang.AutoCloseable
|
|
byte[] buf
流的创建者提供的字节数组。 元素buf[0]到buf[count-1]是唯一可以从流中读取的字节; 元素buf[pos]是要读取的下一个字节。
int count
索引1大于输入流缓冲区中最后一个有效字符。 此值应始终为非负值,并且不得超过buf的长度。 它比可以从输入流缓冲区读取的buf内最后一个字节的位置大1。
int mark
流中当前标记的位置。 构造时,ByteArrayInputStream对象在默认位置被标记为零。 它们可能会被mark()方法标记在缓冲区内的另一个位置。 当前的缓冲区位置由reset()方法设置为该点。
如果没有标记被设置,则mark的值是传递给构造函数的偏移量(如果未提供偏移量,则为0)。
int pos
从输入流缓冲区读取的下一个字符的索引。 该值应始终为非负值,且不得大于count的值。 从输入流缓冲区读取的下一个字节将是buf[pos] 。
ByteArrayInputStream (byte[] buf)
创建一个ByteArrayInputStream以便它使用buf作为其缓冲区阵列。 缓冲区数组不被复制。 初始值pos为0 ,初始值为count ,长度为buf 。
| 参数(Parameters) | |
|---|---|
buf |
byte: the input buffer. |
ByteArrayInputStream (byte[] buf,
int offset,
int length)
创建ByteArrayInputStream使用buf作为其缓冲器阵列。 的初始值pos是offset和的初始值count是最小offset+length和buf.length 。 缓冲区数组不被复制。 缓冲区的标记被设置为指定的偏移量。
| 参数(Parameters) | |
|---|---|
buf |
byte: the input buffer. |
offset |
int: the offset in the buffer of the first byte to read. |
length |
int: the maximum number of bytes to read from the buffer. |
int available ()
返回可从此输入流中读取(或跳过)的剩余字节数。
返回的值是 count - pos ,这是从输入缓冲区中剩余的剩余字节数。
| 返回(Returns) | |
|---|---|
int |
the number of remaining bytes that can be read (or skipped over) from this input stream without blocking. |
void close ()
关闭ByteArrayInputStream不起作用。 在关闭流之后可以调用此类中的方法,而不生成IOException 。
| 抛出异常(Throws) | |
|---|---|
IOException |
|
void mark (int readAheadLimit)
设置流中当前标记的位置。 构造时,ByteArrayInputStream对象在默认位置被标记为零。 这种方法可能会将其标记在缓冲区内的另一个位置。
如果没有标记被设置,那么标记的值是传递给构造函数的偏移量(如果未提供偏移量,则为0)。
注意:这个类的 readAheadLimit没有意义。
| 参数(Parameters) | |
|---|---|
readAheadLimit |
int: the maximum limit of bytes that can be read before the mark position becomes invalid. |
boolean markSupported ()
测试此InputStream支持标记/重置。 该markSupported的方法ByteArrayInputStream总是返回true 。
| 返回(Returns) | |
|---|---|
boolean |
true if this stream instance supports the mark and reset methods; false otherwise. |
int read ()
从此输入流中读取下一个字节的数据。 值字节被返回作为int范围0到255 。 如果由于已到达流的末尾而没有字节可用,则返回值-1 。
这个 read方法不能阻塞。
| 返回(Returns) | |
|---|---|
int |
the next byte of data, or -1 if the end of the stream has been reached. |
int read (byte[] b,
int off,
int len)
最多可将len个字节的数据读入此输入流中的一个字节数组。 如果pos等于count ,则返回-1以指示文件结束。 否则,读取的字节数k等于len和count-pos的较小者。 如果k为正,则字节buf[pos]通过buf[pos+k-1]被复制到b[off]通过b[off+k-1]中所执行的方式System.arraycopy 。 值k被添加到pos和k被返回。
这 read方法不能阻止。
| 参数(Parameters) | |
|---|---|
b |
byte: the buffer into which the data is read. |
off |
int: the start offset in the destination array b |
len |
int: the maximum number of bytes read. |
| 返回(Returns) | |
|---|---|
int |
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
If b is null. |
IndexOutOfBoundsException |
If off is negative, len is negative, or len is greater than b.length - off |
long skip (long n)
从此输入流跳过n个字节的输入。 如果到达输入流的末尾,则可以跳过更少的字节。 要跳过的字节的实际数量k等于n和count-pos的较小者。 将值k添加到pos并返回k 。
| 参数(Parameters) | |
|---|---|
n |
long: the number of bytes to be skipped. |
| 返回(Returns) | |
|---|---|
long |
the actual number of bytes skipped. |