1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/ResponseWriter.java,v 1.5 2004/11/07 12:31:42 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
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   */
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      /* (non-Javadoc)
81       * @see java.io.Writer#flush()
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 }