01: /* ServletOutputStreamWrapper.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Mon Jan 17 14:08:22 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.web.servlet;
20:
21: import java.io.Writer;
22: import java.io.OutputStream;
23: import java.io.ByteArrayOutputStream;
24: import java.io.IOException;
25:
26: import javax.servlet.ServletOutputStream;
27:
28: import org.zkoss.io.WriterOutputStream;
29:
30: /**
31: * A facade of OutputStream for implementing ServletOutputStream.
32: *
33: * @author tomyeh
34: */
35: public class ServletOutputStreamWrapper extends ServletOutputStream {
36: private final OutputStream _stream;
37:
38: /** Returns a facade of the specified stream. */
39: public static ServletOutputStream getInstance(OutputStream stream) {
40: if (stream instanceof ServletOutputStream)
41: return (ServletOutputStream) stream;
42: return new ServletOutputStreamWrapper(stream);
43: }
44:
45: /** Returns a facade of the specified writer.
46: *
47: * @param charset the charset. If null, "UTF-8" is assumed.
48: */
49: public static ServletOutputStream getInstance(Writer writer,
50: String charset) {
51: return new ServletOutputStreamWrapper(writer, charset);
52: }
53:
54: private ServletOutputStreamWrapper(OutputStream stream) {
55: if (stream == null)
56: throw new IllegalArgumentException("null");
57: _stream = stream;
58: }
59:
60: /**
61: * @param charset the charset. If null, "UTF-8" is assumed.
62: */
63: public ServletOutputStreamWrapper(Writer writer, String charset) {
64: if (writer == null)
65: throw new IllegalArgumentException("null");
66:
67: _stream = new WriterOutputStream(writer, charset);
68: }
69:
70: public void write(int b) throws IOException {
71: _stream.write(b);
72: }
73:
74: public void flush() throws IOException {
75: _stream.flush();
76: super .flush();
77: }
78:
79: public void close() throws IOException {
80: _stream.close();
81: super.close();
82: }
83: }
|