KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > common > parsers > html > RewindableInputStreamWrapper


1 /*
2  * Copyright 2005 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.common.parsers.html;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 /**
23  * A wrapper for <code>InputStream</code> that:
24  * <ul>
25  * <li>
26  * remembers all bytes read from the <b>stream</b> until
27  * <code>stopRemembering()</code> is called
28  * </li>
29  * <li>
30  * doesn't close the <b>stream</b> when <code>close()</code> is called
31  * </li>
32  * </ul>
33  *
34  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
35  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
36  * @version $Revision: 1.2 $ $Date: 2005/08/02 14:53:29 $
37  */

38 class RewindableInputStreamWrapper extends InputStream JavaDoc {
39     private InputStream JavaDoc stream;
40     private ByteArrayOutputStream JavaDoc readBytes;
41     private boolean remembering;
42
43     private RewindableInputStreamWrapper() {}
44
45     RewindableInputStreamWrapper(InputStream JavaDoc stream) {
46         this.stream = stream;
47         readBytes = new ByteArrayOutputStream JavaDoc();
48         remembering = true;
49     }
50
51     public int available() throws IOException JavaDoc {
52         return stream.available();
53     }
54
55     public void close() throws IOException JavaDoc {}
56
57     public synchronized void reset() throws IOException JavaDoc {
58         stream.reset();
59     }
60
61     public boolean markSupported() {
62         return stream.markSupported();
63     }
64
65     public synchronized void mark(int readlimit) {
66         stream.mark(readlimit);
67     }
68
69     public long skip(long n) throws IOException JavaDoc {
70         return stream.skip(n);
71     }
72
73     public int read(byte[] b) throws IOException JavaDoc {
74         return super.read(b);
75     }
76
77     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
78         return super.read(b, off, len);
79     }
80
81     public int read() throws IOException JavaDoc {
82         int res = stream.read();
83         if (remembering) {
84             readBytes.write(res);
85         }
86         return res;
87     }
88
89     /**
90      * Gets the <code>stream</code>.
91      * @return the <code>stream</code> that is inside this wrapper
92      */

93     public InputStream JavaDoc getStream() {
94         return stream;
95     }
96
97     /**
98      * Returns bytes that were read from this stream, if
99      * <code>stopRemembering</code> was not called, otherwise <code>null</code>.
100      *
101      * @return array of read bytes
102      */

103     public byte[] getReadBytes() {
104         return (readBytes == null) ? null : readBytes.toByteArray();
105     }
106
107     /**
108      * Stops saving read bytes.
109      */

110     public void stopRemembering() {
111         remembering = false;
112         readBytes = null;
113     }
114 }
115
Popular Tags