Java IO Learning (12) Bufferedoutputstream's knowledge, source code, and example

Source: Internet
Author: User
Tags constructor count flush int size

Bufferedoutputstream (buffered output stream) of the cognitive, source code and examples

This chapter contains 3 sections: Bufferedoutputstream Introduction, Bufferedoutputstream source code, and Bufferedoutputstream usage examples.

Bufferedoutputstream Introduction

Bufferedoutputstream is the buffered output stream. It inherits from Filteroutputstream.

The role of Bufferedoutputstream is to provide a "buffering function" for another output stream.

Bufferedoutputstream Function List

Bufferedoutputstream (OutputStream out)
Bufferedoutputstream (outputstream out, int size)
synchronized void Close     ()
synchronized void     flush ()
synchronized void     Write (byte[) buffer, int offset, int length)
synchronized void     write (int onebyte)

Bufferedoutputstream Source Analysis (based on jdk1.7.40)

    public class Bufferedoutputstream extends Filteroutputstream {//storage buffer output stream data byte array protected byte buf[];
    The size of the data in the buffer protected int count;
    Constructor: "Buffered output stream" public Bufferedoutputstream (OutputStream out) of the new byte array size 8192 is (out, 8192); }//constructor: "Buffered output stream" public bufferedoutputstream (outputstream out, int size) {Super] with a new byte array size
        if (size <= 0) {throw new IllegalArgumentException ("Buffer size <= 0");
    } BUF = new byte[size]; //Writes buffered data to the output stream private void Flushbuffer () throws IOException {if (Count > 0) {o
            Ut.write (buf, 0, Count);
        Count = 0; "Data B (converted to byte type)" is written to the output stream public synchronized void write (int b) throws IOException {//if buffering has been
        Full, the buffered data is first written to the output stream.
        if (count >= buf.length) {flushbuffer (); //write "Data B" to the buffer buf[count++] = (byte) b; synchronized void Write (byte b[], int off, int len) throws IOException {//If write length is greater than buffer size, first
            Writes the data in the buffer to the output stream, and then writes the array b directly to the output stream if (len >= buf.length) {flushbuffer ();
            Out.write (b, off, Len);
        Return //If "The remaining buffer space is not enough to store the data to be written", the data in the buffer is first written to the output stream if (Len > Buf.length-count) {flushbuffer (
        } system.arraycopy (b, off, buf, Count, Len);
    Count = Len;
        ///writes "Buffered data" to the output stream public synchronized void flush () throws IOException {Flushbuffer ();
    Out.flush (); }


Bufferedoutputstream's source code is very simple, here is a simple description of the idea of Bufferedoutputstream: Bufferedoutputstream through a byte array to buffer the data, when the buffer full or the user calls flush ( function, it writes the data of the buffer to the output stream.

See more highlights of this column: http://www.bianceng.cn

Related Article

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: 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.