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

  1. package java.io;
  2.  
  3. public class ByteArrayInputStream extends InputStream {
  4.    protected byte[] buf;
  5.    protected int pos;
  6.    protected int count;
  7.  
  8.    public ByteArrayInputStream(byte[] buf) {
  9.       this.buf = buf;
  10.       this.pos = 0;
  11.       this.count = buf.length;
  12.    }
  13.  
  14.    public ByteArrayInputStream(byte[] buf, int offset, int length) {
  15.       this.buf = buf;
  16.       this.pos = offset;
  17.       this.count = Math.min(offset + length, buf.length);
  18.    }
  19.  
  20.    public synchronized int read() {
  21.       return this.pos < this.count ? this.buf[this.pos++] & 255 : -1;
  22.    }
  23.  
  24.    public synchronized int read(byte[] b, int off, int len) {
  25.       if (this.pos >= this.count) {
  26.          return -1;
  27.       } else {
  28.          if (this.pos + len > this.count) {
  29.             len = this.count - this.pos;
  30.          }
  31.  
  32.          if (len <= 0) {
  33.             return 0;
  34.          } else {
  35.             System.arraycopy(this.buf, this.pos, b, off, len);
  36.             this.pos += len;
  37.             return len;
  38.          }
  39.       }
  40.    }
  41.  
  42.    public synchronized long skip(long n) {
  43.       if ((long)this.pos + n > (long)this.count) {
  44.          n = (long)(this.count - this.pos);
  45.       }
  46.  
  47.       if (n < 0L) {
  48.          return 0L;
  49.       } else {
  50.          this.pos = (int)((long)this.pos + n);
  51.          return n;
  52.       }
  53.    }
  54.  
  55.    public synchronized int available() {
  56.       return this.count - this.pos;
  57.    }
  58.  
  59.    public synchronized void reset() {
  60.       this.pos = 0;
  61.    }
  62. }
  63.