Bytearrayinputstream Introduction
Bytearrayinputstream is a byte array input stream. It inherits from InputStream.
It contains an internal buffer that contains the bytes read from the stream; In layman's terms, its internal buffer is a byte array, and the Bytearrayinputstream essence is implemented by byte arrays.
As we all know, InputStream provides an external interface to read byte data through the read (), while the interior of the Bytearrayinputstream defines a counter that is used to track the next byte to be read by the read () method.
Sample code
For detailed usage of APIs in Bytearrayinputstream, refer to 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 "abcddefghijklmnopqrsttuvwxyz" private static final byte[] Arrayletters = {0x61, 0x62, 0x63, 0x64, 0x65, 0 X66, 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 Bytearrayinput
Stream byte stream, content is arrayletters array Bytearrayinputstream Bais = new Bytearrayinputstream (arrayletters);
Reads 5 bytes from a stream for (int i=0; i<len; i++) {//If the next byte can continue to be read, read the next byte if (bais.available () >= 0) { Reads the next byte of the stream "int tmp" = Bais.read ();
System.out.printf ("%d:0x%s\n", I, integer.tohexstring (TMP));
}///If "This byte stream" does not support the tagging feature, exit if (!bais.marksupported ()) {System.out.println ("Make not supported!") directly;
return; }//Mark "next read position in the byte stream".
That is, Mark "0x66" because 5 bytes have been read before, so the next read position is 6th byte "//(s), and" parameter 0 "in the mark (0) function of the Bytearrayinputstream class is meaningless.
(), Mark () and reset () are matched, reset () resets the "next read position in the byte stream" to "the location saved in Mark ()" Bais.mark (0); Skips 5 bytes.
After skipping 5 bytes, the next read value in the byte stream should be "0x6b".
Bais.skip (5); Reads 5 data from a byte stream.
Read "0x6b, 0x6c, 0x6d, 0x6e, 0x6f" byte["buf = new Byte[len];
Bais.read (buf, 0, LEN); Converts a buf to a string string.
"0x6b, 0x6c, 0x6d, 0x6e, 0x6f" the corresponding character 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 where Mark () is marked (0x66).
Bais.reset (); Reads 5 bytes into BUF from the reset byte stream.
That reads "0x66, 0x67, 0x68, 0x69, 0x6a" Bais.read (buf, 0, LEN); Converts a buf to a string string. "0x66, 0x67, 0x68, 0x69,0x6a "The corresponding character is" Fghij "string str2 = new string (BUF);
System.out.printf ("str2=%s\n", str2);
}
}
Run Result:
ARRAYLETTERS=ABCDEFGHIJKLMNOPQRSTUVWXYZ
0:0x61
1:0x62
2:0x63
3:0x64
4:0x65 C7/>str2=fghij
Results show:
(arrayletters) is an array of bytes. 0x61 the corresponding ASCII code value is a,0x62 the corresponding ASCII value is B, and so on ...
(a) Bytearrayinputstream Bais = new Bytearrayinputstream (arrayletters); This sentence is to create a "byte stream Bais", its content is arrayletters.
for (int i=0; i<len; i++); The purpose of this for loop is to read 5 bytes from a byte stream. Reads a byte from a stream of bytes each time Bais.read () is invoked.
(Bais.mark) (0); This sentence is "set the label of the byte Stream", at which point the value of the marked position corresponds to 0x66.
(a) Bais.skip (5); This sentence is skipped 5 bytes. After skipping 5 bytes, the value of the next read byte in the corresponding stream is 0x6b.
(a) Bais.read (buf, 0, LEN); The sentence reads Len data from a byte stream to the BUF, and 0 writes from the No. 0 position in the BUF.
(modified) Bais.reset (); This is to reset the next read position in the byte stream to where Mark () is marked (0x66).
Finished learning Bytearrayinputstream input stream. Below, we learn the corresponding output stream bytearrayoutputstream.
Bytearrayoutputstream Introduction
Bytearrayoutputstream is a byte array output stream. It inherits from OutputStream.
The data in the Bytearrayoutputstream is written to a byte array. The buffer automatically grows as the data is being written. You can use Tobytearray () and toString () to get the data.
Sample code
For detailed usage of APIs in Bytearrayoutputstream, refer to 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 fin
Al int LEN = 5; corresponding to the English letter "abcddefghijklmnopqrsttuvwxyz" private static final byte[] Arrayletters = {0x61, 0x62, 0x63, 0x64, 0x65, 0 X66, 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 Bytearrayout
Putstream byte stream Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Write "A", "B", "C" three letters in turn.
0x41 corresponds to a,0x42 corresponding to b,0x43 corresponding to C.
Baos.write (0x41);
Baos.write (0x42); BAos.write (0x43);
System.out.printf ("baos=%s\n", BAOs);
Writes the first 5 bytes of the arrayletters array from "3" to the BAOs.
Namely correspondence writes "0x64, 0x65, 0x66, 0x67, 0x68", namely "DEFGH" Baos.write (Arrayletters, 3, 5);
System.out.printf ("baos=%s\n", BAOs);
Compute 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);
Writes 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 ();
}
}
}
Run Result:
BAOS=ABC
baos=abcdefgh
size=8
str=abcdefgh
baos2=abcdefgh