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.
NoCloseStreamInterface for all four of the following classes.
// prevent system.in from being closed NoCloseInputStream in = new NoCloseInputStream(System.in); // pass in to other classes: /* somebodyelse.dosomething(in); */ in.reallyClose();
// prevent system.out from being closed NoCloseOutputStream out = new NoCloseOutputStream(System.out); // pass out to other classes: /* somebodyelse.dosomething(out); */ out.reallyClose();
// 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();
// 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();
OstermillerUtil Java Utilities Copyright (c) 2001-2020 by Stephen Ostermiller and other contributors
The OstermillerUtils library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) any later version.
This program 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 for more details.
License FAQs - Why GPL? How about the LGPL or something else?