詳解Java中ByteArray字節(jié)數(shù)組的輸入輸出流的用法
ByteArrayInputStream 介紹
ByteArrayInputStream 是字節(jié)數(shù)組輸入流。它繼承于InputStream。
它包含一個(gè)內(nèi)部緩沖區(qū),該緩沖區(qū)包含從流中讀取的字節(jié);通俗點(diǎn)說(shuō),它的內(nèi)部緩沖區(qū)就是一個(gè)字節(jié)數(shù)組,而B(niǎo)yteArrayInputStream本質(zhì)就是通過(guò)字節(jié)數(shù)組來(lái)實(shí)現(xiàn)的。
我們都知道,InputStream通過(guò)read()向外提供接口,供它們來(lái)讀取字節(jié)數(shù)據(jù);而B(niǎo)yteArrayInputStream 的內(nèi)部額外的定義了一個(gè)計(jì)數(shù)器,它被用來(lái)跟蹤 read() 方法要讀取的下一個(gè)字節(jié)。
示例代碼
關(guān)于ByteArrayInputStream中API的詳細(xì)用法,參考示例代碼(ByteArrayInputStreamTest.java):
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; /** * ByteArrayInputStream 測(cè)試程序 */ public class ByteArrayInputStreamTest { private static final int LEN = 5; // 對(duì)應(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測(cè)試函數(shù) */ private static void tesByteArrayInputStream() { // 創(chuàng)建ByteArrayInputStream字節(jié)流,內(nèi)容是ArrayLetters數(shù)組 ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters); // 從字節(jié)流中讀取5個(gè)字節(jié) for (int i=0; i<LEN; i++) { // 若能繼續(xù)讀取下一個(gè)字節(jié),則讀取下一個(gè)字節(jié) if (bais.available() >= 0) { // 讀取“字節(jié)流的下一個(gè)字節(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é)流中下一個(gè)被讀取的位置”。即--標(biāo)記“0x66”,因?yàn)橐驗(yàn)榍懊嬉呀?jīng)讀取了5個(gè)字節(jié),所以下一個(gè)被讀取的位置是第6個(gè)字節(jié)” // (01), ByteArrayInputStream類的mark(0)函數(shù)中的“參數(shù)0”是沒(méi)有實(shí)際意義的。 // (02), mark()與reset()是配套的,reset()會(huì)將“字節(jié)流中下一個(gè)被讀取的位置”重置為“mark()中所保存的位置” bais.mark(0); // 跳過(guò)5個(gè)字節(jié)。跳過(guò)5個(gè)字節(jié)后,字節(jié)流中下一個(gè)被讀取的值應(yīng)該是“0x6B”。 bais.skip(5); // 從字節(jié)流中讀取5個(gè)數(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”對(duì)應(yīng)字符是“klmno” String str1 = new String(buf); System.out.printf("str1=%s\n", str1); // 重置“字節(jié)流”:即,將“字節(jié)流中下一個(gè)被讀取的位置”重置到“mark()所標(biāo)記的位置”,即0x66。 bais.reset(); // 從“重置后的字節(jié)流”中讀取5個(gè)字節(jié)到buf中。即讀取“0x66, 0x67, 0x68, 0x69, 0x6A” bais.read(buf, 0, LEN); // 將buf轉(zhuǎn)換為String字符串?!?x66, 0x67, 0x68, 0x69, 0x6A”對(duì)應(yīng)字符是“fghij” String str2 = new String(buf); System.out.printf("str2=%s\n", str2); } }
運(yùn)行結(jié)果:
ArrayLetters=abcdefghijklmnopqrstuvwxyz 0 : 0x61 1 : 0x62 2 : 0x63 3 : 0x64 4 : 0x65 str1=klmno str2=fghij
結(jié)果說(shuō)明:
(01) ArrayLetters 是字節(jié)數(shù)組。0x61對(duì)應(yīng)的ASCII碼值是a,0x62對(duì)應(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++) ; 這個(gè)for循環(huán)的作用就是從字節(jié)流中讀取5個(gè)字節(jié)。每次調(diào)用bais.read()就從字節(jié)流中讀取一個(gè)字節(jié)。
(04) bais.mark(0); 這句話就是“設(shè)置字節(jié)流的標(biāo)記”,此時(shí)標(biāo)記的位置對(duì)應(yīng)的值是0x66。
(05) bais.skip(5); 這句話是跳過(guò)5個(gè)字節(jié)。跳過(guò)5個(gè)字節(jié)后,對(duì)應(yīng)的字節(jié)流中下一個(gè)被讀取的字節(jié)的值是0x6B。
(06) bais.read(buf, 0, LEN); 這句話是“從字節(jié)流中讀取LEN個(gè)數(shù)據(jù)寫入到buf中,0表示從buf的第0個(gè)位置開(kāi)始寫入”。
(07) bais.reset(); 這句話是將“字節(jié)流中下一個(gè)被讀取的位置”重置到“mark()所標(biāo)記的位置”,即0x66。
學(xué)完了ByteArrayInputStream輸入流。下面,我們學(xué)習(xí)與之對(duì)應(yīng)的輸出流ByteArrayOutputStream。
ByteArrayOutputStream 介紹
ByteArrayOutputStream 是字節(jié)數(shù)組輸出流。它繼承于OutputStream。
ByteArrayOutputStream 中的數(shù)據(jù)被寫入一個(gè) byte 數(shù)組。緩沖區(qū)會(huì)隨著數(shù)據(jù)的不斷寫入而自動(dòng)增長(zhǎng)??墒褂?toByteArray() 和 toString() 獲取數(shù)據(jù)。
示例代碼
關(guān)于ByteArrayOutputStream中API的詳細(xì)用法,參考示例代碼(ByteArrayOutputStreamTest.java):
import java.io.IOException; import java.io.OutputStream; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; /** * ByteArrayOutputStream 測(cè)試程序 * * @author skywang */ public class ByteArrayOutputStreamTest { private static final int LEN = 5; // 對(duì)應(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); tesByteArrayOutputStream() ; } /** * ByteArrayOutputStream的API測(cè)試函數(shù) */ private static void tesByteArrayOutputStream() { // 創(chuàng)建ByteArrayOutputStream字節(jié)流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 依次寫入“A”、“B”、“C”三個(gè)字母。0x41對(duì)應(yīng)A,0x42對(duì)應(yīng)B,0x43對(duì)應(yīng)C。 baos.write(0x41); baos.write(0x42); baos.write(0x43); System.out.printf("baos=%s\n", baos); // 將ArrayLetters數(shù)組中從“3”開(kāi)始的后5個(gè)字節(jié)寫入到baos中。 // 即對(duì)應(yīng)寫入“0x64, 0x65, 0x66, 0x67, 0x68”,即“defgh” baos.write(ArrayLetters, 3, 5); System.out.printf("baos=%s\n", baos); // 計(jì)算長(zhǎng)度 int size = baos.size(); System.out.printf("size=%s\n", size); // 轉(zhuǎn)換成byte[]數(shù)組 byte[] buf = baos.toByteArray(); String str = new String(buf); System.out.printf("str=%s\n", str); // 將baos寫入到另一個(gè)輸出流中 try { ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); baos.writeTo((OutputStream)baos2); System.out.printf("baos2=%s\n", baos2); } catch (IOException e) { e.printStackTrace(); } } }
運(yùn)行結(jié)果:
baos=ABC baos=ABCdefgh size=8 str=ABCdefgh baos2=ABCdefgh
相關(guān)文章
關(guān)于SpringBoot創(chuàng)建存儲(chǔ)令牌的媒介類和過(guò)濾器的問(wèn)題
這篇文章主要介紹了SpringBoot創(chuàng)建存儲(chǔ)令牌的媒介類和過(guò)濾器的問(wèn)題,需要在配置文件中,添加JWT需要的密匙,過(guò)期時(shí)間和緩存過(guò)期時(shí)間,具體實(shí)例代碼參考下本文2021-09-09詳解rabbitmq創(chuàng)建queue時(shí)arguments參數(shù)注釋
這篇文章主要介紹了rabbitmq創(chuàng)建queue時(shí)arguments參數(shù)注釋,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03mybatisPlus條件構(gòu)造器常用方法小結(jié)
這篇文章主要介紹了mybatisPlus條件構(gòu)造器常用方法,首先是.select和其他條件,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10SpringBoot通知機(jī)制的實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot通知機(jī)制的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07