ByteArrayInputStream 介紹
ByteArrayInputStream 是字節數組輸入流。它繼承于InputStream。
它包含一個內部緩沖區,該緩沖區包含從流中讀取的字節;通俗點說,它的內部緩沖區就是一個字節數組,而ByteArrayInputStream本質就是通過字節數組來實現的。
我們都知道,InputStream通過read()向外提供接口,供它們來讀取字節數據;而ByteArrayInputStream 的內部額外的定義了一個計數器,它被用來跟蹤 read() 方法要讀取的下一個字節。
示例代碼
關于ByteArrayInputStream中API的詳細用法,參考示例代碼(ByteArrayInputStreamTest.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; /** * ByteArrayInputStream 測試程序 */ public class ByteArrayInputStreamTest { private static final int LEN = 5 ; // 對應英文字母“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測試函數 */ 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 ; } // 標記“字節流中下一個被讀取的位置”。即--標記“0x66”,因為因為前面已經讀取了5個字節,所以下一個被讀取的位置是第6個字節” // (01), ByteArrayInputStream類的mark(0)函數中的“參數0”是沒有實際意義的。 // (02), mark()與reset()是配套的,reset()會將“字節流中下一個被讀取的位置”重置為“mark()中所保存的位置” bais.mark( 0 ); // 跳過5個字節。跳過5個字節后,字節流中下一個被讀取的值應該是“0x6B”。 bais.skip( 5 ); // 從字節流中讀取5個數據。即讀取“0x6B, 0x6C, 0x6D, 0x6E, 0x6F” 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()所標記的位置”,即0x66。 bais.reset(); // 從“重置后的字節流”中讀取5個字節到buf中。即讀取“0x66, 0x67, 0x68, 0x69, 0x6A” 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); } } |
運行結果:
1
2
3
4
5
6
7
8
|
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。
學完了ByteArrayInputStream輸入流。下面,我們學習與之對應的輸出流ByteArrayOutputStream。
ByteArrayOutputStream 介紹
ByteArrayOutputStream 是字節數組輸出流。它繼承于OutputStream。
ByteArrayOutputStream 中的數據被寫入一個 byte 數組。緩沖區會隨著數據的不斷寫入而自動增長。可使用 toByteArray() 和 toString() 獲取數據。
示例代碼
關于ByteArrayOutputStream中API的詳細用法,參考示例代碼(ByteArrayOutputStreamTest.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import java.io.IOException; import java.io.OutputStream; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; /** * ByteArrayOutputStream 測試程序 * * @author skywang */ public class ByteArrayOutputStreamTest { private static final int LEN = 5 ; // 對應英文字母“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); tesByteArrayOutputStream() ; } /** * ByteArrayOutputStream的API測試函數 */ private static void tesByteArrayOutputStream() { // 創建ByteArrayOutputStream字節流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 依次寫入“A”、“B”、“C”三個字母。0x41對應A,0x42對應B,0x43對應C。 baos.write( 0x41 ); baos.write( 0x42 ); baos.write( 0x43 ); System.out.printf( "baos=%s\n" , baos); // 將ArrayLetters數組中從“3”開始的后5個字節寫入到baos中。 // 即對應寫入“0x64, 0x65, 0x66, 0x67, 0x68”,即“defgh” baos.write(ArrayLetters, 3 , 5 ); System.out.printf( "baos=%s\n" , baos); // 計算長度 int size = baos.size(); System.out.printf( "size=%s\n" , size); // 轉換成byte[]數組 byte [] buf = baos.toByteArray(); String str = new String(buf); System.out.printf( "str=%s\n" , str); // 將baos寫入到另一個輸出流中 try { ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); baos.writeTo((OutputStream)baos2); System.out.printf( "baos2=%s\n" , baos2); } catch (IOException e) { e.printStackTrace(); } } } |
運行結果:
1
2
3
4
5
|
baos=ABC baos=ABCdefgh size=8 str=ABCdefgh baos2=ABCdefgh |