// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class NonClosingStream extends OutputStream {
    private OutputStream real;
    public NonClosingStream(OutputStream real) {
        this.real = real;
    }
    public void close() throws IOException {
        // ignore
    }
    public void realclose() throws IOException {
        real.close();
    }
    public boolean equals(Object obj) {
        return real.equals(obj);
    }
    public void flush() throws IOException {
        real.flush();
    }
    public int hashCode() {
        return real.hashCode();
    }
    public String toString() {
        return real.toString();
    }
    public void write(byte[] b, int off, int len) throws IOException {
        real.write(b, off, len);
    }
    public void write(byte[] b) throws IOException {
        real.write(b);
    }
    public void write(int b) throws IOException {
        real.write(b);
    }
    public static void main(String[] args) throws IOException {
        // OutputStream os = new FileOutputStream("C:\\z.z");
        NonClosingStream os = new NonClosingStream(new FileOutputStream("C:\\z.z"));
        PrintStream ps = new PrintStream(os);
        ps.println("Hello");
        ps.close();
        PrintStream ps2 = new PrintStream(os);
        ps2.println("Hello");
        ps2.close();
        os.realclose();
    }
}
