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