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

  1. /*
  2.  * @(#)PushbackInputStream.java    1.11 95/08/10 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.  * An input stream that has a 1 byte push back buffer.
  24.  *
  25.  * @version     1.11, 10 Aug 1995
  26.  * @author    Jonathan Payne
  27.  */
  28. public
  29. class PushbackInputStream extends FilterInputStream {
  30.     /**
  31.      * Push back character.
  32.      */
  33.     protected int    pushBack = -1;
  34.  
  35.     /**
  36.      * Creates a PushbackInputStream.
  37.      * @param in the input stream
  38.      */
  39.     public PushbackInputStream(InputStream in) {
  40.     super(in);
  41.     }
  42.  
  43.     /**
  44.      * Reads a byte of data. This method will block if no input is 
  45.      * available.
  46.      * @return     the byte read, or -1 if the end of the
  47.      *        stream is reached.
  48.      * @exception IOException If an I/O error has occurred.
  49.      */
  50.     public int read() throws IOException {
  51.     int c = pushBack;
  52.  
  53.     if (c != -1) {
  54.         pushBack = -1;
  55.     } else {
  56.         c = in.read();
  57.     }
  58.     return c;
  59.     }
  60.  
  61.     /**
  62.      * Reads into an array of bytes.  This method 
  63.      * blocks until some input is available.
  64.      * @param b    the buffer into which the data is read
  65.      * @param off the start offset of the data
  66.      * @param len the maximum number of bytes read
  67.      * @return  the actual number of bytes read, -1 is
  68.      *         returned when the end of the stream is reached.
  69.      * @exception IOException If an I/O error has occurred.
  70.      */
  71.     public int read(byte bytes[], int offset, int length) throws IOException {
  72.     if (pushBack != -1) {
  73.         if (length == 0) {
  74.         return 0;
  75.         }
  76.         bytes[offset] = (byte)pushBack;
  77.         pushBack = -1;
  78.         return 1;
  79.     }
  80.     return in.read(bytes, offset, length);
  81.     }
  82.  
  83.     /**
  84.      * Pushes back a character. 
  85.      * @param ch the character to push back.
  86.      * @exception IOException If an attempt to push back more than one 
  87.      * character is made.
  88.      */
  89.     public void unread(int ch) throws IOException {
  90.     if (pushBack != -1) {
  91.         throw new IOException("Attempt to unread more than one character!");
  92.     }
  93.     pushBack = ch;
  94.     }
  95.  
  96.     /**
  97.      * Returns the number of bytes that can be read.
  98.      * without blocking.
  99.      */
  100.     public int available() throws IOException {
  101.     return (pushBack == -1) ? super.available() : super.available() + 1;
  102.     }
  103.  
  104.     /**
  105.      * Returns true if this stream type supports mark/reset.
  106.      */
  107.     public boolean markSupported() {
  108.     return false;
  109.     }
  110. }
  111.