Java.IO.BufferedOutputStream

Constructor

构造一个缓冲流,内部缓冲的默认大小为8192

public BufferedOutputStream(OutputStream out)
public BufferedOutputStream(OutputStream out, int size)

Method

flushBuffer()

刷新缓冲区,将内部缓冲缓冲区中的内容写入

    private void flushBuffer() throws IOException {
        if (count > 0) {
            out.write(buf, 0, count);
            count = 0;
        }
    }

public synchronized void write(int b) throws IOException
public synchronized void write(byte b[], int off, int len) throws IOException

向流中写入字节或字节数组

    public synchronized void write(int b) throws IOException {
        if (count >= buf.length) {
            flushBuffer();
        }
        buf[count++] = (byte)b;
    }

    public synchronized void write(byte b[], int off, int len) throws IOException {
//如果要写入的字节数组的长度大于缓冲区的大小,先清空缓冲区,再写入
        if (len >= buf.length) {
            /* If the request length exceeds the size of the output buffer,
               flush the output buffer and then write the data directly.
               In this way buffered streams will cascade harmlessly. */
            flushBuffer();
            out.write(b, off, len);
            return;
        }
//如果要写入的字节的长度大于缓冲区的剩余大小,刷新缓冲区再写入
        if (len > buf.length - count) {
            flushBuffer();
        }
        System.arraycopy(b, off, buf, count, len);
        count += len;
    }

public synchronized void flush() throws IOException

刷新缓冲区

    public synchronized void flush() throws IOException {
        flushBuffer();
        out.flush();
    }

你可能感兴趣的:(Java.IO.BufferedOutputStream)