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 / BufferedInputStream.class (.txt) next >
Encoding:
Java Class File  |  1979-12-31  |  2.1 KB  |  175 lines

  1. package java.io;
  2.  
  3. public class BufferedInputStream extends FilterInputStream {
  4.    private static int defaultBufferSize = 2048;
  5.    protected byte[] buf;
  6.    protected int count;
  7.    protected int pos;
  8.    protected int markpos;
  9.    protected int marklimit;
  10.  
  11.    private void ensureOpen() throws IOException {
  12.       if (super.in == null) {
  13.          throw new IOException("Stream closed");
  14.       }
  15.    }
  16.  
  17.    public BufferedInputStream(InputStream var1) {
  18.       this(var1, defaultBufferSize);
  19.    }
  20.  
  21.    public BufferedInputStream(InputStream var1, int var2) {
  22.       super(var1);
  23.       this.markpos = -1;
  24.       if (var2 <= 0) {
  25.          throw new IllegalArgumentException("Buffer size <= 0");
  26.       } else {
  27.          this.buf = new byte[var2];
  28.       }
  29.    }
  30.  
  31.    private void fill() throws IOException {
  32.       if (this.markpos < 0) {
  33.          this.pos = 0;
  34.       } else if (this.pos >= this.buf.length) {
  35.          if (this.markpos > 0) {
  36.             int var1 = this.pos - this.markpos;
  37.             System.arraycopy(this.buf, this.markpos, this.buf, 0, var1);
  38.             this.pos = var1;
  39.             this.markpos = 0;
  40.          } else if (this.buf.length >= this.marklimit) {
  41.             this.markpos = -1;
  42.             this.pos = 0;
  43.          } else {
  44.             int var3 = this.pos * 2;
  45.             if (var3 > this.marklimit) {
  46.                var3 = this.marklimit;
  47.             }
  48.  
  49.             byte[] var2 = new byte[var3];
  50.             System.arraycopy(this.buf, 0, var2, 0, this.pos);
  51.             this.buf = var2;
  52.          }
  53.       }
  54.  
  55.       this.count = this.pos;
  56.       int var4 = super.in.read(this.buf, this.pos, this.buf.length - this.pos);
  57.       if (var4 > 0) {
  58.          this.count = var4 + this.pos;
  59.       }
  60.  
  61.    }
  62.  
  63.    public synchronized int read() throws IOException {
  64.       this.ensureOpen();
  65.       if (this.pos >= this.count) {
  66.          this.fill();
  67.          if (this.pos >= this.count) {
  68.             return -1;
  69.          }
  70.       }
  71.  
  72.       return this.buf[this.pos++] & 255;
  73.    }
  74.  
  75.    private int read1(byte[] var1, int var2, int var3) throws IOException {
  76.       int var4 = this.count - this.pos;
  77.       if (var4 <= 0) {
  78.          if (var3 >= this.buf.length && this.markpos < 0) {
  79.             return super.in.read(var1, var2, var3);
  80.          }
  81.  
  82.          this.fill();
  83.          var4 = this.count - this.pos;
  84.          if (var4 <= 0) {
  85.             return -1;
  86.          }
  87.       }
  88.  
  89.       int var5 = var4 < var3 ? var4 : var3;
  90.       System.arraycopy(this.buf, this.pos, var1, var2, var5);
  91.       this.pos += var5;
  92.       return var5;
  93.    }
  94.  
  95.    public synchronized int read(byte[] var1, int var2, int var3) throws IOException {
  96.       this.ensureOpen();
  97.       if ((var2 | var3 | var2 + var3 | var1.length - (var2 + var3)) < 0) {
  98.          throw new IndexOutOfBoundsException();
  99.       } else if (var3 == 0) {
  100.          return 0;
  101.       } else {
  102.          int var4 = this.read1(var1, var2, var3);
  103.          if (var4 <= 0) {
  104.             return var4;
  105.          } else {
  106.             while(var4 < var3 && super.in.available() > 0) {
  107.                int var5 = this.read1(var1, var2 + var4, var3 - var4);
  108.                if (var5 <= 0) {
  109.                   break;
  110.                }
  111.  
  112.                var4 += var5;
  113.             }
  114.  
  115.             return var4;
  116.          }
  117.       }
  118.    }
  119.  
  120.    public synchronized long skip(long var1) throws IOException {
  121.       this.ensureOpen();
  122.       if (var1 <= 0L) {
  123.          return 0L;
  124.       } else {
  125.          long var3 = (long)(this.count - this.pos);
  126.          if (var3 <= 0L) {
  127.             if (this.markpos < 0) {
  128.                return super.in.skip(var1);
  129.             }
  130.  
  131.             this.fill();
  132.             var3 = (long)(this.count - this.pos);
  133.             if (var3 <= 0L) {
  134.                return 0L;
  135.             }
  136.          }
  137.  
  138.          long var5 = var3 < var1 ? var3 : var1;
  139.          this.pos = (int)((long)this.pos + var5);
  140.          return var5;
  141.       }
  142.    }
  143.  
  144.    public synchronized int available() throws IOException {
  145.       this.ensureOpen();
  146.       return this.count - this.pos + super.in.available();
  147.    }
  148.  
  149.    public synchronized void mark(int var1) {
  150.       this.marklimit = var1;
  151.       this.markpos = this.pos;
  152.    }
  153.  
  154.    public synchronized void reset() throws IOException {
  155.       this.ensureOpen();
  156.       if (this.markpos < 0) {
  157.          throw new IOException("Resetting to invalid mark");
  158.       } else {
  159.          this.pos = this.markpos;
  160.       }
  161.    }
  162.  
  163.    public boolean markSupported() {
  164.       return true;
  165.    }
  166.  
  167.    public void close() throws IOException {
  168.       if (super.in != null) {
  169.          super.in.close();
  170.          super.in = null;
  171.          this.buf = null;
  172.       }
  173.    }
  174. }
  175.