Flush () in the Java IO stream

Source: Internet
Author: User

Implemented through Bufferedoutputstream or bufferedwriter links to the underlying stream. Therefore, in writing

When the data is finished, flush becomes particularly important.

For example:

The Web server responds with a 300-byte message through the output flow to the client, but at this point the output stream has a 1024-byte buffer. Therefore, the output stream waits for the Web server to continue to respond to the client, and when the Web server's response information fills the buffer in the output stream, the output stream responds to the message to the Web client.

In order to solve this embarrassing situation, the flush () method appeared. The flush () method can force the output stream (or buffered stream) to send data, even if the buffer is not filled at this time, to break the state of the deadlock.

When we send data using the output stream, when the data cannot fill the buffer of the output stream, the data is stored in the buffer of the output stream. If we call the close output stream at this time, the data stored in the buffer of the output stream will be lost. So, when you close the output stream, you should flush the output stream of the flush, saying, "Forcing all buffered output data to be written out to the underlying output stream".

Two. Read the flush () source code:

The following is an example of the Bufferedoutputstream class:

extends Filteroutputstream   {          publicsynchronizedvoid flush ()  throws  ioexception        {           flushbuffer ();           Out.flush ();        }       Privatevoid Flushbuffer ()  throws  ioexception       {        if(Count > 0)           {            0, count);             = 0;}}     }

See here everyone understand, actually flush () is also through Out.write () to write data to the underlying output stream.

Flush () in the Java IO stream

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.