001: /*
002: * Copyright (C) 2003-2007 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017: package com.Ostermiller.util;
018:
019: import java.io.*;
020:
021: /**
022: * A writer with a close method with no effect.
023: * More information about this class is available from <a target="_top" href=
024: * "http://ostermiller.org/utils/NoCloseStream.html">ostermiller.org</a>.
025: * <p>
026: * This class is designed to wrap a normal writer
027: * so that it can be passed to methods that write to it
028: * and may erroneously close it. This class is a workaround
029: * when the method cannot be modified because it is in a
030: * library.
031: *
032: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
033: * @since ostermillerutils 1.01.00
034: */
035: public class NoCloseWriter extends Writer implements NoCloseStream {
036:
037: /**
038: * The writer that is being protected.
039: * All methods should be forwarded to it,
040: * except for the close method, which should
041: * do nothing. The reallyClose method should
042: * actually close this stream.
043: *
044: * @since ostermillerutils 1.01.00
045: */
046: protected Writer out;
047:
048: /**
049: * Protect a new writer.
050: *
051: * @param out The writer that is being protected.
052: */
053: public NoCloseWriter(Writer out) {
054: this .out = out;
055: }
056:
057: /**
058: * {@inheritDoc}
059: */
060: @Override
061: public void write(int c) throws IOException {
062: out.write(c);
063: }
064:
065: /**
066: * {@inheritDoc}
067: */
068: @Override
069: public void write(char[] cbuf) throws IOException {
070: out.write(cbuf);
071: }
072:
073: /**
074: * {@inheritDoc}
075: */
076: @Override
077: public void write(char[] cbuf, int off, int len) throws IOException {
078: out.write(cbuf, off, len);
079: }
080:
081: /**
082: * {@inheritDoc}
083: */
084: @Override
085: public void write(String str) throws IOException {
086: out.write(str);
087: }
088:
089: /**
090: * {@inheritDoc}
091: */
092: @Override
093: public void write(String str, int off, int len) throws IOException {
094: out.write(str, off, len);
095: }
096:
097: /**
098: * {@inheritDoc}
099: */
100: @Override
101: public void flush() throws IOException {
102: out.flush();
103: }
104:
105: /**
106: * Has no effect.
107: *
108: * @see #reallyClose()
109: *
110: * @since ostermillerutils 1.01.00
111: */
112: @Override
113: public void close() throws IOException {
114: // Does nothing
115: }
116:
117: /**
118: * {@inheritDoc}
119: */
120: public void reallyClose() throws IOException {
121: out.close();
122: }
123: }
|