您的位置:首页 > 编程语言 > Java开发

JDK中java.io.OutputStream源码

2017-02-10 18:05 197 查看
/*

 * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.

 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

 */

package java.io;

/**

 * This abstract class is the superclass of all classes representing

 * an output stream of bytes. An output stream accepts output bytes

 * and sends them to some sink.

 * 

 * 这个抽象类是所有输出字节流的超类。一个输出流接受输出字节并发送这些字节到指定位置。

 * 

 * <p>

 * Applications that need to define a subclass of

 * <code>OutputStream</code> must always provide at least a method

 * that writes one byte of output.

 * 

 * OutputStream的子类应用必须提供至少一个方法写入输出的字节。

 *

 * @author  Arthur van Hoff

 * @see     java.io.BufferedOutputStream

 * @see     java.io.ByteArrayOutputStream

 * @see     java.io.DataOutputStream

 * @see     java.io.FilterOutputStream

 * @see     java.io.InputStream

 * @see     java.io.OutputStream#write(int)

 * @since   JDK1.0

 */

public abstract class OutputStream implements Closeable, Flushable {

    /**

     * Writes the specified byte to this output stream. The general

     * contract for <code>write</code> is that one byte is written

     * to the output stream. The byte to be written is the eight

     * low-order bits of the argument <code>b</code>. The 24

     * high-order bits of <code>b</code> are ignored.

     * 

     * 向输出流写入特定的字节。通常写write是一字节一字节写入输出流的。

     * 写入的字节只有低8位,高的24位呗省略了。

     * 

     * <p>

     * Subclasses of <code>OutputStream</code> must provide an

     * implementation for this method.

     * 

     * OutputStream的子类必须提供write方法的实现。

     *

     * @param      b   the <code>byte</code>.

     * @exception  IOException  if an I/O error occurs. In particular,

     *             an <code>IOException</code> may be thrown if the

     *             output stream has been closed.

     */

    public abstract void write(int b) throws IOException;

    /**

     * Writes <code>b.length</code> bytes from the specified byte array

     * to this output stream. The general contract for <code>write(b)</code>

     * is that it should have exactly the same effect as the call

     * <code>write(b, 0, b.length)</code>.

     * 

     * 从特定字节数组写入b长度的字节到输出流。

     * 通常write(b)和write(b,0,length)方法有相同的作用。

     *

     * @param      b   the data.

     * @exception  IOException  if an I/O error occurs.

     * @see        java.io.OutputStream#write(byte[], int, int)

     */

    public void write(byte b[]) throws IOException {

        write(b, 0, b.length);

    }

    /**

     * Writes <code>len</code> bytes from the specified byte array

     * starting at offset <code>off</code> to this output stream.

     * The general contract for <code>write(b, off, len)</code> is that

     * some of the bytes in the array <code>b</code> are written to the

     * output stream in order; element <code>b[off]</code> is the first

     * byte written and <code>b[off+len-1]</code> is the last byte written

     * by this operation.

     * 

     * 从off位置开始从特定的字节数组写入len长度的字节到输出流。

     * 通常write(b,off,len)规定数组b中的字节按照顺序写入输出流;

     * b[off]是第一个写入的字节,b[off+len-1]是最后一个写入的字节。

     * 

     * <p>

     * The <code>write</code> method of <code>OutputStream</code> calls

     * the write method of one argument on each of the bytes to be

     * written out. Subclasses are encouraged to override this method and

     * provide a more efficient implementation.

     * 

     * 这个OutputStream的write方法调用了含有一个参数的write方法去写入。

     * 子类最好重写此方法并提供更有效的实现。

     * 

     * <p>

     * If <code>b</code> is <code>null</code>, a

     * <code>NullPointerException</code> is thrown.

     * <p>

     * 

     * 数组b为空时,抛出空指针异常

     * 

     * If <code>off</code> is negative, or <code>len</code> is negative, or

     * <code>off+len</code> is greater than the length of the array

     * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.

     * 

     * off或len为负数或者off+len大于数组b的长度,抛出下标越界。

     *

     * @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. In particular,

     *             an <code>IOException</code> is thrown if the output

     *             stream is closed.

     */

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

        if (b == null) {

            throw new NullPointerException();

        } else if ((off < 0) || (off > b.length) || (len < 0) ||

                   ((off + len) > b.length) || ((off + len) < 0)) {

            throw new IndexOutOfBoundsException();

        } else if (len == 0) {

            return;

        }

        for (int i = 0 ; i < len ; i++) {

            write(b[off + i]);

        }

    }

    /**

     * Flushes this output stream and forces any buffered output bytes

     * to be written out. The general contract of <code>flush</code> is

     * that calling it is an indication that, if any bytes previously

     * written have been buffered by the implementation of the output

     * stream, such bytes should immediately be written to their

     * intended destination.

     * 

     * 清洗输出流并强制将任何缓存的输出字节写出。

     * 通常flush的规定,调用这个方法表示之前所有通过输出流实现缓存的字节应该马上写入对应的位置。

     * 

     * <p>

     * If the intended destination of this stream is an abstraction provided by

     * the underlying operating system, for example a file, then flushing the

     * stream guarantees only that bytes previously written to the stream are

     * passed to the operating system for writing; it does not guarantee that

     * they are actually written to a physical device such as a disk drive.

     * <p>

     * 

     * 如果流的输出目标位置是由底层的操作系统提供的抽象,如一个文件,flush清洗流只能确保之前写入流的字节通过操作系统写入;

     * 但不能保证这些字节可以写入物理设备如硬盘驱动。

     * 

     * The <code>flush</code> method of <code>OutputStream</code> does nothing.

     * OutputStream的flush方法没有做任何处理。

     *

     * @exception  IOException  if an I/O error occurs.

     */

    public void flush() throws IOException {

    }

    /**

     * Closes this output stream and releases any system resources

     * associated with this stream. The general contract
4000
of <code>close</code>

     * is that it closes the output stream. A closed stream cannot perform

     * output operations and cannot be reopened.

     * 

     * 关闭输出流,释放与流相关的系统资源。

     * 通常关闭操作是关闭输出流。

     * 一个关闭的流不可以在做输出操作也不可以在打开。

     * 

     * <p>

     * The <code>close</code> method of <code>OutputStream</code> does nothing.

     * OutputStream的close方法也没有做任何处理

     *

     * @exception  IOException  if an I/O error occurs.

     */

    public void close() throws IOException {

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: