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

  1. /*
  2.  * @(#)InputStream.java    1.16 95/12/19 Arthur van Hoff
  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.  * An abstract class representing an input stream of bytes.
  24.  * All InputStreams are based on this class.
  25.  * @see        OutputStream
  26.  * @see        FilterInputStream
  27.  * @see        BufferedInputStream
  28.  * @see        DataInputStream
  29.  * @see        ByteArrayInputStream
  30.  * @see        PushbackInputStream
  31.  * @version     1.16, 19 Dec 1995
  32.  * @author    Arthur van Hoff
  33.  */
  34. public abstract class InputStream {
  35.     /**
  36.      * Reads a byte of data. This method will block if no input is 
  37.      * available.
  38.      * @return     the byte read, or -1 if the end of the
  39.      *        stream is reached.
  40.      * @exception IOException If an I/O error has occurred.
  41.      */
  42.     public abstract int read() throws IOException;
  43.  
  44.     /**
  45.      * Reads into an array of bytes.  This method will
  46.      * block until some input is available.
  47.      * @param b    the buffer into which the data is read
  48.      * @return  the actual number of bytes read, -1 is
  49.      *         returned when the end of the stream is reached.
  50.      * @exception IOException If an I/O error has occurred.
  51.      */
  52.     public int read(byte b[]) throws IOException {
  53.     return read(b, 0, b.length);
  54.     }
  55.  
  56.     /**
  57.      * Reads into an array of bytes.  This method will
  58.      * block until some input is available.
  59.      * @param b    the buffer into which the data is read
  60.      * @param off the start offset of the data
  61.      * @param len the maximum number of bytes read
  62.      * @return  the actual number of bytes read, -1 is
  63.      *         returned when the end of the stream is reached.
  64.      * @exception IOException If an I/O error has occurred.
  65.      */
  66.     public int read(byte b[], int off, int len) throws IOException {
  67.     if (len <= 0) {
  68.         return 0;
  69.     }
  70.  
  71.     int c = read();
  72.     if (c == -1) {
  73.         return -1;
  74.     }
  75.     b[off] = (byte)c;
  76.  
  77.     int i = 1;
  78.     try {
  79.         for (; i < len ; i++) {
  80.         c = read();
  81.         if (c == -1) {
  82.             break;
  83.         }
  84.         if (b != null) {
  85.             b[off + i] = (byte)c;
  86.         }
  87.         }
  88.     } catch (IOException ee) {
  89.     }
  90.     return i;
  91.     }
  92.  
  93.     /**
  94.      * Skips n bytes of input.
  95.      * @param n the number of bytes to be skipped
  96.      * @return    the actual number of bytes skipped.
  97.      * @exception IOException If an I/O error has occurred.
  98.      */
  99.     public long skip(long n) throws IOException {
  100.     byte data[] = new byte[(int)n];
  101.     return read(data);
  102.     }
  103.  
  104.     /**
  105.      * Returns the number of bytes that can be read
  106.      * without blocking.
  107.      * @return the number of available bytes.
  108.      */
  109.     public int available() throws IOException {
  110.     return 0;
  111.     }
  112.  
  113.     /**
  114.      * Closes the input stream. Must be called
  115.      * to release any resources associated with
  116.      * the stream.
  117.      * @exception IOException If an I/O error has occurred.
  118.      */
  119.     public void close() throws IOException {}
  120.  
  121.     /**
  122.      * Marks the current position in the input stream.  A subsequent
  123.      * call to reset() will reposition the stream at the last
  124.      * marked position so that subsequent reads will re-read
  125.      * the same bytes.  The stream promises to allow readlimit bytes
  126.      * to be read before the mark position gets invalidated.
  127.      *
  128.      * @param readlimit the maximum limit of bytes allowed to be read
  129.      * before the mark position becomes invalid.
  130.      */
  131.     public synchronized void mark(int readlimit) {}
  132.  
  133.     /**
  134.      * Repositions the stream to the last marked position.  If the
  135.      * stream has not been marked, or if the mark has been invalidated,
  136.      * an IOException is thrown.  Stream marks are intended to be used in
  137.      * situations where you need to read ahead a little to see what's in
  138.      * the stream.  Often this is most easily done by invoking some
  139.      * general parser.  If the stream is of the type handled by the
  140.      * parser, it just chugs along happily.  If the stream is not of
  141.      * that type, the parser should toss an exception when it fails,
  142.      * which, if it happens within readlimit bytes, allows the outer
  143.      * code to reset the stream and try another parser.
  144.      * @exception IOException If the stream has not been marked or 
  145.      * if the mark has been invalidated.
  146.      */
  147.     public synchronized void reset() throws IOException {
  148.     throw new IOException("mark/reset not supported");
  149.     }
  150.  
  151.     /**
  152.      * Returns a boolean indicating whether or not this stream type 
  153.      * supports mark/reset.
  154.      * @return true if this stream type supports mark/reset; false
  155.      * otherwise.
  156.      */
  157.     public boolean markSupported() {
  158.     return false;
  159.     }
  160. }
  161.