JDK的java.io.BufferedOutputStream的源码
2017-03-26 22:42
183 查看
/*
* Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.io;
/**
* The class implements a buffered output stream. By setting up such
* an output stream, an application can write bytes to the underlying
* output stream without necessarily causing a call to the underlying
* system for each byte written.
*
* 此类继承了一个缓存的输出流。
* 通过设置这样的输出流,应用可以将字节写入下面的输出流而下面的系统不用每写入一个字节都调用。
*
* @author Arthur van Hoff
* @since JDK1.0
*/
public
class BufferedOutputStream extends FilterOutputStream {
/**
* The internal buffer where data is stored.
* 数据存储的内部缓存区。
*/
protected byte buf[];
/**
* The number of valid bytes in the buffer. This value is always
* in the range <tt>0</tt> through <tt>buf.length</tt>; elements
* <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
* byte data.
*
* 缓存中有效字节数量。
* 值的范围从0到缓存区的长度,有效值存放在内部缓存的数组内。
*/
protected int count;
/**
* Creates a new buffered output stream to write data to the
* specified underlying output stream.
*
* 创建一个新的缓存输出流,将数据写入指定的下面的输出流。
*
* @param out the underlying output stream.
*/
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
}
/**
* Creates a new buffered output stream to write data to the
* specified underlying output stream with the specified buffer
* size.
*
* 创建一个缓存输出流,用指定大小的缓存将数据写入指定的下面的输出流。
*
* @param out the underlying output stream.
* @param size the buffer size.
* @exception IllegalArgumentException if size <= 0.
*/
public BufferedOutputStream(OutputStream out, int size) {
super(out);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}
/** Flush the internal buffer 清理内部缓存 */
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}
/**
* Writes the specified byte to this buffered output stream.
* 将指定字节写入缓存输出流
*
* @param b the byte to be written. 写入的字节
* @exception IOException if an I/O error occurs.
*/
public synchronized void write(int b) throws IOException {
if (count >= buf.length) {
flushBuffer();
}
buf[count++] = (byte)b;
}
/**
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this buffered output stream.
*
* 从指定的字节数组中写入常读为len长度的字节,从offset位置开始写入到缓存输出流。
*
* <p> Ordinarily this method stores bytes from the given array into this
* stream's buffer, flushing the buffer to the underlying output stream as
* needed. If the requested length is at least as large as this stream's
* buffer, however, then this method will flush the buffer and write the
* bytes directly to the underlying output stream. Thus redundant
* <code>BufferedOutputStream</code>s will not copy data unnecessarily.
*
* 通常这个方法从指定的数组中将字节存储在流的缓存中,如果需要清洗下面的输出流的缓存。
* 但是如果需要的长度至少和流的缓存一样大,则这个方法会清空缓存直接写入字节到下面的输出流。
* 这样多余的缓存输出流不会复制不必要的数据。
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @exception IOException if an I/O error occurs.
*/
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;
}
/**
* Flushes this buffered output stream. This forces any buffered
* output bytes to be written out to the underlying output stream.
*
* 清洗缓存输出流。
* 这个方法会强制缓存输出字节写出到下面的输出流。
*
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public synchronized void flush() throws IOException {
flushBuffer();
out.flush();
}
}
* Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.io;
/**
* The class implements a buffered output stream. By setting up such
* an output stream, an application can write bytes to the underlying
* output stream without necessarily causing a call to the underlying
* system for each byte written.
*
* 此类继承了一个缓存的输出流。
* 通过设置这样的输出流,应用可以将字节写入下面的输出流而下面的系统不用每写入一个字节都调用。
*
* @author Arthur van Hoff
* @since JDK1.0
*/
public
class BufferedOutputStream extends FilterOutputStream {
/**
* The internal buffer where data is stored.
* 数据存储的内部缓存区。
*/
protected byte buf[];
/**
* The number of valid bytes in the buffer. This value is always
* in the range <tt>0</tt> through <tt>buf.length</tt>; elements
* <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
* byte data.
*
* 缓存中有效字节数量。
* 值的范围从0到缓存区的长度,有效值存放在内部缓存的数组内。
*/
protected int count;
/**
* Creates a new buffered output stream to write data to the
* specified underlying output stream.
*
* 创建一个新的缓存输出流,将数据写入指定的下面的输出流。
*
* @param out the underlying output stream.
*/
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
}
/**
* Creates a new buffered output stream to write data to the
* specified underlying output stream with the specified buffer
* size.
*
* 创建一个缓存输出流,用指定大小的缓存将数据写入指定的下面的输出流。
*
* @param out the underlying output stream.
* @param size the buffer size.
* @exception IllegalArgumentException if size <= 0.
*/
public BufferedOutputStream(OutputStream out, int size) {
super(out);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}
/** Flush the internal buffer 清理内部缓存 */
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}
/**
* Writes the specified byte to this buffered output stream.
* 将指定字节写入缓存输出流
*
* @param b the byte to be written. 写入的字节
* @exception IOException if an I/O error occurs.
*/
public synchronized void write(int b) throws IOException {
if (count >= buf.length) {
flushBuffer();
}
buf[count++] = (byte)b;
}
/**
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this buffered output stream.
*
* 从指定的字节数组中写入常读为len长度的字节,从offset位置开始写入到缓存输出流。
*
* <p> Ordinarily this method stores bytes from the given array into this
* stream's buffer, flushing the buffer to the underlying output stream as
* needed. If the requested length is at least as large as this stream's
* buffer, however, then this method will flush the buffer and write the
* bytes directly to the underlying output stream. Thus redundant
* <code>BufferedOutputStream</code>s will not copy data unnecessarily.
*
* 通常这个方法从指定的数组中将字节存储在流的缓存中,如果需要清洗下面的输出流的缓存。
* 但是如果需要的长度至少和流的缓存一样大,则这个方法会清空缓存直接写入字节到下面的输出流。
* 这样多余的缓存输出流不会复制不必要的数据。
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @exception IOException if an I/O error occurs.
*/
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;
}
/**
* Flushes this buffered output stream. This forces any buffered
* output bytes to be written out to the underlying output stream.
*
* 清洗缓存输出流。
* 这个方法会强制缓存输出字节写出到下面的输出流。
*
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public synchronized void flush() throws IOException {
flushBuffer();
out.flush();
}
}
相关文章推荐
- JDK中java.io.OutputStream源码
- JDK 1.7 java.io 源码学习之Closeable、Flushable、Appendable接口
- JDK 1.7 java.io 源码学习之InputStream和OutputStream
- JDK 1.7 java.io 源码学习之ByteArrayInputStream和ByteArrayOutputStream
- JDK源码阅读(1)_简介+ java.io
- JDK 1.7 java.io 源码学习之ObjectInputStream和ObjectOutputStream
- JDK源码阅读(五)java.io.Serializable接口
- JDK 1.7 java.io 源码学习之AutoCloseable接口和try-with-resources语法
- JDK的java.io.Reader源码
- jdk 源码分析(18)java io包分析
- JDK 1.7 java.io 源码学习之FilterInputStream和FilterOutputStream
- JDK 1.7 java.io 源码学习之Serializable接口
- JDK 1.7 java.io 源码学习之FileInputStream和FileOutputStream
- JDK中java.io.InputStream源码
- Java 源码学习线路————_先JDK工具包集合_再core包,也就是String、StringBuffer等_Java IO类库
- JDK 1.7 java.io 源码学习之AutoCloseable接口和try-with-resources语法
- IBM JDK与SUN JDK的区别——java.io.File
- JDK源码分析之 java.lang.Boolean
- Java 查看jdk源码-查找源代码位置
- [Java IO源码]缓冲流的实现原理