1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ResponseWriter.java,v 1.1.2.3 2004/02/22 18:21:18 olegk Exp $
3    * $Revision: 1.1.2.3 $
4    * $Date: 2004/02/22 18:21:18 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
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      /* (non-Javadoc)
76       * @see java.io.Writer#flush()
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 }