KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > util > ByteArrayOutputStreamWrapper


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.ui.core.util;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 import javax.servlet.ServletOutputStream JavaDoc;
24
25 /*
26  * @author llavandowska
27  *
28  * Implementation of ServletOutputStream that allows the filter to hold the
29  * Response content for insertion into the cache.
30  */

31 public class ByteArrayOutputStreamWrapper extends ServletOutputStream JavaDoc
32 {
33     protected OutputStream JavaDoc intStream;
34     protected ByteArrayOutputStream JavaDoc baStream;
35     protected boolean finallized = false;
36     protected boolean flushOnFinalizeOnly = true;
37
38     public ByteArrayOutputStreamWrapper(OutputStream JavaDoc outStream)
39     {
40         intStream = outStream;
41         baStream = new ByteArrayOutputStream JavaDoc();
42     }
43
44     public ByteArrayOutputStreamWrapper()
45     {
46         intStream = System.out;
47         baStream = new ByteArrayOutputStream JavaDoc();
48     }
49
50     public ByteArrayOutputStream JavaDoc getByteArrayStream()
51     {
52         return baStream;
53     }
54
55     public void setFinallized()
56     {
57         finallized = true;
58     }
59
60     public boolean isFinallized()
61     {
62         return finallized;
63     }
64
65
66     public void write(int i) throws java.io.IOException JavaDoc
67     {
68         baStream.write(i);
69     }
70
71     public void close() throws java.io.IOException JavaDoc
72     {
73         if (finallized) {
74             processStream();
75             intStream.close();
76         }
77     }
78
79     public void flush() throws java.io.IOException JavaDoc
80     {
81         if (baStream.size() != 0) {
82             if (!flushOnFinalizeOnly || finallized) {
83                 processStream();
84                 baStream = new ByteArrayOutputStream JavaDoc();
85             }
86         }
87     }
88
89     protected void processStream() throws java.io.IOException JavaDoc
90     {
91         intStream.write(baStream.toByteArray());
92         intStream.flush();
93     }
94     
95     public void clear()
96     {
97         baStream = new ByteArrayOutputStream JavaDoc();
98     }
99 }
100
Popular Tags