KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > filter > ByteArrayOutputStreamWrapper


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet.filter;
4
5 import jodd.io.FastByteArrayOutputStream;
6
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.OutputStream JavaDoc;
9
10 import javax.servlet.ServletOutputStream JavaDoc;
11
12 /**
13  * Implementation of ServletOutputStream that allows the filter to hold the
14  * Response content for insertion into the cache.
15  */

16 public class ByteArrayOutputStreamWrapper extends ServletOutputStream JavaDoc {
17     protected OutputStream intStream;
18     protected FastByteArrayOutputStream baStream;
19     protected boolean finallized = false;
20     protected boolean flushOnFinalizeOnly = true;
21
22     public ByteArrayOutputStreamWrapper(OutputStream outStream) {
23         intStream = outStream;
24         baStream = new FastByteArrayOutputStream();
25     }
26
27     public ByteArrayOutputStreamWrapper() {
28         intStream = System.out;
29         baStream = new FastByteArrayOutputStream();
30     }
31
32     public FastByteArrayOutputStream getByteArrayStream() {
33         return baStream;
34     }
35
36     public void setFinallized() {
37         finallized = true;
38     }
39
40     public boolean isFinallized() {
41         return finallized;
42     }
43
44
45     public void write(int i) throws java.io.IOException JavaDoc {
46         baStream.write(i);
47     }
48
49     public void close() throws java.io.IOException JavaDoc {
50         if (finallized) {
51             processStream();
52             intStream.close();
53         }
54     }
55     public void reset() {
56         baStream.reset();
57     }
58
59     public void flush() throws java.io.IOException JavaDoc {
60         if (baStream.size() != 0) {
61             if (!flushOnFinalizeOnly || finallized) {
62                 processStream();
63                 baStream = new FastByteArrayOutputStream();
64             }
65         }
66     }
67
68     protected void processStream() throws java.io.IOException JavaDoc {
69         intStream.write(baStream.toByteArray());
70         intStream.flush();
71     }
72
73     public void clear() {
74         baStream = new FastByteArrayOutputStream();
75     }
76 }
77
78
Popular Tags