KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 /**
22  * A stream that combines two 'sources': first it provides bytes from an array
23  * of bytes, then from the contained <b>stream</b>.
24  *
25  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
26  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
27  * @version $Revision: 1.3 $ $Date: 2006/03/16 11:09:36 $
28  */

29 class PrefilledInputStream extends InputStream JavaDoc {
30     private InputStream JavaDoc stream;
31     private byte[] prefilledBytes;
32     private int passedBytesNumber;
33
34     private PrefilledInputStream() {}
35
36     /**
37      * Constructor that takes a {@link RewindableInputStreamWrapper} instance
38      * and gets <b>stream</b> and array of bytes from it.
39      *
40      * @param rewindableStreamWrapper stream
41      * @see RewindableInputStreamWrapper
42      */

43     PrefilledInputStream(RewindableInputStreamWrapper rewindableStreamWrapper) {
44         this.stream = rewindableStreamWrapper.getStream();
45         this.prefilledBytes = rewindableStreamWrapper.getReadBytes();
46         passedBytesNumber = 0;
47     }
48
49     /**
50      * @see java.io.InputStream#available()
51      */

52     public int available() throws IOException JavaDoc {
53         return prefilledBytes.length - passedBytesNumber + stream.available();
54     }
55
56     /**
57      * @see java.io.InputStream#close()
58      */

59     public void close() throws IOException JavaDoc {
60         prefilledBytes = new byte[0];
61         passedBytesNumber = 0;
62         stream.close();
63     }
64
65     /**
66      * @see java.io.InputStream#reset()
67      */

68     public synchronized void reset() throws IOException JavaDoc {
69         passedBytesNumber = 0;
70         stream.reset();
71     }
72
73     /**
74      * @see java.io.InputStream#markSupported()
75      */

76     public boolean markSupported() {
77         return false;
78     }
79
80     /**
81      * @see java.io.InputStream#mark(int)
82      */

83     public synchronized void mark(int readlimit) {
84     }
85
86     /**
87      * @see java.io.InputStream#skip(long)
88      */

89     public long skip(long n) throws IOException JavaDoc {
90         int prefilledBytesLeft = prefilledBytes.length - passedBytesNumber;
91         long skippedByStream;
92         long skippedAmongPrefilled;
93
94         if (n <= prefilledBytesLeft) {
95             passedBytesNumber += n;
96             skippedByStream = 0;
97             skippedAmongPrefilled = n;
98         } else {
99             passedBytesNumber = prefilledBytes.length;
100             skippedByStream = stream.skip(n - prefilledBytesLeft);
101             skippedAmongPrefilled = prefilledBytesLeft;
102         }
103         return skippedAmongPrefilled + skippedByStream;
104     }
105
106     /**
107      * @see java.io.InputStream#read(byte[])
108      */

109     public int read(byte[] b) throws IOException JavaDoc {
110         return super.read(b);
111     }
112
113     /**
114      * @see java.io.InputStream#read(byte[], int, int)
115      */

116     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
117         return super.read(b, off, len);
118     }
119
120     /**
121      * @see java.io.InputStream#read()
122      */

123     public int read() throws IOException JavaDoc {
124         if (passedBytesNumber < prefilledBytes.length) {
125             return prefilledBytes[passedBytesNumber++];
126         } else {
127             return stream.read();
128         }
129     }
130 }
131
Popular Tags