ByteArrayInputStream介紹
ByteArrayInputStream是字節數組輸入流,繼承於InputStream,它包含一個內部緩衝區,該緩衝區包含從流中讀取的字節,它的內部緩衝區就是一個字節數組,而ByteArrayInputStream本質就是通過字節數組來實現的。inputSteam通過read()向外提供接口,供他們來讀取字節數據,而ByteArrayInputStream的內部額外定義了一個計數器,它被用來跟蹤read()方法讀取的下一個字節
InputStream函數列表
// 構造函數
InputStream()
int available()
void close()
void mark(int readlimit)
boolean markSupported()
int read(byte[] buffer)
abstract int read()
int read(byte[] buffer, int offset, int length)
synchronized void reset()
long skip(long byteCount)
ByteArrayInputStream函數列表
// 構造函數
ByteArrayInputStream(byte[] buf)
ByteArrayInputStream(byte[] buf, int offset, int length)
synchronized int available()
void close()
synchronized void mark(int readlimit)
boolean markSupported()
synchronized int read()
synchronized int read(byte[] buffer, int offset, int length)
synchronized void reset()
synchronized long skip(long byteCount)
InputStream和ByteArrayInputStream源碼分析
InputStream源碼分析
package java.io;
public abstract class InputStream implements Closeable {
// 能skip的大小
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
// 從輸入流中讀取數據的下一個字節。
public abstract int read() throws IOException;
// 將數據從輸入流讀入 byte 數組。
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
// 將最多 len 個數據字節從此輸入流讀入 byte 數組。
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
// 跳過輸入流中的n個字節
public long skip(long n) throws IOException {
long remaining = n;
int nr;
if (n <= 0) {
return 0;
}
int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
byte[] skipBuffer = new byte[size];
while (remaining > 0) {
nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
if (nr < 0) {
break;
}
remaining -= nr;
}
return n - remaining;
}
public int available() throws IOException {
return 0;
}
public void close() throws IOException {}
public synchronized void mark(int readlimit) {}
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
public boolean markSupported() {
return false;
}
}
2ByteArrayInputStream源碼分析
package java.io;
public class ByteArrayInputStream extends InputStream {
// 保存字節輸入流數據的字節數組
protected byte buf[];
// 下一個會被讀取的字節的索引
protected int pos;
// 標記的索引
protected int mark = 0;
// 字節流的長度
protected int count;
// 構造函數:創建一個內容爲buf的字節流
public ByteArrayInputStream(byte buf[]) {
// 初始化“字節流對應的字節數組爲buf”
this.buf = buf;
// 初始化“下一個要被讀取的字節索引號爲0”
this.pos = 0;
// 初始化“字節流的長度爲buf的長度”
this.count = buf.length;
}
// 構造函數:創建一個內容爲buf的字節流,並且是從offset開始讀取數據,讀取的長度爲length
public ByteArrayInputStream(byte buf[], int offset, int length) {
// 初始化“字節流對應的字節數組爲buf”
this.buf = buf;
// 初始化“下一個要被讀取的字節索引號爲offset”
this.pos = offset;
// 初始化“字節流的長度”
this.count = Math.min(offset + length, buf.length);
// 初始化“標記的字節流讀取位置”
this.mark = offset;
}
// 讀取下一個字節
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
// 將“字節流的數據寫入到字節數組b中”
// off是“字節數組b的偏移地址”,表示從數組b的off開始寫入數據
// len是“寫入的字節長度”
public synchronized int read(byte b[], int off, int len) {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
}
if (pos >= count) {
return -1;
}
int avail = count - pos;
if (len > avail) {
len = avail;
}
if (len <= 0) {
return 0;
}
System.arraycopy(buf, pos, b, off, len);
pos += len;
return len;
}
// 跳過“字節流”中的n個字節。
public synchronized long skip(long n) {
long k = count - pos;
if (n < k) {
k = n < 0 ? 0 : n;
}
pos += k;
return k;
}
// “能否讀取字節流的下一個字節”
public synchronized int available() {
return count - pos;
}
// 是否支持“標籤”
public boolean markSupported() {
return true;
}
// 保存當前位置。readAheadLimit在此處沒有任何實際意義
public void mark(int readAheadLimit) {
mark = pos;
}
// 重置“字節流的讀取索引”爲“mark所標記的位置”
public synchronized void reset() {
pos = mark;
}
public void close() throws IOException {
}
}
說明
ByteArrayInputStream實際上是通過"字節數組"去保存數據
(01) 通過ByteArrayInputStream(byte buf[]) 或 ByteArrayInputStream(byte buf[], int offset, int length) ,我們可以根據buf數組來創建字節流對象。
(02) read()的作用是從字節流中“讀取下一個字節”。
(03) read(byte[] buffer, int offset, int length)的作用是從字節流讀取字節數據,並寫入到字節數組buffer中。offset是將字節寫入到buffer的起始位置,length是寫入的字節的長度。
(04) markSupported()是判斷字節流是否支持“標記功能”。它一直返回true。
(05) mark(int readlimit)的作用是記錄標記位置。記錄標記位置之後,某一時刻調用reset()則將“字節流下一個被讀取的位置”重置到“mark(int readlimit)所標記的位置”;也就是說,reset()之後再讀取字節流時,是從mark(int readlimit)所標記的位置開始讀取。
示例代碼
package com.tuhu.filt.javadata;
import java.io.ByteArrayInputStream;
public class ByteArrayInputStreamTest {
private static final int LEN = 5;
//對應小寫英文字母 a-z
private static final byte[] ArrayLetters = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
};
private static void tesByteArrayInputStream(){
//創建ByteArrayInputStream字節流,內容是ArrayLetters數組
ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);
//從字節流中讀取5個字節
for(int i = 0;i<LEN;i++){
//若能讀取下一個字節,則讀取下一個字節
if(bais.available() >= 0){
//讀取"字節流的下一個字節"
int tmp = bais.read();
System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));
}
}
//若"該字節流"不支持標記功能,則直接退出
if(!bais.markSupported()){
System.out.println("make not supported");
return;
}
//標記"字節流中下一個被讀取的位置"即標記"ox66"因爲前面已經讀取了5個字節,所以下一個被讀取的位置是第6個字節
//(01), ByteArrayInputStream類的mark(0)函數中的“參數0”是沒有實際意義的。
//(02), mark()與reset()是配套的,reset()會將“字節流中下一個被讀取的位置”重置爲“mark()中所保存的位置”
bais.mark(0);
//跳過5個字節後,字節流下一個被讀取的值應該是ox6B
bais.skip(5);
byte[] buf = new byte[LEN];
bais.read(buf,0,LEN);
//將buf轉換爲String 字符串 。“0x6B, 0x6C, 0x6D, 0x6E, 0x6F”對應字符是“klmno”
String str1 = new String(buf);
System.out.printf("str1=%s\n", str1);
//重置"字節流":即將"字節流中下一個被讀取的位置"重置到
//mark()所標記的位置
bais.reset();
bais.read(buf,0,LEN);
//將buf轉換爲String字符串。“0x66, 0x67, 0x68, 0x69, 0x6A”對應字符是“fghij”
String str2 = new String(buf);
System.out.printf("str2=%s\n", str2);
}
public static void main(String[] args) {
String tmp = new String(ArrayLetters);
System.out.println(
"ArrayLetters="+tmp
);
tesByteArrayInputStream();
}
}
ArrayLetters=abcdefghijklmnopqrstuvwxyz
0 : 0x61
1 : 0x62
2 : 0x63
3 : 0x64
4 : 0x65
str1=klmno
str2=fghij
結果說明:
(01) ArrayLetters 是字節數組。0x61對應的ASCII碼值是a,0x62對應的ASCII碼值是b,依次類推...
(02) ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters); 這句話是創建“字節流bais”,它的內容就是ArrayLetters。
(03) for (int i=0; i<LEN; i++) ; 這個for循環的作用就是從字節流中讀取5個字節。每次調用bais.read()就從字節流中讀取一個字節。
(04) bais.mark(0); 這句話就是“設置字節流的標記”,此時標記的位置對應的值是0x66。
(05) bais.skip(5); 這句話是跳過5個字節。跳過5個字節後,對應的字節流中下一個被讀取的字節的值是0x6B。
(06) bais.read(buf, 0, LEN); 這句話是“從字節流中讀取LEN個數據寫入到buf中,0表示從buf的第0個位置開始寫入”。
(07) bais.reset(); 這句話是將“字節流中下一個被讀取的位置”重置到“mark()所標記的位置”,即0x66。