001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/ResponseWriter.java,v 1.5 2004/11/07 12:31:42 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.server;
032:
033: import java.io.BufferedWriter;
034: import java.io.FilterWriter;
035: import java.io.IOException;
036: import java.io.OutputStream;
037: import java.io.OutputStreamWriter;
038: import java.io.UnsupportedEncodingException;
039:
040: /**
041: * Provides a hybrid Writer/OutputStream for sending HTTP response data
042: *
043: * @author Christian Kohlschuetter
044: */
045: public class ResponseWriter extends FilterWriter {
046: public static final String CRLF = "\r\n";
047: public static final String ISO_8859_1 = "ISO-8859-1";
048: private OutputStream outStream = null;
049: private String encoding = null;
050:
051: public ResponseWriter(final OutputStream outStream)
052: throws UnsupportedEncodingException {
053: this (outStream, CRLF, ISO_8859_1);
054: }
055:
056: public ResponseWriter(final OutputStream outStream,
057: final String encoding) throws UnsupportedEncodingException {
058: this (outStream, CRLF, encoding);
059: }
060:
061: public ResponseWriter(final OutputStream outStream,
062: final String lineSeparator, final String encoding)
063: throws UnsupportedEncodingException {
064: super (new BufferedWriter(new OutputStreamWriter(outStream,
065: encoding)));
066: this .outStream = outStream;
067: this .encoding = encoding;
068: }
069:
070: public String getEncoding() {
071: return encoding;
072: }
073:
074: public void close() throws IOException {
075: if (outStream != null) {
076: super .close();
077: outStream = null;
078: }
079: }
080:
081: /* (non-Javadoc)
082: * @see java.io.Writer#flush()
083: */
084: public void flush() throws IOException {
085: if (outStream != null) {
086: super .flush();
087: outStream.flush();
088: }
089: }
090:
091: public void write(byte b) throws IOException {
092: super .flush();
093: outStream.write((int) b);
094: }
095:
096: public void write(byte[] b) throws IOException {
097: super .flush();
098: outStream.write(b);
099: }
100:
101: public void write(byte[] b, int off, int len) throws IOException {
102: super .flush();
103: outStream.write(b, off, len);
104: }
105:
106: public void print(String s) throws IOException {
107: if (s == null) {
108: s = "null";
109: }
110: write(s);
111: }
112:
113: public void print(int i) throws IOException {
114: write(Integer.toString(i));
115: }
116:
117: public void println(int i) throws IOException {
118: write(Integer.toString(i));
119: write(CRLF);
120: }
121:
122: public void println(String s) throws IOException {
123: print(s);
124: write(CRLF);
125: }
126:
127: public void println() throws IOException {
128: write(CRLF);
129: }
130:
131: }
|