KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > AutoCloseInputStream


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/AutoCloseInputStream.java,v 1.7.2.1 2004/02/22 18:21:13 olegk Exp $
3  * $Revision: 1.7.2.1 $
4  * $Date: 2004/02/22 18:21:13 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient;
33
34 import java.io.FilterInputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37
38 /**
39  * Closes an underlying stream as soon as the end of the stream is reached, and
40  * notifies a client when it has done so.
41  *
42  * @author Ortwin Glück
43  * @author Eric Johnson
44  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
45  *
46  * @since 2.0
47  */

48
49 class AutoCloseInputStream extends FilterInputStream JavaDoc {
50
51     /**
52      * True if this stream is open. Assume that the underlying stream
53      * is open until we get an EOF indication.
54      */

55     private boolean streamOpen = true;
56
57     /** True if the stream closed itself. */
58     private boolean selfClosed = false;
59
60     /**
61      * The watcher is notified when the contents of the stream have
62      * been exhausted
63      */

64     private ResponseConsumedWatcher watcher = null;
65
66     /**
67      * Create a new auto closing stream for the provided connection
68      *
69      * @param in the input stream to read from
70      * @param watcher To be notified when the contents of the stream have been
71      * consumed.
72      */

73     public AutoCloseInputStream(
74             final InputStream JavaDoc in, final ResponseConsumedWatcher watcher) {
75         super(in);
76         this.watcher = watcher;
77     }
78
79     /**
80      * Reads the next byte of data from the input stream.
81      *
82      * @throws IOException when there is an error reading
83      * @return the character read, or -1 for EOF
84      */

85     public int read() throws IOException JavaDoc {
86         int l = -1;
87
88         if (isReadAllowed()) {
89             // underlying stream not closed, go ahead and read.
90
l = super.read();
91             checkClose(l);
92         }
93
94         return l;
95     }
96
97     /**
98      * Reads up to <code>len</code> bytes of data from the stream.
99      *
100      * @param b a <code>byte</code> array to read data into
101      * @param off an offset within the array to store data
102      * @param len the maximum number of bytes to read
103      * @return the number of bytes read or -1 for EOF
104      * @throws IOException if there are errors reading
105      */

106     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
107         int l = -1;
108
109         if (isReadAllowed()) {
110             l = super.read(b, off, len);
111             checkClose(l);
112         }
113
114         return l;
115     }
116
117     /**
118      * Reads some number of bytes from the input stream and stores them into the
119      * buffer array b.
120      *
121      * @param b a <code>byte</code> array to read data into
122      * @return the number of bytes read or -1 for EOF
123      * @throws IOException if there are errors reading
124      */

125     public int read(byte[] b) throws IOException JavaDoc {
126         int l = -1;
127
128         if (isReadAllowed()) {
129             l = super.read(b);
130             checkClose(l);
131         }
132         return l;
133     }
134
135     /**
136      * Close the stream, and also close the underlying stream if it is not
137      * already closed.
138      * @throws IOException If an IO problem occurs.
139      */

140     public void close() throws IOException JavaDoc {
141         if (!selfClosed) {
142             selfClosed = true;
143             notifyWatcher();
144         }
145     }
146
147     /**
148      * Close the underlying stream should the end of the stream arrive.
149      *
150      * @param readResult The result of the read operation to check.
151      * @throws IOException If an IO problem occurs.
152      */

153     private void checkClose(int readResult) throws IOException JavaDoc {
154         if (readResult == -1) {
155             notifyWatcher();
156         }
157     }
158
159     /**
160      * See whether a read of the underlying stream should be allowed, and if
161      * not, check to see whether our stream has already been closed!
162      *
163      * @return <code>true</code> if it is still OK to read from the stream.
164      * @throws IOException If an IO problem occurs.
165      */

166     private boolean isReadAllowed() throws IOException JavaDoc {
167         if (!streamOpen && selfClosed) {
168             throw new IOException JavaDoc("Attempted read on closed stream.");
169         }
170         return streamOpen;
171     }
172
173     /**
174      * Notify the watcher that the contents have been consumed.
175      * @throws IOException If an IO problem occurs.
176      */

177     private void notifyWatcher() throws IOException JavaDoc {
178         if (streamOpen) {
179             super.close();
180             streamOpen = false;
181
182             if (watcher != null) {
183                 watcher.responseConsumed();
184             }
185         }
186     }
187 }
188
189
Popular Tags