Java. Bytearrayinputstream and Bytearrayoutputstream understand again

Source: Internet
Author: User

The first time to see the Bytearrayoutputstream is in the nutch part of the source code, and later in the IO operations involved in the discovery of the traces of these two classes, it is really very useful, so put their usage summary.

the use of Bytearrayoutputstream

The following is documented in the JDK:

public class Bytearrayoutputstream extends outputstream

This class implements an output stream in which the data is written to a byte array. The buffer grows automatically as data is written continuously . Data can be obtained using Tobytearray () and toString ().

Closing bytearrayoutputstream is not valid. The methods in this class can still be called after the stream is closed, without generating any ioexception.

My personal understanding is that Bytearrayoutputstream is used to cache the data (the target of the data write (output stream literal)), writes the data to its internal buffer, the buffer grows automatically, and the data can be extracted from it when the write is complete. For this reason, Bytearrayoutputstream is often used to store data for one-time writes.

Instance:

Reads the binary data from the file and stores it all in Bytearrayoutputstream.

FileInputStream fis=new fileinputstream ("test");

Bufferedinputstream bis=new bufferedinputstream (FIS);

Bytearrayoutputstream baos=new Bytearrayoutputstream ();

int C=bis.read ();//reads the next byte in the BIS stream

while (C!=-1) {

Baos.write (c);

C=bis.read ();

}

Bis.close ();

byte retarr[]=baos.tobytearray ();

The use of Bytearrayinputstream

Comparatively speaking, Bytearrayinputstream is relatively rare. Look at the description in the JDK document first:

The public class Bytearrayinputstreamextends Inputstreambytearrayinputstream contains an internal buffer that contains bytes read from the stream. The internal counter tracks the next byte to be provided by the Read method.

Closing Bytearrayinputstream is not valid. The methods in this class can still be called after the stream is closed, without generating any ioexception.

constructor function:

Bytearrayinputstream (byte[] buf)

Note that it needs to provide a byte array as a buffer.

Like most inputstream semantics, you can read data from its buffer, so we can wrap another layer of InputStream outside of it to use the Read method we need.

Personally think a better use is to read the packet in the network, because the packet is generally fixed length, we can first allocate a large enough byte array, such as byte buf[]=new byte[1024];

Then call a method to get packets from the network, for example:

Socket s= ...;

DataInputStream dis=new DataInputStream (S.getinputstream ());

Dis.read (BUF);//store all data in BUF

Bytearrayinputstream bais=new Bytearrayinputstream (BUF); Think of the part as the input stream

DataInputStream dis_2=new DataInputStream (Bais);

You can now use the various read methods of Dis_2 to read the specified bytes

For example, the first byte is a version number, dis_2.readbyte ();

Wait a minute......

The two-time wrapper for the above example looks a bit superfluous, but the advantage of using Bytearrayinputstream is that its data still exists after the stream is switched off.

Java. Bytearrayinputstream and Bytearrayoutputstream understand again

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.