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)); } }