KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > filters > ByteArrayOutputStreamWrapper


1 package org.roller.presentation.filters;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.OutputStream JavaDoc;
5
6 import javax.servlet.ServletOutputStream JavaDoc;
7
8 /*
9  * @author llavandowska
10  *
11  * Implementation of ServletOutputStream that allows the filter to hold the
12  * Response content for insertion into the cache.
13  */

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