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

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