View Javadoc

1   /*
2    * $Id: ResponseWriter.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http;
12  
13  import java.io.BufferedWriter;
14  import java.io.FilterWriter;
15  import java.io.IOException;
16  import java.io.OutputStream;
17  import java.io.OutputStreamWriter;
18  import java.io.UnsupportedEncodingException;
19  
20  /**
21   * Provides a hybrid Writer/OutputStream for sending HTTP response data
22   */
23  public class ResponseWriter extends FilterWriter
24  {
25      public static final String CRLF = "\r\n";
26      public static final String ISO_8859_1 = "ISO-8859-1";
27      private OutputStream outStream = null;
28      private String encoding = null;
29  
30      public ResponseWriter(final OutputStream outStream) throws UnsupportedEncodingException
31      {
32          this(outStream, CRLF, ISO_8859_1);
33      }
34  
35      public ResponseWriter(final OutputStream outStream, final String encoding)
36          throws UnsupportedEncodingException
37      {
38          this(outStream, CRLF, encoding);
39      }
40  
41      public ResponseWriter(final OutputStream outStream, final String lineSeparator, final String encoding)
42          throws UnsupportedEncodingException
43      {
44          super(new BufferedWriter(new OutputStreamWriter(outStream, encoding)));
45          this.outStream = outStream;
46          this.encoding = encoding;
47      }
48  
49      public String getEncoding()
50      {
51          return encoding;
52      }
53  
54      @Override
55      public void close() throws IOException
56      {
57          if (outStream != null)
58          {
59              super.close();
60              outStream = null;
61          }
62      }
63  
64      @Override
65      public void flush() throws IOException
66      {
67          if (outStream != null)
68          {
69              super.flush();
70              outStream.flush();
71          }
72      }
73  
74      public void write(byte b) throws IOException
75      {
76          super.flush();
77          outStream.write(b);
78      }
79  
80      public void write(byte[] b) throws IOException
81      {
82          super.flush();
83          outStream.write(b);
84      }
85  
86      public void write(byte[] b, int off, int len) throws IOException
87      {
88          super.flush();
89          outStream.write(b, off, len);
90      }
91  
92      public void print(String s) throws IOException
93      {
94          if (s == null)
95          {
96              s = "null";
97          }
98          write(s);
99      }
100 
101     public void print(int i) throws IOException
102     {
103         write(Integer.toString(i));
104     }
105 
106     public void println(int i) throws IOException
107     {
108         write(Integer.toString(i));
109         write(CRLF);
110     }
111 
112     public void println(String s) throws IOException
113     {
114         print(s);
115         write(CRLF);
116     }
117 
118     public void println() throws IOException
119     {
120         write(CRLF);
121     }
122 
123 }