home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / java / io / PushbackInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  1.6 KB  |  129 lines

  1. package java.io;
  2.  
  3. public class PushbackInputStream extends FilterInputStream {
  4.    protected byte[] buf;
  5.    protected int pos;
  6.  
  7.    private void ensureOpen() throws IOException {
  8.       if (super.in == null) {
  9.          throw new IOException("Stream closed");
  10.       }
  11.    }
  12.  
  13.    public PushbackInputStream(InputStream var1, int var2) {
  14.       super(var1);
  15.       if (var2 <= 0) {
  16.          throw new IllegalArgumentException("size <= 0");
  17.       } else {
  18.          this.buf = new byte[var2];
  19.          this.pos = var2;
  20.       }
  21.    }
  22.  
  23.    public PushbackInputStream(InputStream var1) {
  24.       this(var1, 1);
  25.    }
  26.  
  27.    public int read() throws IOException {
  28.       this.ensureOpen();
  29.       return this.pos < this.buf.length ? this.buf[this.pos++] & 255 : super.read();
  30.    }
  31.  
  32.    public int read(byte[] var1, int var2, int var3) throws IOException {
  33.       this.ensureOpen();
  34.       if (var2 >= 0 && var2 <= var1.length && var3 >= 0 && var2 + var3 <= var1.length && var2 + var3 >= 0) {
  35.          if (var3 == 0) {
  36.             return 0;
  37.          } else {
  38.             int var4 = this.buf.length - this.pos;
  39.             if (var4 > 0) {
  40.                if (var3 < var4) {
  41.                   var4 = var3;
  42.                }
  43.  
  44.                System.arraycopy(this.buf, this.pos, var1, var2, var4);
  45.                this.pos += var4;
  46.                var2 += var4;
  47.                var3 -= var4;
  48.             }
  49.  
  50.             if (var3 > 0) {
  51.                var3 = super.read(var1, var2, var3);
  52.                if (var3 == -1) {
  53.                   return var4 == 0 ? -1 : var4;
  54.                } else {
  55.                   return var4 + var3;
  56.                }
  57.             } else {
  58.                return var4;
  59.             }
  60.          }
  61.       } else {
  62.          throw new IndexOutOfBoundsException();
  63.       }
  64.    }
  65.  
  66.    public void unread(int var1) throws IOException {
  67.       this.ensureOpen();
  68.       if (this.pos == 0) {
  69.          throw new IOException("Push back buffer is full");
  70.       } else {
  71.          this.buf[--this.pos] = (byte)var1;
  72.       }
  73.    }
  74.  
  75.    public void unread(byte[] var1, int var2, int var3) throws IOException {
  76.       this.ensureOpen();
  77.       if (var3 > this.pos) {
  78.          throw new IOException("Push back buffer is full");
  79.       } else {
  80.          this.pos -= var3;
  81.          System.arraycopy(var1, var2, this.buf, this.pos, var3);
  82.       }
  83.    }
  84.  
  85.    public void unread(byte[] var1) throws IOException {
  86.       this.unread(var1, 0, var1.length);
  87.    }
  88.  
  89.    public int available() throws IOException {
  90.       this.ensureOpen();
  91.       return this.buf.length - this.pos + super.available();
  92.    }
  93.  
  94.    public long skip(long var1) throws IOException {
  95.       this.ensureOpen();
  96.       if (var1 <= 0L) {
  97.          return 0L;
  98.       } else {
  99.          long var3 = (long)(this.buf.length - this.pos);
  100.          if (var3 > 0L) {
  101.             if (var1 < var3) {
  102.                var3 = var1;
  103.             }
  104.  
  105.             this.pos = (int)((long)this.pos + var3);
  106.             var1 -= var3;
  107.          }
  108.  
  109.          if (var1 > 0L) {
  110.             var3 += super.skip(var1);
  111.          }
  112.  
  113.          return var3;
  114.       }
  115.    }
  116.  
  117.    public boolean markSupported() {
  118.       return false;
  119.    }
  120.  
  121.    public synchronized void close() throws IOException {
  122.       if (super.in != null) {
  123.          super.in.close();
  124.          super.in = null;
  125.          this.buf = null;
  126.       }
  127.    }
  128. }
  129.