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

  1. package java.io;
  2.  
  3. public class BufferedOutputStream extends FilterOutputStream {
  4.    protected byte[] buf;
  5.    protected int count;
  6.  
  7.    public BufferedOutputStream(OutputStream out) {
  8.       this(out, 512);
  9.    }
  10.  
  11.    public BufferedOutputStream(OutputStream out, int size) {
  12.       super(out);
  13.       this.buf = new byte[size];
  14.    }
  15.  
  16.    public synchronized void write(int b) throws IOException {
  17.       if (this.count == this.buf.length) {
  18.          this.flush();
  19.       }
  20.  
  21.       this.buf[this.count++] = (byte)b;
  22.    }
  23.  
  24.    public synchronized void write(byte[] b, int off, int len) throws IOException {
  25.       int avail = this.buf.length - this.count;
  26.       if (len <= avail) {
  27.          System.arraycopy(b, off, this.buf, this.count, len);
  28.          this.count += len;
  29.       } else {
  30.          this.flush();
  31.          super.out.write(b, off, len);
  32.       }
  33.    }
  34.  
  35.    public synchronized void flush() throws IOException {
  36.       super.out.write(this.buf, 0, this.count);
  37.       super.out.flush();
  38.       this.count = 0;
  39.    }
  40. }
  41.