01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
21: import java.io.OutputStream;
22:
23: import javax.servlet.ServletOutputStream;
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 {
32: protected OutputStream intStream;
33: protected ByteArrayOutputStream baStream;
34: protected boolean finallized = false;
35: protected boolean flushOnFinalizeOnly = true;
36:
37: public ByteArrayOutputStreamWrapper(OutputStream outStream) {
38: intStream = outStream;
39: baStream = new ByteArrayOutputStream();
40: }
41:
42: public ByteArrayOutputStreamWrapper() {
43: intStream = System.out;
44: baStream = new ByteArrayOutputStream();
45: }
46:
47: public ByteArrayOutputStream getByteArrayStream() {
48: return baStream;
49: }
50:
51: public void setFinallized() {
52: finallized = true;
53: }
54:
55: public boolean isFinallized() {
56: return finallized;
57: }
58:
59: public void write(int i) throws java.io.IOException {
60: baStream.write(i);
61: }
62:
63: public void close() throws java.io.IOException {
64: if (finallized) {
65: processStream();
66: intStream.close();
67: }
68: }
69:
70: public void flush() throws java.io.IOException {
71: if (baStream.size() != 0) {
72: if (!flushOnFinalizeOnly || finallized) {
73: processStream();
74: baStream = new ByteArrayOutputStream();
75: }
76: }
77: }
78:
79: protected void processStream() throws java.io.IOException {
80: intStream.write(baStream.toByteArray());
81: intStream.flush();
82: }
83:
84: public void clear() {
85: baStream = new ByteArrayOutputStream();
86: }
87: }
|