001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.util;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021:
022: import javax.servlet.ServletOutputStream;
023:
024: /**
025: * This is a specialized class implementing a ServletOutputStream that works in
026: * conjunction with a PrintWriter to send data to the browser. It is used when a
027: * J2EE server throws an IllegalStateException when you call getOutputStream on
028: * a response which someone has previously called getWriter on.
029: */
030: public class PrintWriterServletOutputStream extends ServletOutputStream {
031:
032: /**
033: * The PrintWriter that is wrapped on top of the base input stream
034: */
035: PrintWriter mPrintWriter;
036:
037: /**
038: * Construct a ServletOutputStream that coordinates output using a base
039: * ServletOutputStream and a PrintWriter that is wrapped on top of that
040: * OutputStream.
041: */
042: public PrintWriterServletOutputStream(PrintWriter pO) {
043: super ();
044: mPrintWriter = pO;
045: }
046:
047: /**
048: * Writes an array of bytes
049: * @param pBuf the array to be written
050: * @throws IOException if an I/O error occurred
051: */
052: public void write(byte[] pBuf) throws IOException {
053: char[] cbuf = new char[pBuf.length];
054: for (int i = 0; i < cbuf.length; i++) {
055: cbuf[i] = (char) (pBuf[i] & 0xff);
056: }
057: mPrintWriter.write(cbuf, 0, pBuf.length);
058: }
059:
060: /**
061: * Writes a single byte to the output stream
062: */
063: public void write(int pVal) throws IOException {
064: mPrintWriter.write(pVal);
065: }
066:
067: /**
068: * Writes a subarray of bytes
069: * @param pBuf the array to be written
070: * @param pOffset the offset into the array
071: * @param pLength the number of bytes to write
072: * @throws IOException if an I/O error occurred
073: */
074: public void write(byte[] pBuf, int pOffset, int pLength)
075: throws IOException {
076: char[] cbuf = new char[pLength];
077: for (int i = 0; i < pLength; i++) {
078: cbuf[i] = (char) (pBuf[i + pOffset] & 0xff);
079: }
080: mPrintWriter.write(cbuf, 0, pLength);
081: }
082:
083: /**
084: * Flushes the stream, writing any buffered output bytes
085: * @throws IOException if an I/O error occurred
086: */
087: public void flush() throws IOException {
088: mPrintWriter.flush();
089: }
090:
091: /**
092: * Closes the stream
093: * @throws IOException if an I/O error occurred
094: */
095: public void close() throws IOException {
096: mPrintWriter.close();
097: }
098:
099: /**
100: * Prints a string.
101: * @param pVal the String to be printed
102: * @throws IOException if an I/O error has occurred
103: */
104: public void print(String pVal) throws IOException {
105: mPrintWriter.print(pVal);
106: }
107:
108: /**
109: * Prints an string followed by a CRLF.
110: * @param pVal the String to be printed
111: * @throws IOException if an I/O error has occurred
112: */
113: public void println(String pVal) throws IOException {
114: mPrintWriter.println(pVal);
115: }
116:
117: /**
118: * Prints a CRLF
119: * @throws IOException if an I/O error has occurred
120: */
121: public void println() throws IOException {
122: mPrintWriter.println();
123: }
124:
125: }
|