ByteArrayInputStream簡介和使用_動力節(jié)點Java學(xué)院整理
ByteArrayInputStream 介紹
ByteArrayInputStream 是字節(jié)數(shù)組輸入流。它繼承于InputStream。
它包含一個內(nèi)部緩沖區(qū),該緩沖區(qū)包含從流中讀取的字節(jié);通俗點說,它的內(nèi)部緩沖區(qū)就是一個字節(jié)數(shù)組,而ByteArrayInputStream本質(zhì)就是通過字節(jié)數(shù)組來實現(xiàn)的。
我們都知道,InputStream通過read()向外提供接口,供它們來讀取字節(jié)數(shù)據(jù);而ByteArrayInputStream 的內(nèi)部額外的定義了一個計數(shù)器,它被用來跟蹤 read() 方法要讀取的下一個字節(jié)。
InputStream 函數(shù)列表
// 構(gòu)造函數(shù)
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 函數(shù)列表
// 構(gòu)造函數(shù)
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是ByteArrayInputStream的父類,我們先看看InputStream的源碼,然后再學(xué)ByteArrayInputStream的源碼。
1. InputStream.java源碼分析(基于jdk1.7.40)
package java.io;
public abstract class InputStream implements Closeable {
// 能skip的大小
private static final int MAX_SKIP_BUFFER_SIZE = ;
// 從輸入流中讀取數(shù)據(jù)的下一個字節(jié)。
public abstract int read() throws IOException;
// 將數(shù)據(jù)從輸入流讀入 byte 數(shù)組。
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
// 將最多 len 個數(shù)據(jù)字節(jié)從此輸入流讀入 byte 數(shù)組。
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 == -) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
// 跳過輸入流中的n個字節(jié)
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;
}
}
2. ByteArrayInputStream.java源碼分析(基于jdk1.7.40)
package java.io;
public class ByteArrayInputStream extends InputStream {
// 保存字節(jié)輸入流數(shù)據(jù)的字節(jié)數(shù)組
protected byte buf[];
// 下一個會被讀取的字節(jié)的索引
protected int pos;
// 標(biāo)記的索引
protected int mark = 0;
// 字節(jié)流的長度
protected int count;
// 構(gòu)造函數(shù):創(chuàng)建一個內(nèi)容為buf的字節(jié)流
public ByteArrayInputStream(byte buf[]) {
// 初始化“字節(jié)流對應(yīng)的字節(jié)數(shù)組為buf”
this.buf = buf;
// 初始化“下一個要被讀取的字節(jié)索引號為”
this.pos = ;
// 初始化“字節(jié)流的長度為buf的長度”
this.count = buf.length;
}
// 構(gòu)造函數(shù):創(chuàng)建一個內(nèi)容為buf的字節(jié)流,并且是從offset開始讀取數(shù)據(jù),讀取的長度為length
public ByteArrayInputStream(byte buf[], int offset, int length) {
// 初始化“字節(jié)流對應(yīng)的字節(jié)數(shù)組為buf”
this.buf = buf;
// 初始化“下一個要被讀取的字節(jié)索引號為offset”
this.pos = offset;
// 初始化“字節(jié)流的長度”
this.count = Math.min(offset + length, buf.length);
// 初始化“標(biāo)記的字節(jié)流讀取位置”
this.mark = offset;
}
// 讀取下一個字節(jié)
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
// 將“字節(jié)流的數(shù)據(jù)寫入到字節(jié)數(shù)組b中”
// off是“字節(jié)數(shù)組b的偏移地址”,表示從數(shù)組b的off開始寫入數(shù)據(jù)
// len是“寫入的字節(jié)長度”
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;
}
// 跳過“字節(jié)流”中的n個字節(jié)。
public synchronized long skip(long n) {
long k = count - pos;
if (n < k) {
k = n < 0 ? 0 : n;
}
pos += k;
return k;
}
// “能否讀取字節(jié)流的下一個字節(jié)”
public synchronized int available() {
return count - pos;
}
// 是否支持“標(biāo)簽”
public boolean markSupported() {
return true;
}
// 保存當(dāng)前位置。readAheadLimit在此處沒有任何實際意義
public void mark(int readAheadLimit) {
mark = pos;
}
// 重置“字節(jié)流的讀取索引”為“mark所標(biāo)記的位置”
public synchronized void reset() {
pos = mark;
}
public void close() throws IOException {
}
}
說明:
ByteArrayInputStream實際上是通過“字節(jié)數(shù)組”去保存數(shù)據(jù)。
(01) 通過ByteArrayInputStream(byte buf[]) 或 ByteArrayInputStream(byte buf[], int offset, int length) ,我們可以根據(jù)buf數(shù)組來創(chuàng)建字節(jié)流對象。
(02) read()的作用是從字節(jié)流中“讀取下一個字節(jié)”。
(03) read(byte[] buffer, int offset, int length)的作用是從字節(jié)流讀取字節(jié)數(shù)據(jù),并寫入到字節(jié)數(shù)組buffer中。offset是將字節(jié)寫入到buffer的起始位置,length是寫入的字節(jié)的長度。
(04) markSupported()是判斷字節(jié)流是否支持“標(biāo)記功能”。它一直返回true。
(05) mark(int readlimit)的作用是記錄標(biāo)記位置。記錄標(biāo)記位置之后,某一時刻調(diào)用reset()則將“字節(jié)流下一個被讀取的位置”重置到“mark(int readlimit)所標(biāo)記的位置”;也就是說,reset()之后再讀取字節(jié)流時,是從mark(int readlimit)所標(biāo)記的位置開始讀取。
示例代碼
關(guān)于ByteArrayInputStream中API的詳細用法,參考示例代碼(ByteArrayInputStreamTest.java):
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* ByteArrayInputStream 測試程序
*
*/
public class ByteArrayInputStreamTest {
private static final int LEN = 5;
// 對應(yīng)英文字母“abcddefghijklmnopqrsttuvwxyz”
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
};
public static void main(String[] args) {
String tmp = new String(ArrayLetters);
System.out.println("ArrayLetters="+tmp);
tesByteArrayInputStream() ;
}
/**
* ByteArrayInputStream的API測試函數(shù)
*/
private static void tesByteArrayInputStream() {
// 創(chuàng)建ByteArrayInputStream字節(jié)流,內(nèi)容是ArrayLetters數(shù)組
ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);
// 從字節(jié)流中讀取5個字節(jié)
for (int i=0; i<LEN; i++) {
// 若能繼續(xù)讀取下一個字節(jié),則讀取下一個字節(jié)
if (bais.available() >= 0) {
// 讀取“字節(jié)流的下一個字節(jié)”
int tmp = bais.read();
System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));
}
}
// 若“該字節(jié)流”不支持標(biāo)記功能,則直接退出
if (!bais.markSupported()) {
System.out.println("make not supported!");
return ;
}
// 標(biāo)記“字節(jié)流中下一個被讀取的位置”。即--標(biāo)記“0x66”,因為因為前面已經(jīng)讀取了5個字節(jié),所以下一個被讀取的位置是第6個字節(jié)”
// (01), ByteArrayInputStream類的mark(0)函數(shù)中的“參數(shù)0”是沒有實際意義的。
// (02), mark()與reset()是配套的,reset()會將“字節(jié)流中下一個被讀取的位置”重置為“mark()中所保存的位置”
bais.mark(0);
// 跳過5個字節(jié)。跳過5個字節(jié)后,字節(jié)流中下一個被讀取的值應(yīng)該是“0x6B”。
bais.skip(5);
// 從字節(jié)流中讀取5個數(shù)據(jù)。即讀取“0x6B, 0x6C, 0x6D, 0x6E, 0x6F”
byte[] buf = new byte[LEN];
bais.read(buf, 0, LEN);
// 將buf轉(zhuǎn)換為String字符串?!?x6B, 0x6C, 0x6D, 0x6E, 0x6F”對應(yīng)字符是“klmno”
String str1 = new String(buf);
System.out.printf("str1=%s\n", str1);
// 重置“字節(jié)流”:即,將“字節(jié)流中下一個被讀取的位置”重置到“mark()所標(biāo)記的位置”,即0x66。
bais.reset();
// 從“重置后的字節(jié)流”中讀取5個字節(jié)到buf中。即讀取“0x66, 0x67, 0x68, 0x69, 0x6A”
bais.read(buf, 0, LEN);
// 將buf轉(zhuǎn)換為String字符串?!?x66, 0x67, 0x68, 0x69, 0x6A”對應(yīng)字符是“fghij”
String str2 = new String(buf);
System.out.printf("str2=%s\n", str2);
}
}
運行結(jié)果:
ArrayLetters=abcdefghijklmnopqrstuvwxyz 0 : 0x61 1 : 0x62 2 : 0x63 3 : 0x64 4 : 0x65 str1=klmno str2=fghij
結(jié)果說明:
(01) ArrayLetters 是字節(jié)數(shù)組。0x61對應(yīng)的ASCII碼值是a,0x62對應(yīng)的ASCII碼值是b,依次類推...
(02) ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters); 這句話是創(chuàng)建“字節(jié)流bais”,它的內(nèi)容就是ArrayLetters。
(03) for (int i=0; i<LEN; i++) ; 這個for循環(huán)的作用就是從字節(jié)流中讀取5個字節(jié)。每次調(diào)用bais.read()就從字節(jié)流中讀取一個字節(jié)。
(04) bais.mark(0); 這句話就是“設(shè)置字節(jié)流的標(biāo)記”,此時標(biāo)記的位置對應(yīng)的值是0x66。
(05) bais.skip(5); 這句話是跳過5個字節(jié)。跳過5個字節(jié)后,對應(yīng)的字節(jié)流中下一個被讀取的字節(jié)的值是0x6B。
(06) bais.read(buf, 0, LEN); 這句話是“從字節(jié)流中讀取LEN個數(shù)據(jù)寫入到buf中,0表示從buf的第0個位置開始寫入”。
(07) bais.reset(); 這句話是將“字節(jié)流中下一個被讀取的位置”重置到“mark()所標(biāo)記的位置”,即0x66。
學(xué)完了ByteArrayInputStream輸入流。下面,我們學(xué)習(xí)與之對應(yīng)的輸出流ByteArrayOutputStream。
以上所述是小編給大家介紹的ByteArrayInputStream簡介和使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

