NoClose Streams

Create streams that cannot be closed with the normal close() mechanism. Instead, a reallyClose() method is provided while the close() method does nothing.

This is useful when you have a stream to pass to methods that either read or write using the stream. If any of the methods call the stream's close method before you are done with the stream, this class is useful. Simply wrap your stream in one of the NoClose Streams, and pass that around. When the other method calls the close method it will have no effect. You can later close your stream by calling the reallyClose() method.

NoCloseStream

Interface for all four of the following classes.

NoCloseInputStream

Example

// prevent system.in from being closed
NoCloseInputStream in = new NoCloseInputStream(System.in);
// pass in to other classes:
/* somebodyelse.dosomething(in); */
in.reallyClose();

NoCloseOutputStream

Example

// prevent system.out from being closed
NoCloseOutputStream out = new NoCloseOutputStream(System.out);
// pass out to other classes:
/* somebodyelse.dosomething(out); */
out.reallyClose();

NoCloseReader

Example

// prevent a file reader from being closed
NoCloseReader in = new NoCloseReader(new FileReader("file.txt"));
// pass in to other classes:
/* somebodyelse.dosomething(in); */
in.reallyClose();

NoCloseWriter

Example

// prevent a file writer from being closed
NoCloseWriter out = new NoCloseWriter(new FileWriter("file.txt"));
// pass out to other classes:
/* somebodyelse.dosomething(out); */
out.reallyClose();