001: /*
002: * Created on Sep 17, 2004
003: */
004: package uk.org.ponder.streamutil;
005:
006: import java.io.InputStream;
007: import java.io.OutputStream;
008: import java.io.Reader;
009: import java.io.Writer;
010:
011: import uk.org.ponder.util.Logger;
012:
013: /**
014: * @author Antranig Basman (antranig@caret.cam.ac.uk)
015: *
016: */
017: public class StreamCloseUtil {
018: /**
019: * Exceptions should NEVER be thrown from "cleanup" methods. Compensate for
020: * this incorrect JDK design with this method. Any exception thrown from the
021: * close operation will be swallowed and printed to a log. If this logging
022: * operation causes a FURTHER exception, this will itself be swallowed with no
023: * further logging attempted...
024: *
025: * @param w A Writer to be closed, which may be <code>null</code>.
026: */
027: public static void closeWriter(Writer w) {
028: if (w != null) {
029: try {
030: w.close();
031: } catch (Throwable t) {
032: try {
033: Logger.log.info(
034: "Unhandled exception while closing Writer",
035: t);
036: } catch (Throwable t2) {
037: }
038: }
039: }
040: }
041:
042: /**
043: * See comments for closeWriter.
044: *
045: * @param os
046: */
047: public static void closeOutputStream(OutputStream os) {
048: if (os != null) {
049: try {
050: os.close();
051: } catch (Throwable t) {
052: try {
053: Logger.log
054: .info(
055: "Unhandled exception while closing OutputStream",
056: t);
057: } catch (Throwable t2) {
058: }
059: }
060: }
061: }
062:
063: /**
064: * See comments for closeWriter.
065: *
066: * @param is
067: */
068: public static void closeInputStream(InputStream is) {
069: if (is != null) {
070: try {
071: is.close();
072: } catch (Throwable t) {
073: try {
074: Logger.log
075: .info(
076: "Unhandled exception while closing InputStream",
077: t);
078: } catch (Throwable t2) {
079: }
080: }
081: }
082: }
083:
084: /**
085: * See comments above.
086: */
087: public static void closeReader(Reader r) {
088: if (r != null) {
089: try {
090: r.close();
091: } catch (Throwable t) {
092: try {
093: Logger.log.info(
094: "Unhandled exception while closing Reader",
095: t);
096: } catch (Throwable t2) {
097: }
098: }
099: }
100: }
101: }
|