1 /*
2 * Copyright (c) 2002-2003 by OpenSymphony
3 * All rights reserved.
4 */
5 package com.opensymphony.oscache.web.filter;
6
7 import java.io.IOException;
8 import java.io.OutputStream;
9
10 import javax.servlet.ServletOutputStream;
11
12 /**
13 * Extends the base <code>ServletOutputStream</code> class so that
14 * the stream can be captured as it gets written. This is achieved
15 * by overriding the <code>write()</code> methods and outputting
16 * the data to two streams - the original stream and a secondary stream
17 * that is designed to capture the written data.
18 *
19 * @version $Revision: 393 $
20 * @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
21 */
22 public class SplitServletOutputStream extends ServletOutputStream {
23 OutputStream captureStream = null;
24 OutputStream passThroughStream = null;
25
26 /**
27 * Constructs a split output stream that both captures and passes through
28 * the servlet response.
29 *
30 * @param captureStream The stream that will be used to capture the data.
31 * @param passThroughStream The pass-through <code>ServletOutputStream</code>
32 * that will write the response to the client as originally intended.
33 */
34 public SplitServletOutputStream(OutputStream captureStream, OutputStream passThroughStream) {
35 this.captureStream = captureStream;
36 this.passThroughStream = passThroughStream;
37 }
38
39 /**
40 * Writes the incoming data to both the output streams.
41 *
42 * @param value The int data to write.
43 * @throws IOException
44 */
45 public void write(int value) throws IOException {
46 captureStream.write(value);
47 passThroughStream.write(value);
48 }
49
50 /**
51 * Writes the incoming data to both the output streams.
52 *
53 * @param value The bytes to write to the streams.
54 * @throws IOException
55 */
56 public void write(byte[] value) throws IOException {
57 captureStream.write(value);
58 passThroughStream.write(value);
59 }
60
61 /**
62 * Writes the incoming data to both the output streams.
63 *
64 * @param b The bytes to write out to the streams.
65 * @param off The offset into the byte data where writing should begin.
66 * @param len The number of bytes to write.
67 * @throws IOException
68 */
69 public void write(byte[] b, int off, int len) throws IOException {
70 captureStream.write(b, off, len);
71 passThroughStream.write(b, off, len);
72 }
73
74 /**
75 * Flushes both the output streams.
76 * @throws IOException
77 */
78 public void flush() throws IOException {
79 super.flush();
80 captureStream.flush(); //why not?
81 passThroughStream.flush();
82 }
83
84 /**
85 * Closes both the output streams.
86 * @throws IOException
87 */
88 public void close() throws IOException {
89 super.close();
90 captureStream.close();
91 passThroughStream.close();
92 }
93
94 }