ByteArrayInputStream Introduction
ByteArrayInputStream is a byte array input stream. It inherits from InputStream.
It contains an internal buffer that contains bytes read from the stream; in layman's terms, its internal buffer is an array of bytes, and ByteArrayInputStream is essentially implemented through an array of bytes.
We all know that InputStream provides interfaces outward through read() for them to read byte data; while ByteArrayInputStream internally defines an additional counter that is used to track the next byte to be read by the read() method.
Sample code For detailed usage of API in ByteArrayInputStream, refer to the sample code (ByteArrayInputStreamTest.java):
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;/** * ByteArrayInputStream Test program*/public class ByteArrayInputStreamTest { private static final int LEN = 5; // Corresponding to the English letter "abcddefghijklmnopqrssttuvwxyz" 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 test function */ private static void tesByteArrayInputStream() { // Create ByteArrayInputStream byte stream, the content is ArrayLetters array ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters); // Read 5 bytes from the byte stream for (int i=0; i<LEN; i++) { // If you can continue to read the next byte, read the next byte if (bais.available() >= 0) { // Read "Next byte of the byte stream" int tmp = bais.read(); System.out.printf("%d : 0x%s/n", i, Integer.toHexString(tmp)); } } // If "this byte stream" does not support the marking function, then exit directly if (!bais.markSupported()) { System.out.println("make not supported!"); return ; } // Mark "the next read position in the byte stream". That is, -- mark "0x66", because 5 bytes have been read before, the next read position is the 6th byte" // (01), "parameter 0" in the mark(0) function of ByteArrayInputStream class has no practical significance. // (02), mark() and reset() are matching, reset() will reset "the next read position in the byte stream" to "the saved position in the mark()" bais.mark(0); // Skip 5 bytes. After skipping 5 bytes, the next read value in the byte stream should be "0x6B". bais.skip(5); // Read 5 data from the byte stream. That is, read "0x6B, 0x6C, 0x6D, 0x6E, 0x6F" byte[] buf = new byte[LEN]; bais.read(buf, 0, LEN); // Convert buf to a String string. The corresponding character "0x6B, 0x6C, 0x6D, 0x6E, 0x6F" is "klmno" String str1 = new String(buf); System.out.printf("str1=%s/n", str1); // Reset "Byte Stream": that is, reset "the next read position in the byte stream" to "the position marked by mark()", that is, 0x66. bais.reset(); // Read 5 bytes from the "Reset Byte Stream" into the buf. That is, read "0x66, 0x67, 0x68, 0x69, 0x6A" bais.read(buf, 0, LEN); // Convert buf to a String string. The corresponding character of "0x66, 0x67, 0x68, 0x69, 0x6A" is "fghij" String str2 = new String(buf); System.out.printf("str2=%s/n", str2); }} Running results:
ArrayLetters=abcdefghijklmnopqrstuvwxyz0 : 0x611 : 0x622 : 0x633 : 0x644 : 0x65str1=klmnostr2=fghij
Results description:
(01) ArrayLetters is a byte array. The ASCII code value corresponding to 0x61 is a, the ASCII code value corresponding to 0x62 is b, and so on...
(02) ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters); This sentence is to create "byte stream bais", and its content is ArrayLetters.
(03) for (int i=0; i<LEN; i++); The purpose of this for loop is to read 5 bytes from the byte stream. Each time bais.read() is called, one byte is read from the byte stream.
(04) bais.mark(0); This sentence is "set the mark of the byte stream". At this time, the corresponding value of the mark position is 0x66.
(05) bais.skip(5); This sentence is to skip 5 bytes. After skipping 5 bytes, the value of the next read byte in the corresponding byte stream is 0x6B.
(06) bais.read(buf, 0, LEN); This sentence is "Read LEN data from the byte stream and write it into buf, 0 means writing from the 0th position of buf".
(07) bais.reset(); This sentence resets "the next read position in the byte stream" to "the position marked by mark()", that is, 0x66.
After learning the ByteArrayInputStream input stream. Next, we learn the corresponding output stream ByteArrayOutputStream.
ByteArrayOutputStream Introduction
ByteArrayOutputStream is a byte array output stream. It inherits from OutputStream.
The data in ByteArrayOutputStream is written to a byte array. The buffer will automatically grow as the data is continuously written. Data can be obtained using toByteArray() and toString().
Sample code For detailed usage of API in ByteArrayOutputStream, refer to the sample code (ByteArrayOutputStreamTest.java):
import java.io.IOException;import java.io.OutputStream;import java.io.ByteArrayOutputStream;import java.io.ByteArrayInputStream;/** * ByteArrayOutputStream Test program* * @author skywang */public class ByteArrayOutputStreamTest { private static final int LEN = 5; // Corresponding to the English letter "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 test function */ private static void tesByteArrayOutputStream() { // Create ByteArrayOutputStream byte stream ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Write three letters "A", "B", and "C" in turn. 0x41 corresponds to A, 0x42 corresponds to B, and 0x43 corresponds to C. baos.write(0x41); baos.write(0x42); baos.write(0x43); System.out.printf("baos=%s/n", baos); // Write the last 5 bytes from "3" in the ArrayLetters array into baos. // That is, "0x64, 0x65, 0x66, 0x67, 0x68" is written correspondingly, that is, "defgh" baos.write(ArrayLetters, 3, 5); System.out.printf("baos=%s/n", baos); // Calculate the length int size = baos.size(); System.out.printf("size=%s/n", size); // Convert to byte[] array byte[] buf = baos.toByteArray(); String str = new String(buf); System.out.printf("str=%s/n", str); // Convert to byte[] array byte[] buf = baos.toByteArray(); String str = new String(buf); System.out.printf("str=%s/n", str); // Write baos to another output stream try { ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); baos.writeTo((OutputStream)baos2); System.out.printf("baos2=%s/n", baos2); } catch (IOException e) { e.printStackTrace(); } }} Running results:
baos=ABCbaos=ABCdefghsize=8str=ABCdefghbaos2=ABCdefgh