1 16 package org.apache.cocoon.caching; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 21 29 30 public final class CachingOutputStream 31 extends OutputStream { 32 33 private OutputStream receiver; 34 35 36 private byte buf[]; 37 38 39 private int bufCount; 40 41 public CachingOutputStream(OutputStream os) { 42 this.receiver = os; 43 this.buf = new byte[1024]; 44 this.bufCount = 0; 45 } 46 47 public byte[] getContent() { 48 byte newbuf[] = new byte[this.bufCount]; 49 System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount); 50 return newbuf; 51 } 52 53 public void write(int b) throws IOException { 54 this.receiver.write(b); 55 int newcount = this.bufCount + 1; 56 if (newcount > this.buf.length) { 57 byte newbuf[] = new byte[Math.max(this.buf.length << 1, newcount)]; 58 System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount); 59 this.buf = newbuf; 60 } 61 this.buf[this.bufCount] = (byte)b; 62 this.bufCount = newcount; 63 } 64 65 public void write( byte b[] ) throws IOException { 66 this.write(b, 0, b.length); 67 } 68 69 public void write(byte b[], int off, int len) throws IOException { 70 this.receiver.write(b, off, len); 71 if (len == 0) return; 72 int newcount = this.bufCount + (len-off); 73 if (newcount > this.buf.length) { 74 byte newbuf[] = new byte[Math.max(this.buf.length << 1, newcount)]; 75 System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount); 76 this.buf = newbuf; 77 } 78 System.arraycopy(b, off, this.buf, this.bufCount, len); 79 this.bufCount = newcount; 80 } 81 82 public void flush() throws IOException { 83 this.receiver.flush(); 84 } 85 86 public void close() throws IOException { 87 this.receiver.close(); 88 } 89 90 91 } 92 | Popular Tags |