1 7 8 package java.io; 9 10 20 public 21 class BufferedOutputStream extends FilterOutputStream { 22 25 protected byte buf[]; 26 27 33 protected int count; 34 35 41 public BufferedOutputStream(OutputStream out) { 42 this(out, 8192); 43 } 44 45 54 public BufferedOutputStream(OutputStream out, int size) { 55 super(out); 56 if (size <= 0) { 57 throw new IllegalArgumentException ("Buffer size <= 0"); 58 } 59 buf = new byte[size]; 60 } 61 62 63 private void flushBuffer() throws IOException { 64 if (count > 0) { 65 out.write(buf, 0, count); 66 count = 0; 67 } 68 } 69 70 76 public synchronized void write(int b) throws IOException { 77 if (count >= buf.length) { 78 flushBuffer(); 79 } 80 buf[count++] = (byte)b; 81 } 82 83 99 public synchronized void write(byte b[], int off, int len) throws IOException { 100 if (len >= buf.length) { 101 104 flushBuffer(); 105 out.write(b, off, len); 106 return; 107 } 108 if (len > buf.length - count) { 109 flushBuffer(); 110 } 111 System.arraycopy(b, off, buf, count, len); 112 count += len; 113 } 114 115 122 public synchronized void flush() throws IOException { 123 flushBuffer(); 124 out.flush(); 125 } 126 } 127 | Popular Tags |