001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.io;
019:
020: /**
021: * FilteredInputStream is a class which takes an input stream and
022: * <em>filters</em> the input in some way. The filtered view may be a buffered
023: * view or one which uncompresses data before returning bytes read.
024: * FilterInputStreams are meant for byte streams.
025: *
026: * @see FilterOutputStream
027: */
028: public class FilterInputStream extends InputStream {
029:
030: /**
031: * The target InputStream which is being filtered.
032: */
033: protected InputStream in;
034:
035: /**
036: * Constructs a new FilterInputStream on the InputStream <code>in</code>.
037: * All reads are now filtered through this stream.
038: *
039: * @param in
040: * The non-null InputStream to filter reads on.
041: */
042: protected FilterInputStream(InputStream in) {
043: super ();
044: this .in = in;
045: }
046:
047: /**
048: * Answers a int representing the number of bytes that are available before
049: * this FilterInputStream will block. This method returns the number of
050: * bytes available in the target stream.
051: *
052: * @return the number of bytes available before blocking.
053: *
054: * @throws IOException
055: * If an error occurs in this stream.
056: */
057: @Override
058: public int available() throws IOException {
059: return in.available();
060: }
061:
062: /**
063: * Close this FilterInputStream. This implementation closes the target
064: * stream.
065: *
066: * @throws IOException
067: * If an error occurs attempting to close this stream.
068: */
069: @Override
070: public void close() throws IOException {
071: in.close();
072: }
073:
074: /**
075: * Set a Mark position in this FilterInputStream. The parameter
076: * <code>readLimit</code> indicates how many bytes can be read before a
077: * mark is invalidated. Sending reset() will reposition the Stream back to
078: * the marked position provided <code>readLimit</code> has not been
079: * surpassed.
080: * <p>
081: * This implementation sets a mark in the target stream.
082: *
083: * @param readlimit
084: * the number of bytes to be able to read before invalidating the
085: * mark.
086: */
087: @Override
088: public synchronized void mark(int readlimit) {
089: in.mark(readlimit);
090: }
091:
092: /**
093: * Answers a boolean indicating whether or not this FilterInputStream
094: * supports mark() and reset(). This implementation answers whether or not
095: * the target stream supports marking.
096: *
097: * @return <code>true</code> if mark() and reset() are supported,
098: * <code>false</code> otherwise.
099: */
100: @Override
101: public boolean markSupported() {
102: return in.markSupported();
103: }
104:
105: /**
106: * Reads a single byte from this FilterInputStream and returns the result as
107: * an int. The low-order byte is returned or -1 of the end of stream was
108: * encountered. This implementation returns a byte from the target stream.
109: *
110: * @return the byte read or -1 if end of stream.
111: *
112: * @throws IOException
113: * If the stream is already closed or another IOException
114: * occurs.
115: */
116: @Override
117: public int read() throws IOException {
118: return in.read();
119: }
120:
121: /**
122: * Reads bytes from this FilterInputStream and stores them in byte array
123: * <code>buffer</code>. Answer the number of bytes actually read or -1 if
124: * no bytes were read and end of stream was encountered. This implementation
125: * reads bytes from the target stream.
126: *
127: * @param buffer
128: * the byte array in which to store the read bytes.
129: * @return the number of bytes actually read or -1 if end of stream.
130: *
131: * @throws IOException
132: * If the stream is already closed or another IOException
133: * occurs.
134: */
135: @Override
136: public int read(byte[] buffer) throws IOException {
137: return read(buffer, 0, buffer.length);
138: }
139:
140: /**
141: * Reads at most <code>count</code> bytes from this FilterInputStream and
142: * stores them in byte array <code>buffer</code> starting at
143: * <code>offset</code>. Answer the number of bytes actually read or -1 if
144: * no bytes were read and end of stream was encountered. This implementation
145: * reads bytes from the target stream.
146: *
147: * @param buffer
148: * the byte array in which to store the read bytes.
149: * @param offset
150: * the offset in <code>buffer</code> to store the read bytes.
151: * @param count
152: * the maximum number of bytes to store in <code>buffer</code>.
153: * @return the number of bytes actually read or -1 if end of stream.
154: *
155: * @throws IOException
156: * If the stream is already closed or another IOException
157: * occurs.
158: */
159: @Override
160: public int read(byte[] buffer, int offset, int count)
161: throws IOException {
162: return in.read(buffer, offset, count);
163: }
164:
165: /**
166: * Reset this FilterInputStream to the last marked location. If the
167: * <code>readlimit</code> has been passed or no <code>mark</code> has
168: * been set, throw IOException. This implementation resets the target
169: * stream.
170: *
171: * @throws IOException
172: * If the stream is already closed or another IOException
173: * occurs.
174: */
175: @Override
176: public synchronized void reset() throws IOException {
177: in.reset();
178: }
179:
180: /**
181: * Skips <code>count</code> number of bytes in this InputStream.
182: * Subsequent <code>read()</code>'s will not return these bytes unless
183: * <code>reset()</code> is used. This implementation skips
184: * <code>count</code> number of bytes in the target stream.
185: *
186: * @param count
187: * the number of bytes to skip.
188: * @return the number of bytes actually skipped.
189: *
190: * @throws IOException
191: * If the stream is already closed or another IOException
192: * occurs.
193: */
194: @Override
195: public long skip(long count) throws IOException {
196: return in.skip(count);
197: }
198: }
|