home *** CD-ROM | disk | FTP | other *** search
- package java.io;
-
- public class BufferedInputStream extends FilterInputStream {
- protected byte[] buf;
- protected int count;
- protected int pos;
- protected int markpos;
- protected int marklimit;
-
- public BufferedInputStream(InputStream in) {
- this(in, 2048);
- }
-
- public BufferedInputStream(InputStream in, int size) {
- super(in);
- this.markpos = -1;
- this.buf = new byte[size];
- }
-
- private void fill() throws IOException {
- if (this.markpos < 0) {
- this.pos = 0;
- } else if (this.pos >= this.buf.length) {
- if (this.markpos > 0) {
- int sz = this.pos - this.markpos;
- System.arraycopy(this.buf, this.markpos, this.buf, 0, sz);
- this.pos = sz;
- this.markpos = 0;
- } else if (this.buf.length >= this.marklimit) {
- this.markpos = -1;
- this.pos = 0;
- } else {
- int nsz = this.pos * 2;
- if (nsz > this.marklimit) {
- nsz = this.marklimit;
- }
-
- byte[] nbuf = new byte[nsz];
- System.arraycopy(this.buf, 0, nbuf, 0, this.pos);
- this.buf = nbuf;
- }
- }
-
- int n = super.in.read(this.buf, this.pos, this.buf.length - this.pos);
- this.count = n <= 0 ? 0 : n + this.pos;
- }
-
- public synchronized int read() throws IOException {
- if (this.pos >= this.count) {
- this.fill();
- if (this.count == 0) {
- return -1;
- }
- }
-
- return this.buf[this.pos++] & 255;
- }
-
- public synchronized int read(byte[] b, int off, int len) throws IOException {
- int avail = this.count - this.pos;
- if (avail <= 0) {
- this.fill();
- avail = this.count - this.pos;
- if (avail <= 0) {
- return -1;
- }
- }
-
- int cnt = avail < len ? avail : len;
- System.arraycopy(this.buf, this.pos, b, off, cnt);
- this.pos += cnt;
- return cnt;
- }
-
- public synchronized long skip(long n) throws IOException {
- long avail = (long)(this.count - this.pos);
- if (avail >= n) {
- this.pos = (int)((long)this.pos + n);
- return n;
- } else {
- this.pos = (int)((long)this.pos + avail);
- return avail + super.in.skip(n - avail);
- }
- }
-
- public synchronized int available() throws IOException {
- return this.count - this.pos + super.in.available();
- }
-
- public synchronized void mark(int readlimit) {
- this.marklimit = readlimit;
- this.markpos = this.pos;
- }
-
- public synchronized void reset() throws IOException {
- if (this.markpos < 0) {
- throw new IOException("Resetting to invalid mark");
- } else {
- this.pos = this.markpos;
- }
- }
-
- public boolean markSupported() {
- return true;
- }
- }
-