001    /* FilterInputStream.java -- Base class for classes that filter input
002       Copyright (C) 1998, 1999, 2001, 2005  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010     
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.io;
040    
041    /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
042     * "The Java Language Specification", ISBN 0-201-63451-1
043     * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
044     * Status:  Believed complete and correct.
045     */
046    
047    /**
048      * This is the common superclass of all standard classes that filter 
049      * input.  It acts as a layer on top of an underlying <code>InputStream</code>
050      * and simply redirects calls made to it to the subordinate InputStream
051      * instead.  Subclasses of this class perform additional filtering
052      * functions in addition to simply redirecting the call.
053      * <p>
054      * This class is not abstract.  However, since it only redirects calls
055      * to a subordinate <code>InputStream</code> without adding any functionality
056      * on top of it, this class should not be used directly.  Instead, various
057      * subclasses of this class should be used.  This is enforced with a
058      * protected constructor.  Do not try to hack around it.
059      * <p>
060      * When creating a subclass of <code>FilterInputStream</code>, override the
061      * appropriate methods to implement the desired filtering.  However, note
062      * that the <code>read(byte[])</code> method does not need to be overridden
063      * as this class redirects calls to that method to 
064      * <code>read(byte[], int, int)</code> instead of to the subordinate
065      * <code>InputStream read(byte[])</code> method.
066      *
067      * @author Aaron M. Renn (arenn@urbanophile.com)
068      * @author Warren Levy (warrenl@cygnus.com)
069      */
070    public class FilterInputStream extends InputStream
071    {
072      /**
073        * This is the subordinate <code>InputStream</code> to which method calls
074        * are redirected
075        */
076      protected InputStream in;
077    
078      /**
079        * Create a <code>FilterInputStream</code> with the specified subordinate
080        * <code>InputStream</code>.
081        *
082        * @param in The subordinate <code>InputStream</code>
083        */
084      protected FilterInputStream(InputStream in)
085      {
086        this.in = in;
087      }
088    
089      /**
090        * Calls the <code>in.mark(int)</code> method.
091        *
092        * @param readlimit The parameter passed to <code>in.mark(int)</code>
093        */
094      public void mark(int readlimit)
095      {
096        in.mark(readlimit);
097      }
098    
099      /**
100        * Calls the <code>in.markSupported()</code> method.
101        *
102        * @return <code>true</code> if mark/reset is supported, <code>false</code>
103        *         otherwise
104        */
105      public boolean markSupported()
106      {
107        return in.markSupported();
108      }
109    
110      /**
111        * Calls the <code>in.reset()</code> method.
112        *
113        * @exception IOException If an error occurs
114        */
115      public void reset() throws IOException
116      {
117        in.reset();
118      }
119    
120      /**
121        * Calls the <code>in.available()</code> method.
122        *
123        * @return The value returned from <code>in.available()</code>
124        *
125        * @exception IOException If an error occurs
126        */
127      public int available() throws IOException
128      {
129        return in.available();
130      }
131    
132      /**
133        * Calls the <code>in.skip(long)</code> method
134        *
135        * @param numBytes The requested number of bytes to skip. 
136        *
137        * @return The value returned from <code>in.skip(long)</code>
138        *
139        * @exception IOException If an error occurs
140        */
141      public long skip(long numBytes) throws IOException
142      {
143        return in.skip(numBytes);
144      }
145    
146      /**
147        * Calls the <code>in.read()</code> method
148        *
149        * @return The value returned from <code>in.read()</code>
150        *
151        * @exception IOException If an error occurs
152        */
153      public int read() throws IOException
154      {
155        return in.read();
156      }
157    
158      /**
159        * Calls the <code>read(byte[], int, int)</code> overloaded method.  
160        * Note that 
161        * this method does not redirect its call directly to a corresponding
162        * method in <code>in</code>.  This allows subclasses to override only the
163        * three argument version of <code>read</code>.
164        *
165        * @param buf The buffer to read bytes into
166        *
167        * @return The value retured from <code>in.read(byte[], int, int)</code>
168        *
169        * @exception IOException If an error occurs
170        */
171      public int read(byte[] buf) throws IOException
172      {
173        return read(buf, 0, buf.length);
174      }
175    
176      /**
177        * Calls the <code>in.read(byte[], int, int)</code> method.
178        *
179        * @param buf The buffer to read bytes into
180        * @param offset The index into the buffer to start storing bytes
181        * @param len The maximum number of bytes to read.
182        *
183        * @return The value retured from <code>in.read(byte[], int, int)</code>
184        *
185        * @exception IOException If an error occurs
186        */
187      public int read(byte[] buf, int offset, int len) throws IOException
188      {
189        return in.read(buf, offset, len);
190      }
191    
192      /**
193        * This method closes the input stream by closing the input stream that
194        * this object is filtering.  Future attempts to access this stream may
195        * throw an exception.
196        * 
197        * @exception IOException If an error occurs
198        */
199      public void close() throws IOException
200      {
201        in.close();
202      }
203    }