home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / FilterInputStream.java < prev    next >
Text File  |  1996-05-03  |  5KB  |  149 lines

  1. /*
  2.  * @(#)FilterInputStream.java    1.13 95/12/19 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. /**
  23.  * Abstract class representing a filtered input stream of bytes.
  24.  * This class is the basis for enhancing input stream functionality.
  25.  * It allows multiple input stream filters to be chained together,
  26.  * each providing additional functionality.
  27.  * @version     1.13, 19 Dec 1995
  28.  * @author    Jonathan Payne
  29.  */
  30. public
  31. class FilterInputStream extends InputStream {
  32.     /**
  33.      * The actual input stream.
  34.      */
  35.     protected InputStream in;
  36.  
  37.     /**
  38.      * Creates an input stream filter.
  39.      * @param in    the input stream
  40.      */
  41.     protected FilterInputStream(InputStream in) {
  42.     this.in = in;
  43.     }
  44.  
  45.     /**
  46.      * Reads a byte. Will block if no input is available.
  47.      * @return     the byte read, or -1 if the end of the
  48.      *        stream is reached.
  49.      * @exception IOException If an I/O error has occurred.
  50.      */
  51.     public int read() throws IOException {
  52.     return in.read();
  53.     }
  54.  
  55.     /**
  56.      * Reads into an array of bytes.
  57.      * Blocks until some input is available.
  58.      * @param b    the buffer into which the data is read
  59.      * @return  the actual number of bytes read. Returns -1
  60.      *         when the end of the stream is reached.
  61.      * @exception IOException If an I/O error has occurred.
  62.      */
  63.     public int read(byte b[]) throws IOException {
  64.     return read(b, 0, b.length);
  65.     }
  66.  
  67.     /**
  68.      * Reads into an array of bytes.
  69.      * Blocks until some input is available.
  70.      * This method should be overridden in a subclass for
  71.      * efficiency (the default implementation reads 1 byte
  72.      * at a time).
  73.      * @param b    the buffer into which the data is read
  74.      * @param off the start offset of the data
  75.      * @param len the maximum number of bytes read
  76.      * @return  the actual number of bytes read. Returns -1 
  77.      *         when the end of the stream is reached.
  78.      * @exception IOException If an I/O error has occurred.
  79.      */
  80.     public int read(byte b[], int off, int len) throws IOException {
  81.     return in.read(b, off, len);
  82.     }
  83.  
  84.     /**
  85.      * Skips bytes of input.
  86.      * @param n     bytes to be skipped
  87.      * @return    actual number of bytes skipped
  88.      * @exception IOException If an I/O error has occurred.
  89.      */
  90.     public long skip(long n) throws IOException {
  91.     return in.skip(n);
  92.     }
  93.  
  94.     /**
  95.      * Returns the number of bytes that can be read
  96.      * without blocking.
  97.      * @return the number of available bytes
  98.      */
  99.     public int available() throws IOException {
  100.     return in.available();
  101.     }
  102.  
  103.     /**
  104.      * Closes the input stream. Must be called
  105.      * to release any resources associated with
  106.      * the stream.
  107.      * @exception IOException If an I/O error has occurred.
  108.      */
  109.     public void close() throws IOException {
  110.     in.close();
  111.     }
  112.  
  113.     /**
  114.      * Marks the current position in the input stream.  A subsequent
  115.      * call to reset() will reposition the stream at the last
  116.      * marked position so that subsequent reads will re-read
  117.      * the same bytes.  The stream promises to allow readlimit bytes
  118.      * to be read before the mark position gets invalidated.
  119.      * @param readlimit the maximum limit of bytes allowed tobe read before the
  120.      * mark position becomes invalid.
  121.      */
  122.     public synchronized void mark(int readlimit) {
  123.     in.mark(readlimit);
  124.     }
  125.  
  126.     /**
  127.      * Repositions the stream to the last marked position.  If the
  128.      * stream has not been marked, or if the mark has been invalidated,
  129.      * an IOException is thrown.  Stream marks are intended to be used in
  130.      * situations where you need to read ahead a little to see what's in
  131.      * the stream.  Often this is most easily done by invoking some
  132.      * general parser.  If the stream is of the type handled by the
  133.      * parse, it just chugs along happily.  If the stream is not of
  134.      * that type, the parser should toss an exception when it fails.
  135.      * If this happens within readlimit bytes,  it allows the outer
  136.      * code to reset the stream and try another parser.
  137.      */
  138.     public synchronized void reset() throws IOException {
  139.     in.reset();
  140.     }
  141.  
  142.     /**
  143.      * Returns true if this stream type supports mark/reset.
  144.      */
  145.     public boolean markSupported() {
  146.     return in.markSupported();
  147.     }
  148. }
  149.