BytearrayinputStream 소개
BytearRayInputStream은 바이트 배열 입력 스트림입니다. 입력 스트림에서 물려받습니다.
스트림에서 읽은 바이트를 포함하는 내부 버퍼가 포함되어 있습니다. Layman의 용어로는 내부 버퍼는 바이트 배열이며 BytearrayinputStream은 본질적으로 바이트 배열을 통해 구현됩니다.
우리는 입력 스트림이 읽기 ()를 통해 바이트 데이터를 읽기 위해 바깥쪽으로 인터페이스를 제공한다는 것을 알고 있습니다. BytearrayInputStream은 내부적으로 read () 메소드에 의해 읽을 다음 바이트를 추적하는 데 사용되는 추가 카운터를 내부적으로 정의합니다.
BytearRayInputStream에서 API의 자세한 사용을위한 샘플 코드 샘플 코드 (bytearrayInputStreamTest.java)를 참조하십시오.
import java.io.bytearrayinputStream; import java.io.bytearrayoutputStream;/*** BytearRayinputStream 테스트 프로그램*/public class bytearrayinputStreamtest {private static final int len = 5; // 영어 문자 "ABCDDEFGHIJKLMMNOPQRSSTTUVWXYZ"개인 정적 최종 바이트 [] ARRAYLETTERS = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6B, 0x6C, 0x6C, 0x6C 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 테스트 함수 * / 개인 정적 무효 TESBYTERRAYINPUTSTREAM () {// BYTEARRAYINPUTSTREAM BYTE 스트림 생성 컨텐츠는 배열 배열 BYTEARRAYINPUTSTREAM BAIS = NEW BYTEARRAYINPUTSTREAM (New ByTearRayInputStream); // (int i = 0; i <len; i ++) {// 다음 바이트를 계속 읽을 수 있다면 다음 바이트를 읽을 수 있다면 (bais.available ()> = 0) {// "byte 스트림의 다음 바이트"int tmp = bais.read (); System.out.printf ( "%d : 0x%s/n", i, integer.tohexstring (tmp)); }} // "이 바이트 스트림"이 마킹 함수를 지원하지 않는 경우 (! bais.marksupported ()) {System.out.println ( "지원되지 않음!"); 반품 ; } // "바이트 스트림의 다음 읽기 위치"를 표시합니다. 즉, 마크 "0x66"은 5 바이트가 이전에 읽었 기 때문에, 다음 읽기 위치는 6 번째 바이트 "// (01),"매개 변수 0 ", BytearRayinputStream 클래스의 Mark (0) 함수의"매개 변수 0 "입니다. // (02), Mark (), Reset ()는"다음 판결 () "의 Will will will in the the the the will will will will will will will will will will will will will will reset (). bais.mark (0); BAIS.READ (BUF, 0, LEN); 바이트 스트림 "은"Mark ()로 표시된 위치 ", 즉 0x66. Bais.reset (); //"0x66, 0x67, 0x68, 0x69, 0x6a "Bais.read (Buf, 0, Len)를 읽습니다. // buf를 문자열로 변환합니다. "0x66, 0x67, 0x68, 0x69, 0x6a"의 해당 문자는 "fghij"str2 = new String (buf)이며; System.out.printf ( "str2 =%s/n", str2); }} 실행 결과 :
Arrayletters = abcdefghijklmnopqrstuvwxyz0 : 0x622 : 0x633 : 0x644 : 0x65str1 = klmnostr2 = fghij
결과 설명 :
(01) Arrayletters는 바이트 어레이입니다. 0x61에 해당하는 ASCII 코드 값은 A이고, 0x62에 해당하는 ASCII 코드 값은 B입니다.
(02) BytearrayinputStream Bais = 새로운 BytearrayinputStream (Arrayletters); 이 문장은 "바이트 스트림 Bais"를 작성하는 것이며 그 내용은 배열입니다.
(int i = 0; i <len; i ++)에 대한 (03); 루프의 목적은 바이트 스트림에서 5 바이트를 읽는 것입니다. bais.read ()가 호출 될 때마다 1 바이트는 바이트 스트림에서 읽습니다.
(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은 바이트 배열 출력 스트림입니다. 출력 스트림에서 상속됩니다.
BytearRayoutputStream의 데이터는 바이트 어레이에 기록됩니다. 데이터가 지속적으로 작성됨에 따라 버퍼가 자동으로 증가합니다. 데이터는 TobyTearRay () 및 ToString ()을 사용하여 얻을 수 있습니다.
BYTEARRAYOUTPUTSTREAM에서 API의 자세한 사용을위한 샘플 코드 샘플 코드 (bytearRayOutputStreamTest.java)를 참조하십시오.
import java.io.ioexception; import java.io.outputStream; import java.io.bytearrayoutputStream; import java.io.byTearRayInputStream;/** * ByTearRayoutputStream 테스트 프로그램 * * @Author SkyWang */public class bytearrayoutputStreamtest {private intatic int int len = 5; // 영어 문자 "abcddefghijklmnopqrsttuvwxyz"개인 정적 최종 바이트 [] arrayletters = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6e, 0x6o 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 테스트 함수 * / 개인 정적 무효화 tesbyTearRayoutputStream () {// bytearRayoutputStream 바이트 스트림 ByTearRayOutputStream Baos = new ByTearRayoutputStream (); // 3 글자 "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); // 배열 배열에서 "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); // 바이트로 변환 [] 배열 바이트 [] buf = baos.tobytearRay (); 문자열 str = 새 문자열 (buf); System.out.printf ( "str =%s/n", str); // 바이트로 변환 [] 배열 바이트 [] buf = baos.tobytearRay (); 문자열 str = 새 문자열 (buf); System.out.printf ( "str =%s/n", str); // 다른 출력 스트림에 bao를 쓰기 {bytearRayoutputStream baos2 = new BytearRayoutputStream (); baos.writeto ((outputstream) baos2); System.out.printf ( "baos2 =%s/n", baos2); } catch (ioexception e) {e.printstacktrace (); }}} 실행 결과 :
baos = abcbaos = abcdefghsize = 8st = abcdefghbaos2 = abcdefgh