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