| 1 10 11 package com.go.trove.io; 12 13 import java.io.*; 14 15 21 26 public class FastBufferedOutputStream extends FilterOutputStream { 27 private byte[] mBuffer; 30 private int mCount; 31 32 public FastBufferedOutputStream(OutputStream out) { 33 this(out, 512); 34 } 35 36 public FastBufferedOutputStream(OutputStream out, int size) { 37 super(out); 38 if (size <= 0) { 39 throw new IllegalArgumentException ("Buffer size <= 0"); 40 } 41 mBuffer = new byte[size]; 42 } 43 44 private void flushBuffer() throws IOException { 45 if (mCount > 0) { 46 out.write(mBuffer, 0, mCount); 47 mCount = 0; 48 } 49 } 50 51 public void write(int b) throws IOException { 52 if (mCount >= mBuffer.length) { 53 flushBuffer(); 54 } 55 mBuffer[mCount++] = (byte)b; 56 } 57 58 public void write(byte b[], int off, int len) throws IOException { 59 if (len >= mBuffer.length) { 60 flushBuffer(); 61 out.write(b, off, len); 62 return; 63 } 64 if (len > mBuffer.length - mCount) { 65 flushBuffer(); 66 } 67 System.arraycopy(b, off, mBuffer, mCount, len); 68 mCount += len; 69 } 70 71 public synchronized void flush() throws IOException { 72 flushBuffer(); 73 out.flush(); 74 } 75 } 76 | Popular Tags |