KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > cache > filter > CachingOutputStreamWrapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.appserv.web.cache.filter;
25
26 import java.io.IOException JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28
29 import javax.servlet.ServletOutputStream JavaDoc;
30
31 /**
32  * an output stream wrapper to cache response bytes
33  */

34 public class CachingOutputStreamWrapper extends ServletOutputStream JavaDoc {
35
36     ByteArrayOutputStream JavaDoc baos;
37
38     public CachingOutputStreamWrapper() {
39         this.baos = new ByteArrayOutputStream JavaDoc(4096);
40     }
41
42     /**
43      * Write the specified byte to our output stream.
44      *
45      * @param b The byte to be written
46      *
47      * @exception IOException if an input/output error occurs
48      */

49     public void write(int b) throws IOException JavaDoc {
50         baos.write(b);
51     }
52
53     /**
54      * Write <code>b.length</code> bytes from the specified byte array
55      * to our output stream.
56      *
57      * @param b The byte array to be written
58      *
59      * @exception IOException if an input/output error occurs
60      */

61     public void write(byte b[]) throws IOException JavaDoc {
62         baos.write(b, 0, b.length);
63     }
64
65     /**
66      * Write <code>len</code> bytes from the specified byte array, starting
67      * at the specified offset, to our output stream.
68      *
69      * @param b The byte array containing the bytes to be written
70      * @param off Zero-relative starting offset of the bytes to be written
71      * @param len The number of bytes to be written
72      *
73      * @exception IOException if an input/output error occurs
74      */

75     public void write(byte b[], int off, int len) throws IOException JavaDoc {
76         baos.write(b, off, len);
77     }
78
79     /**
80      * Flush any buffered data for this output stream, which also causes the
81      * response to be committed.
82      */

83     public void flush() throws IOException JavaDoc {
84         // nothing to do with cached bytes
85
}
86
87     /**
88      * Close this output stream, causing any buffered data to be flushed and
89      * any further output data to throw an IOException.
90      */

91     public void close() throws IOException JavaDoc {
92         // nothing to do with cached bytes
93
}
94
95     /**
96      * return the cached bytes
97      */

98     public byte[] getBytes() {
99         return baos.toByteArray();
100     }
101 }
102
Popular Tags