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 reader which 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 reader
027: * so that it can be passed to methods that read from 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 NoCloseReader extends Reader implements NoCloseStream {
036:
037: /**
038: * The reader 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 Reader in;
047:
048: /**
049: * Protect a new reader.
050: *
051: * @param in The reader that is being protected.
052: *
053: * @since ostermillerutils 1.01.00
054: */
055: public NoCloseReader(Reader in) {
056: this .in = in;
057: }
058:
059: /**
060: * {@inheritDoc}
061: */
062: @Override
063: public int read() throws IOException {
064: return in.read();
065: }
066:
067: /**
068: * {@inheritDoc}
069: */
070: @Override
071: public int read(char[] cbuf) throws IOException {
072: return in.read(cbuf);
073: }
074:
075: /**
076: * {@inheritDoc}
077: */
078: @Override
079: public int read(char[] cbuf, int off, int len) throws IOException {
080: return in.read(cbuf, off, len);
081: }
082:
083: /**
084: * {@inheritDoc}
085: */
086: @Override
087: public long skip(long n) throws IOException {
088: return in.skip(n);
089: }
090:
091: /**
092: * {@inheritDoc}
093: */
094: @Override
095: public boolean ready() throws IOException {
096: return in.ready();
097: }
098:
099: /**
100: * Has no effect.
101: *
102: * @see #reallyClose()
103: *
104: * @since ostermillerutils 1.01.00
105: */
106: @Override
107: public void close() throws IOException {
108: // Does nothing
109: }
110:
111: /**
112: * {@inheritDoc}
113: */
114: @Override
115: public void mark(int readlimit) throws IOException {
116: in.mark(readlimit);
117: }
118:
119: /**
120: * {@inheritDoc}
121: */
122: @Override
123: public void reset() throws IOException {
124: in.reset();
125: }
126:
127: /**
128: * {@inheritDoc}
129: */
130: @Override
131: public boolean markSupported() {
132: return in.markSupported();
133: }
134:
135: /**
136: * {@inheritDoc}
137: */
138: public void reallyClose() throws IOException {
139: in.close();
140: }
141: }
|