JDK

java.io.OutputStreamクラス

OutputStreamクラスもByteArrayOutputStreamクラス,FileOutputStreamクラス,ObjectOutputStreamクラスなどのスーパークラスとなる抽象クラスである。

 InputStreamクラスの場合と同じように,何らの出力もしない無名の実装クラスを返すnullOutputStream()メソッドが定義されている。

抽象メソッドとしては,void write(int b)だけが宣言されている。flush()メソッドや,close()メソッドは実装はされているものの,中身は空である。

write(byte b[], int off, int len)メソッドはオフセットと長さを指定して,バイト配列を出力する。forループで配列の内容を繰り返しながら,抽象メソッドであるwrite(int)メソッドを呼び出す実装である。write(byte b[])メソッドは,渡された配列を先頭からすべて出力するようにwrite(b, 0, b.length)を実行するだけである。

/*
 * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package java.io;
import java.util.Objects;
public abstract class OutputStream implements Closeable, Flushable {
    public static OutputStream nullOutputStream() {
        return new OutputStream() {
            private volatile boolean closed;
            private void ensureOpen() throws IOException {
                if (closed) {
                    throw new IOException("Stream closed");
                }
            }
            @Override
            public void write(int b) throws IOException {
                ensureOpen();
            }
            @Override
            public void write(byte b[], int off, int len) throws IOException {
                Objects.checkFromIndexSize(off, len, b.length);
                ensureOpen();
            }
            @Override
            public void close() {
                closed = true;
            }
        };
    }
    public abstract void write(int b) throws IOException;
    public void write(byte b[]) throws IOException {
        write(b, 0, b.length);
    }
    public void write(byte b[], int off, int len) throws IOException {
        Objects.checkFromIndexSize(off, len, b.length);
        // len == 0 condition implicitly handled by loop bounds
        for (int i = 0 ; i < len ; i++) {
            write(b[off + i]);
        }
    }
    public void flush() throws IOException {
    }
    public void close() throws IOException {
    }
}