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