Re: Why should close() close the underlying stream?
Eric Sosman wrote:
Daniel Pitts wrote:
Eric Sosman wrote:
[...] If desired you could add a reallyClose() method
that forwards to super.close(), and/or a getActualStream()
method that returns the superclass instance.
You can't return a superclass instance, [...]
Gugghh! You're right, of course; sorry for the red
herring.
Something similar can be made to work.
package october;
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();
}
}
Arne