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)
Package java.io;
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 (); }
}
Description
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.cnhttp://www.bianceng.cn/Programming/Java/