KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > server > ResponseWriter


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 JavaDoc;
35 import java.io.FilterWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.io.OutputStreamWriter JavaDoc;
39 import java.io.UnsupportedEncodingException JavaDoc;
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 JavaDoc {
47     public static final String JavaDoc CRLF = "\r\n";
48     public static final String JavaDoc ISO_8859_1 = "ISO-8859-1";
49     private OutputStream JavaDoc outStream;
50     private String JavaDoc encoding;
51
52     public ResponseWriter(OutputStream JavaDoc outStream) throws UnsupportedEncodingException JavaDoc {
53         this(outStream, CRLF);
54     }
55     public ResponseWriter(OutputStream JavaDoc outStream, String JavaDoc lineSeparator) throws UnsupportedEncodingException JavaDoc {
56         this(outStream, lineSeparator, ISO_8859_1);
57     }
58     public ResponseWriter(OutputStream JavaDoc outStream, String JavaDoc lineSeparator, String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc {
59         super(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(outStream, encoding)));
60         this.outStream = outStream;
61         this.encoding = encoding;
62     }
63     
64     public String JavaDoc getEncoding() {
65         return encoding;
66     }
67     
68     public void close() throws IOException JavaDoc {
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 JavaDoc {
79         if(out != null) {
80             super.flush();
81             outStream.flush();
82         }
83     }
84
85     public void write(byte b) throws IOException JavaDoc {
86         super.flush();
87         outStream.write((int)b);
88     }
89     public void write(byte[] b) throws IOException JavaDoc {
90         super.flush();
91         outStream.write(b);
92     }
93     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
94         super.flush();
95         outStream.write(b,off,len);
96     }
97
98     public void print(String JavaDoc s) throws IOException JavaDoc {
99         if (s == null) {
100             s = "null";
101         }
102         write(s);
103     }
104     
105     public void print(int i) throws IOException JavaDoc {
106         write(Integer.toString(i));
107     }
108     public void println(int i) throws IOException JavaDoc {
109         write(Integer.toString(i));
110         write(CRLF);
111     }
112
113     public void println(String JavaDoc s) throws IOException JavaDoc {
114         print(s);
115         write(CRLF);
116     }
117     
118     public void println() throws IOException JavaDoc {
119         write(CRLF);
120     }
121     
122     
123 }
124
Popular Tags