KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > NoCloseReader


1 /*
2  * Streams that have a different close mechanism.
3  * Copyright (C) 2003 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.io.*;
21
22 /**
23  * A reader which a close method with no effect.
24  * More information about this class is available from <a target="_top" HREF=
25  * "http://ostermiller.org/utils/NoCloseStream.html">ostermiller.org</a>.
26  * <p>
27  * This class is designed to wrap a normal reader
28  * so that it can be passed to methods that read from it
29  * and may erroneously close it. This class is a workaround
30  * when the method cannot be modified because it is in a
31  * library.
32  *
33  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
34  * @since ostermillerutils 1.01.00
35  */

36 public class NoCloseReader extends Reader implements NoCloseStream {
37
38     /**
39      * The reader that is being protected.
40      * All methods should be forwarded to it,
41      * except for the close method, which should
42      * do nothing. The reallyClose method should
43      * actually close this stream.
44      *
45      * @since ostermillerutils 1.01.00
46      */

47     protected Reader in;
48
49     /**
50      * Protect a new reader.
51      *
52      * @param in The reader that is being protected.
53      *
54      * @since ostermillerutils 1.01.00
55      */

56     public NoCloseReader(Reader in){
57         this.in = in;
58     }
59
60     /**
61      * {@inheritDoc}
62      */

63     public int read() throws IOException {
64         return in.read();
65     }
66
67     /**
68      * {@inheritDoc}
69      */

70     public int read(char[] cbuf) throws IOException {
71         return in.read(cbuf);
72     }
73
74     /**
75      * {@inheritDoc}
76      */

77     public int read(char[] cbuf, int off, int len) throws IOException {
78         return in.read(cbuf, off, len);
79     }
80
81     /**
82      * {@inheritDoc}
83      */

84     public long skip(long n) throws IOException {
85         return in.skip(n);
86     }
87
88     /**
89      * {@inheritDoc}
90      */

91     public boolean ready() throws IOException {
92         return in.ready();
93     }
94
95     /**
96      * Has no effect.
97      *
98      * @see #reallyClose()
99      *
100      * @since ostermillerutils 1.01.00
101      */

102     public void close() throws IOException {
103     }
104
105     /**
106      * {@inheritDoc}
107      */

108     public void mark(int readlimit) throws IOException {
109         in.mark(readlimit);
110     }
111
112     /**
113      * {@inheritDoc}
114      */

115     public void reset() throws IOException {
116         in.reset();
117     }
118
119     /**
120      * {@inheritDoc}
121      */

122     public boolean markSupported(){
123         return in.markSupported();
124     }
125
126     /**
127      * {@inheritDoc}
128      */

129     public void reallyClose() throws IOException {
130         in.close();
131     }
132 }
133
Popular Tags