KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > replacementproxy > CacheingOutputStream


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.replacementproxy;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.List JavaDoc;
27
28
29 public class CacheingOutputStream extends OutputStream JavaDoc implements Serializable JavaDoc {
30   transient OutputStream JavaDoc out;
31   private byte buf[];
32   private List JavaDoc headers;
33   private int idx;
34   private String JavaDoc contentType;
35   private Date JavaDoc cachedDate;
36
37   public CacheingOutputStream(OutputStream JavaDoc out, int size, List JavaDoc headers, String JavaDoc contentType) {
38     super();
39     this.out = out;
40     cachedDate = new Date JavaDoc();
41     this.contentType = contentType;
42     this.headers = headers;
43     buf = new byte[size];
44   }
45   
46   public Date JavaDoc 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   /*
57    * (non-Javadoc)
58    *
59    * @see java.io.Closeable#close()
60    */

61   public void close() throws IOException JavaDoc {
62     out.close();
63   }
64
65   /*
66    * (non-Javadoc)
67    *
68    * @see java.io.OutputStream#write(byte[], int, int)
69    */

70   public synchronized void write(byte[] b, int off, int len) throws IOException JavaDoc {
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 JavaDoc();
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   /*
88    * (non-Javadoc)
89    *
90    * @see java.io.OutputStream#write(int)
91    */

92   public synchronized void write(int b) throws IOException JavaDoc {
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   /*
105    * (non-Javadoc)
106    *
107    * @see java.io.Flushable#flush()
108    */

109   public void flush() throws IOException JavaDoc {
110     out.flush();
111   }
112   /**
113    * @return Returns the contentType.
114    */

115   public String JavaDoc getContentType() {
116     return contentType;
117   }
118   /**
119    * @param contentType The contentType to set.
120    */

121   public void setContentType(String JavaDoc contentType) {
122     this.contentType = contentType;
123   }
124   /**
125    * @return Returns the headers.
126    */

127   public List JavaDoc getHeaders() {
128     return headers;
129   }
130   /**
131    * @param headers The headers to set.
132    */

133   public void setHeaders(List JavaDoc headers) {
134     this.headers = headers;
135   }
136 }
Popular Tags