1 19 20 package com.sslexplorer.replacementproxy; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.Serializable ; 25 import java.util.Date ; 26 import java.util.List ; 27 28 29 public class CacheingOutputStream extends OutputStream implements Serializable { 30 transient OutputStream out; 31 private byte buf[]; 32 private List headers; 33 private int idx; 34 private String contentType; 35 private Date cachedDate; 36 37 public CacheingOutputStream(OutputStream out, int size, List headers, String contentType) { 38 super(); 39 this.out = out; 40 cachedDate = new Date (); 41 this.contentType = contentType; 42 this.headers = headers; 43 buf = new byte[size]; 44 } 45 46 public Date getCachedDate() { 47 return cachedDate; 48 } 49 50 public byte[] getBytes() { 51 byte b[] = new byte[idx]; 52 System.arraycopy(buf, 0, b, 0, idx); 53 return b; 54 } 55 56 61 public void close() throws IOException { 62 out.close(); 63 } 64 65 70 public synchronized void write(byte[] b, int off, int len) throws IOException { 71 out.write(b, off, len); 72 if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { 73 throw new IndexOutOfBoundsException (); 74 } else if (len == 0) { 75 return; 76 } 77 int nidx = idx + len; 78 if (nidx > buf.length) { 79 byte newbuf[] = new byte[Math.max(buf.length << 1, nidx)]; 80 System.arraycopy(buf, 0, newbuf, 0, idx); 81 buf = newbuf; 82 } 83 System.arraycopy(b, off, buf, idx, len); 84 idx = nidx; 85 } 86 87 92 public synchronized void write(int b) throws IOException { 93 out.write(b); 94 int nidx = idx + 1; 95 if (nidx > buf.length) { 96 byte nbuf[] = new byte[Math.max(buf.length << 1, nidx)]; 97 System.arraycopy(buf, 0, nbuf, 0, idx); 98 buf = nbuf; 99 } 100 buf[idx] = (byte) b; 101 idx = nidx; 102 } 103 104 109 public void flush() throws IOException { 110 out.flush(); 111 } 112 115 public String getContentType() { 116 return contentType; 117 } 118 121 public void setContentType(String contentType) { 122 this.contentType = contentType; 123 } 124 127 public List getHeaders() { 128 return headers; 129 } 130 133 public void setHeaders(List headers) { 134 this.headers = headers; 135 } 136 } | Popular Tags |