KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > ResponseWriter


1 /*
2  * $Id: ResponseWriter.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.http;
12
13 import java.io.BufferedWriter JavaDoc;
14 import java.io.FilterWriter JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.io.OutputStreamWriter JavaDoc;
18 import java.io.UnsupportedEncodingException JavaDoc;
19
20 /**
21  * Provides a hybrid Writer/OutputStream for sending HTTP response data
22  */

23 public class ResponseWriter extends FilterWriter JavaDoc
24 {
25     public static final String JavaDoc CRLF = "\r\n";
26     public static final String JavaDoc ISO_8859_1 = "ISO-8859-1";
27     private OutputStream JavaDoc outStream = null;
28     private String JavaDoc encoding = null;
29
30     public ResponseWriter(final OutputStream JavaDoc outStream) throws UnsupportedEncodingException JavaDoc
31     {
32         this(outStream, CRLF, ISO_8859_1);
33     }
34
35     public ResponseWriter(final OutputStream JavaDoc outStream, final String JavaDoc encoding)
36         throws UnsupportedEncodingException JavaDoc
37     {
38         this(outStream, CRLF, encoding);
39     }
40
41     public ResponseWriter(final OutputStream JavaDoc outStream, final String JavaDoc lineSeparator, final String JavaDoc encoding)
42         throws UnsupportedEncodingException JavaDoc
43     {
44         super(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(outStream, encoding)));
45         this.outStream = outStream;
46         this.encoding = encoding;
47     }
48
49     public String JavaDoc getEncoding()
50     {
51         return encoding;
52     }
53
54     public void close() throws IOException JavaDoc
55     {
56         if (outStream != null)
57         {
58             super.close();
59             outStream = null;
60         }
61     }
62
63     /*
64      * (non-Javadoc)
65      *
66      * @see java.io.Writer#flush()
67      */

68     public void flush() throws IOException JavaDoc
69     {
70         if (outStream != null)
71         {
72             super.flush();
73             outStream.flush();
74         }
75     }
76
77     public void write(byte b) throws IOException JavaDoc
78     {
79         super.flush();
80         outStream.write(b);
81     }
82
83     public void write(byte[] b) throws IOException JavaDoc
84     {
85         super.flush();
86         outStream.write(b);
87     }
88
89     public void write(byte[] b, int off, int len) throws IOException JavaDoc
90     {
91         super.flush();
92         outStream.write(b, off, len);
93     }
94
95     public void print(String JavaDoc s) throws IOException JavaDoc
96     {
97         if (s == null)
98         {
99             s = "null";
100         }
101         write(s);
102     }
103
104     public void print(int i) throws IOException JavaDoc
105     {
106         write(Integer.toString(i));
107     }
108
109     public void println(int i) throws IOException JavaDoc
110     {
111         write(Integer.toString(i));
112         write(CRLF);
113     }
114
115     public void println(String JavaDoc s) throws IOException JavaDoc
116     {
117         print(s);
118         write(CRLF);
119     }
120
121     public void println() throws IOException JavaDoc
122     {
123         write(CRLF);
124     }
125
126 }
127
Popular Tags