ResponseWriter xref

    1   /*
    2    * $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 $
    3    * $Revision: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    *
    6    * ====================================================================
    7    *
    8    *  Licensed to the Apache Software Foundation (ASF) under one or more
    9    *  contributor license agreements.  See the NOTICE file distributed with
    10   *  this work for additional information regarding copyright ownership.
    11   *  The ASF licenses this file to You under the Apache License, Version 2.0
    12   *  (the "License"); you may not use this file except in compliance with
    13   *  the License.  You may obtain a copy of the License at
    14   *
    15   *      http://www.apache.org/licenses/LICENSE-2.0
    16   *
    17   *  Unless required by applicable law or agreed to in writing, software
    18   *  distributed under the License is distributed on an "AS IS" BASIS,
    19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    20   *  See the License for the specific language governing permissions and
    21   *  limitations under the License.
    22   * ====================================================================
    23   *
    24   * This software consists of voluntary contributions made by many
    25   * individuals on behalf of the Apache Software Foundation.  For more
    26   * information on the Apache Software Foundation, please see
    27   * <http://www.apache.org/>.
    28   *
    29   */
    30
    31  package org.apache.commons.httpclient.server;
    32
    33  import java.io.BufferedWriter;
    34  import java.io.FilterWriter;
    35  import java.io.IOException;
    36  import java.io.OutputStream;
    37  import java.io.OutputStreamWriter;
    38  import java.io.UnsupportedEncodingException;
    39
    40  /**
    41   * Provides a hybrid Writer/OutputStream for sending HTTP response data
    42   *
    43   * @author Christian Kohlschuetter
    44   */
    45  public class ResponseWriter extends FilterWriter {
    46      public static final String CRLF = "\r\n";
    47      public static final String ISO_8859_1 = "ISO-8859-1";
    48      private OutputStream outStream = null;
    49      private String encoding = null;
    50
    51      public ResponseWriter(final OutputStream outStream)
    52      throws UnsupportedEncodingException {
    53          this(outStream, CRLF, ISO_8859_1);
    54      }
    55
    56      public ResponseWriter(final OutputStream outStream, final String encoding)
    57      throws UnsupportedEncodingException {
    58          this(outStream, CRLF, encoding);
    59      }
    60
    61      public ResponseWriter(
    62              final OutputStream outStream,
    63              final String lineSeparator,
    64              final String encoding) throws UnsupportedEncodingException {
    65          super(new BufferedWriter(new OutputStreamWriter(outStream, encoding)));
    66          this.outStream = outStream;
    67          this.encoding = encoding;
    68      }
    69
    70      public String getEncoding() {
    71          return encoding;
    72      }
    73
    74      public void close() throws IOException {
    75          if(outStream != null) {
    76              super.close();
    77              outStream = null;
    78          }
    79      }
    80
    81      /* (non-Javadoc)
    82       * @see java.io.Writer#flush()
    83       */
    84      public void flush() throws IOException {
    85          if(outStream != null) {
    86              super.flush();
    87              outStream.flush();
    88          }
    89      }
    90
    91      public void write(byte b) throws IOException {
    92          super.flush();
    93          outStream.write((int)b);
    94      }
    95
    96      public void write(byte[] b) throws IOException {
    97          super.flush();
    98          outStream.write(b);
    99      }
    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 }