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

  1. package java.io;
  2.  
  3. public class BufferedInputStream extends FilterInputStream {
  4.    protected byte[] buf;
  5.    protected int count;
  6.    protected int pos;
  7.    protected int markpos;
  8.    protected int marklimit;
  9.  
  10.    public BufferedInputStream(InputStream in) {
  11.       this(in, 2048);
  12.    }
  13.  
  14.    public BufferedInputStream(InputStream in, int size) {
  15.       super(in);
  16.       this.markpos = -1;
  17.       this.buf = new byte[size];
  18.    }
  19.  
  20.    private void fill() throws IOException {
  21.       if (this.markpos < 0) {
  22.          this.pos = 0;
  23.       } else if (this.pos >= this.buf.length) {
  24.          if (this.markpos > 0) {
  25.             int sz = this.pos - this.markpos;
  26.             System.arraycopy(this.buf, this.markpos, this.buf, 0, sz);
  27.             this.pos = sz;
  28.             this.markpos = 0;
  29.          } else if (this.buf.length >= this.marklimit) {
  30.             this.markpos = -1;
  31.             this.pos = 0;
  32.          } else {
  33.             int nsz = this.pos * 2;
  34.             if (nsz > this.marklimit) {
  35.                nsz = this.marklimit;
  36.             }
  37.  
  38.             byte[] nbuf = new byte[nsz];
  39.             System.arraycopy(this.buf, 0, nbuf, 0, this.pos);
  40.             this.buf = nbuf;
  41.          }
  42.       }
  43.  
  44.       int n = super.in.read(this.buf, this.pos, this.buf.length - this.pos);
  45.       this.count = n <= 0 ? 0 : n + this.pos;
  46.    }
  47.  
  48.    public synchronized int read() throws IOException {
  49.       if (this.pos >= this.count) {
  50.          this.fill();
  51.          if (this.count == 0) {
  52.             return -1;
  53.          }
  54.       }
  55.  
  56.       return this.buf[this.pos++] & 255;
  57.    }
  58.  
  59.    public synchronized int read(byte[] b, int off, int len) throws IOException {
  60.       int avail = this.count - this.pos;
  61.       if (avail <= 0) {
  62.          this.fill();
  63.          avail = this.count - this.pos;
  64.          if (avail <= 0) {
  65.             return -1;
  66.          }
  67.       }
  68.  
  69.       int cnt = avail < len ? avail : len;
  70.       System.arraycopy(this.buf, this.pos, b, off, cnt);
  71.       this.pos += cnt;
  72.       return cnt;
  73.    }
  74.  
  75.    public synchronized long skip(long n) throws IOException {
  76.       long avail = (long)(this.count - this.pos);
  77.       if (avail >= n) {
  78.          this.pos = (int)((long)this.pos + n);
  79.          return n;
  80.       } else {
  81.          this.pos = (int)((long)this.pos + avail);
  82.          return avail + super.in.skip(n - avail);
  83.       }
  84.    }
  85.  
  86.    public synchronized int available() throws IOException {
  87.       return this.count - this.pos + super.in.available();
  88.    }
  89.  
  90.    public synchronized void mark(int readlimit) {
  91.       this.marklimit = readlimit;
  92.       this.markpos = this.pos;
  93.    }
  94.  
  95.    public synchronized void reset() throws IOException {
  96.       if (this.markpos < 0) {
  97.          throw new IOException("Resetting to invalid mark");
  98.       } else {
  99.          this.pos = this.markpos;
  100.       }
  101.    }
  102.  
  103.    public boolean markSupported() {
  104.       return true;
  105.    }
  106. }
  107.