home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / io / PushbackInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  1.3 KB  |  51 lines

  1. package java.io;
  2.  
  3. public class PushbackInputStream extends FilterInputStream {
  4.    protected int pushBack = -1;
  5.  
  6.    public PushbackInputStream(InputStream in) {
  7.       super(in);
  8.    }
  9.  
  10.    public int read() throws IOException {
  11.       int c = this.pushBack;
  12.       if (c != -1) {
  13.          this.pushBack = -1;
  14.       } else {
  15.          c = super.in.read();
  16.       }
  17.  
  18.       return c;
  19.    }
  20.  
  21.    public int read(byte[] bytes, int offset, int length) throws IOException {
  22.       if (this.pushBack != -1) {
  23.          if (length == 0) {
  24.             return 0;
  25.          } else {
  26.             bytes[offset] = (byte)this.pushBack;
  27.             this.pushBack = -1;
  28.             return 1;
  29.          }
  30.       } else {
  31.          return super.in.read(bytes, offset, length);
  32.       }
  33.    }
  34.  
  35.    public void unread(int ch) throws IOException {
  36.       if (this.pushBack != -1) {
  37.          throw new IOException("Attempt to unread more than one character!");
  38.       } else {
  39.          this.pushBack = ch;
  40.       }
  41.    }
  42.  
  43.    public int available() throws IOException {
  44.       return this.pushBack == -1 ? super.available() : super.available() + 1;
  45.    }
  46.  
  47.    public boolean markSupported() {
  48.       return false;
  49.    }
  50. }
  51.