1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient.server;
31
32 import java.io.BufferedWriter;
33 import java.io.FilterWriter;
34 import java.io.IOException;
35 import java.io.OutputStream;
36 import java.io.OutputStreamWriter;
37 import java.io.UnsupportedEncodingException;
38
39 /***
40 * Provides a hybrid Writer/OutputStream for sending HTTP response data
41 *
42 * @author Christian Kohlschuetter
43 */
44 public class ResponseWriter extends FilterWriter {
45 public static final String CRLF = "\r\n";
46 public static final String ISO_8859_1 = "ISO-8859-1";
47 private OutputStream outStream = null;
48 private String encoding = null;
49
50 public ResponseWriter(final OutputStream outStream)
51 throws UnsupportedEncodingException {
52 this(outStream, CRLF, ISO_8859_1);
53 }
54
55 public ResponseWriter(final OutputStream outStream, final String encoding)
56 throws UnsupportedEncodingException {
57 this(outStream, CRLF, encoding);
58 }
59
60 public ResponseWriter(
61 final OutputStream outStream,
62 final String lineSeparator,
63 final String encoding) throws UnsupportedEncodingException {
64 super(new BufferedWriter(new OutputStreamWriter(outStream, encoding)));
65 this.outStream = outStream;
66 this.encoding = encoding;
67 }
68
69 public String getEncoding() {
70 return encoding;
71 }
72
73 public void close() throws IOException {
74 if(outStream != null) {
75 super.close();
76 outStream = null;
77 }
78 }
79
80
81
82
83 public void flush() throws IOException {
84 if(outStream != null) {
85 super.flush();
86 outStream.flush();
87 }
88 }
89
90 public void write(byte b) throws IOException {
91 super.flush();
92 outStream.write((int)b);
93 }
94
95 public void write(byte[] b) throws IOException {
96 super.flush();
97 outStream.write(b);
98 }
99
100 public void write(byte[] b, int off, int len) throws IOException {
101 super.flush();
102 outStream.write(b,off,len);
103 }
104
105 public void print(String s) throws IOException {
106 if (s == null) {
107 s = "null";
108 }
109 write(s);
110 }
111
112 public void print(int i) throws IOException {
113 write(Integer.toString(i));
114 }
115
116 public void println(int i) throws IOException {
117 write(Integer.toString(i));
118 write(CRLF);
119 }
120
121 public void println(String s) throws IOException {
122 print(s);
123 write(CRLF);
124 }
125
126 public void println() throws IOException {
127 write(CRLF);
128 }
129
130 }