1 17 package com.sun.org.apache.xml.internal.security.utils; 18 19 import java.io.IOException ; 20 import java.io.OutputStream ; 21 22 27 public class UnsyncBufferedOutputStream extends OutputStream { 28 final OutputStream out; 29 static final int size=8*1024; 30 final byte[] buf=new byte[size]; 31 int pointer=0; 32 36 public UnsyncBufferedOutputStream(OutputStream out) { 37 this.out=out; 38 } 39 40 41 public void write(byte[] arg0) throws IOException { 42 write(arg0,0,arg0.length); 43 } 44 45 46 public void write(byte[] arg0, int arg1, int len) throws IOException { 47 int newLen=pointer+len; 48 if (newLen> size) { 49 flushBuffer(); 50 if (len>size) { 51 out.write(arg0,arg1,len); 52 return; 53 } 54 newLen=len; 55 } 56 System.arraycopy(arg0,arg1,buf,pointer,len); 57 pointer=newLen; 58 } 59 60 private final void flushBuffer() throws IOException { 61 if (pointer>0) 62 out.write(buf,0,pointer); 63 pointer=0; 64 65 } 66 67 68 public void write(int arg0) throws IOException { 69 if (pointer>= size) { 70 flushBuffer(); 71 } 72 buf[pointer++]=(byte)arg0; 73 74 } 75 76 77 public void flush() throws IOException { 78 flushBuffer(); 79 out.flush(); 80 } 81 82 83 public void close() throws IOException { 84 flush(); 85 } 86 87 } 88 | Popular Tags |