KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > cachius > support > MultiplexServletOutputStream


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.cachius.support;
25
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 import javax.servlet.ServletOutputStream JavaDoc;
30
31 /**
32  * ServletOutputStream that sends the data written to it to two
33  * different streams.
34  *
35  * @author Felix Gnass
36  */

37 public class MultiplexServletOutputStream extends ServletOutputStream JavaDoc {
38     
39     private OutputStream JavaDoc out1 = null;
40     
41     private OutputStream JavaDoc out2 = null;
42
43     private boolean clientAbort = false;
44     
45     /**
46      * Construct a new MultiplexServletOutputStream
47      *
48      * @param out1 The first output stream
49      * @param out2 The second output stream
50      */

51     public MultiplexServletOutputStream(OutputStream JavaDoc out1,
52             ServletOutputStream JavaDoc out2) {
53                 
54         this.out1 = out1;
55         this.out2 = out2;
56     }
57
58     /**
59      * Write to the output streams
60      *
61      * @param value The value to write
62      * @throws IOException
63      */

64     public void write(int value) throws IOException JavaDoc {
65         out1.write(value);
66         try {
67             if (!clientAbort) {
68                 out2.write(value);
69             }
70         }
71         catch (IOException JavaDoc e) {
72             clientAbort = true;
73         }
74     }
75
76     /**
77      * Write to the output streams
78      *
79      * @param value The value to write
80      * @throws IOException
81      */

82     public void write(byte[] value) throws IOException JavaDoc {
83         out1.write(value);
84         try {
85             if (!clientAbort) {
86                 out2.write(value);
87             }
88         }
89         catch (IOException JavaDoc e) {
90             clientAbort = true;
91         }
92     }
93
94     /**
95      * Write to the output streams
96      *
97      * @param b The data to write
98      * @param off The offset before starting to write
99      * @param len The lenght to write
100      * @throws IOException
101      */

102     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
103         out1.write(b, off, len);
104         try {
105             if (!clientAbort) {
106                 out2.write(b, off, len);
107             }
108         }
109         catch (IOException JavaDoc e) {
110             clientAbort = true;
111         }
112     }
113     
114     /**
115      * Flush the streams
116      */

117     public void flush() throws IOException JavaDoc {
118         out1.flush();
119         try {
120             if (!clientAbort) {
121                 out2.flush();
122             }
123         }
124         catch (IOException JavaDoc e) {
125             clientAbort = true;
126         }
127     }
128     
129     /**
130      * Close the streams
131      */

132     public void close() throws IOException JavaDoc {
133         out1.close();
134         out2.close();
135     }
136     
137 }
138
Popular Tags