Bufferedoutputstream Caching function parsing (source reading)

Source: Internet
Author: User

To introduce Bufferedoutputstream, let's first look at the OutputStream class
Abstract class OutputStream class has three write methods

    1. public abstract void Write (int b)
    2. public void Write (byte b[])
    3. public void Write (byte b[], int off, int len)

From the above we can see that the first write method is for subclasses to overwrite, while the second person write (byte b[]) method source code is as follows

publicvoidwrite(bytethrows IOException {        0, b.length);    }

So the final processing or call to the third method write (byte b[],int off,int len), the method is as follows:

  Public void Write(byteB[],intOffintLenthrowsIOException {if(b = =NULL) {Throw NewNullPointerException (); }Else if(Off <0) || (Off > B.length) | | (Len <0) || ((off + len) > b.length) | | (off + len) <0)) {Throw NewIndexoutofboundsexception (); }Else if(len = =0) {return; } for(inti =0; i < Len; i++) {//Note here, this is actually called the previous abstract method, write (int b), and an automatic transformation occurredWrite (B[off + i]); }    }

problem
We do not see how the abstract method is implemented, that is to say, OutputStream also has a buffer function, we can write to a stream of data written into a byte[] buf array, and then call write (byte b[]) or write (byte b[], int Off, int len) Also, why do you want to Bufferedinputstream class, what's the difference between them? At the same time we know that there is a flush () method in the Bufferedinputstream class, and there is no flush () method in the OutputStream stream. Flush () is not necessary, next look at the Bufferedoutputstream class;

First, bufferedoutput the OutputStream class object as a parameter of a constructor method.
First look at the Bufferedoutputstream class source code

 Public class bufferedoutputstream extends filteroutputstream {    ///Here defines a byte[] array to act as a buffer    protected byteBuf[];//This variable is the focus, he is used to record the number of bytes in the current buffer    protected intCountwhen we initialize an object, we give the buf this array 8,192 bytes.     Public Bufferedoutputstream(OutputStream out) { This(Out,8192); } Public Bufferedoutputstream(OutputStream out,intSize) {Super(out);if(Size <=0) {Throw NewIllegalArgumentException ("Buffer size <= 0"); }///Here Create an array object of a given size to act as a bufferBUF =New byte[Size]; } Public synchronized void Write(intbthrowsIOException {if(Count >= buf.length)        {Flushbuffer (); } buf[count++] = (byte) b; }//The method is the focus Public synchronized void Write(byteB[],intOffintLenthrowsIOException {//If the passed in array is longer than the length of the BUF array, the Write method of the OutputStream object is called directly.         if(Len >= buf.length)            {Flushbuffer (); Out.write (b, off, Len);return; }//Verify that the rest of the BUF in the Bufferedoutputstream class can be loaded with the incoming array. If not, the data in the current BUF array is written to the underlying IO stream first        if(Len > Buf.length-count)        {Flushbuffer (); }//This is the focus, if the BUF array is not full in the current Bufferedoutputstream class, the incoming array is copied to the current class object buf array, and the value of Count is updated. System.arraycopy (b, off, buf, Count, Len); Count + = Len;//Call the Flushbuffer method to send data in an array of less than 8,192 bytes. The count is zeroed at the same time.     Private void Flushbuffer()throwsIOException {if(Count >0) {Out.write (buf,0, count); Count =0; }    }//Forces data that is not 8,192 bytes in the BUF data to be written to the underlying IO.      Public synchronized void Flush()throwsIOException {flushbuffer ();    Out.flush (); }}

Conclusion:
The OutputStream buffer (array) is the same as the buffer (array) of the class in Bufferedoutputstream, except that the data in the Bufferedoutputstream class will be written to the underlying IO stream first, Then write to the underlying IO stream, which greatly saves IO operations, greatly improves IO utilization, and writes IO is a resource. There is also a problem, assuming that a file is written to the hard disk, the final data of the file is smaller than the default value of 8,192 bytes, then Bufferoutputstream will not write this data to the underlying IO stream, causing the file to be missing, so it needs to be in close () The flush () method is called before forcing data that is not yet filled with the BUF array to be written to the underlying IO. It can also be seen that the node flow is not flush () method, and the general processing flow will be fixed buf this way, such as commonly used printwriter inside is actually a BufferedWriter object, so also need to call the flush () method to refresh, Because the default is not refreshed.

Reference:
Http://www.bdqn.cn/news/201311/12111.shtml

Bufferedoutputstream Caching function parsing (source reading)

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.