001: /*
002: * $Id: ResponseWriter.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.http;
012:
013: import java.io.BufferedWriter;
014: import java.io.FilterWriter;
015: import java.io.IOException;
016: import java.io.OutputStream;
017: import java.io.OutputStreamWriter;
018: import java.io.UnsupportedEncodingException;
019:
020: /**
021: * Provides a hybrid Writer/OutputStream for sending HTTP response data
022: */
023: public class ResponseWriter extends FilterWriter {
024: public static final String CRLF = "\r\n";
025: public static final String ISO_8859_1 = "ISO-8859-1";
026: private OutputStream outStream = null;
027: private String encoding = null;
028:
029: public ResponseWriter(final OutputStream outStream)
030: throws UnsupportedEncodingException {
031: this (outStream, CRLF, ISO_8859_1);
032: }
033:
034: public ResponseWriter(final OutputStream outStream,
035: final String encoding) throws UnsupportedEncodingException {
036: this (outStream, CRLF, encoding);
037: }
038:
039: public ResponseWriter(final OutputStream outStream,
040: final String lineSeparator, final String encoding)
041: throws UnsupportedEncodingException {
042: super (new BufferedWriter(new OutputStreamWriter(outStream,
043: encoding)));
044: this .outStream = outStream;
045: this .encoding = encoding;
046: }
047:
048: public String getEncoding() {
049: return encoding;
050: }
051:
052: public void close() throws IOException {
053: if (outStream != null) {
054: super .close();
055: outStream = null;
056: }
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see java.io.Writer#flush()
063: */
064: public void flush() throws IOException {
065: if (outStream != null) {
066: super .flush();
067: outStream.flush();
068: }
069: }
070:
071: public void write(byte b) throws IOException {
072: super .flush();
073: outStream.write(b);
074: }
075:
076: public void write(byte[] b) throws IOException {
077: super .flush();
078: outStream.write(b);
079: }
080:
081: public void write(byte[] b, int off, int len) throws IOException {
082: super .flush();
083: outStream.write(b, off, len);
084: }
085:
086: public void print(String s) throws IOException {
087: if (s == null) {
088: s = "null";
089: }
090: write(s);
091: }
092:
093: public void print(int i) throws IOException {
094: write(Integer.toString(i));
095: }
096:
097: public void println(int i) throws IOException {
098: write(Integer.toString(i));
099: write(CRLF);
100: }
101:
102: public void println(String s) throws IOException {
103: print(s);
104: write(CRLF);
105: }
106:
107: public void println() throws IOException {
108: write(CRLF);
109: }
110:
111: }
|