Quadcap Embeddable Server

com/quadcap/http/server22/HttpOutputStream.java

Go to the documentation of this file.
00001 package com.quadcap.http.server22; 00002 00003 /* Copyright 1998 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.ByteArrayOutputStream; 00042 import java.io.BufferedOutputStream; 00043 import java.io.IOException; 00044 import java.io.OutputStream; 00045 00046 import javax.servlet.ServletOutputStream; 00047 00048 import com.quadcap.net.server.WorkerOutputStream; 00049 00050 import com.quadcap.util.Debug; 00051 00052 /** 00053 * An output stream for writing servlet responses. This is an 00054 * abstract class, to be implemented by a network services 00055 * implementor. Servlet writers use the output stream to return data 00056 * to clients. They access it via the ServletResponse's 00057 * getOutputStream method, available from within the servlet's service 00058 * method. Subclasses of ServletOutputStream must provide an 00059 * implementation of the write(int) method. 00060 * <p> 00061 * 00062 * This implementation is friendly to servlets which return "small" 00063 * responses and don't compute Content-Length -- it will compute this 00064 * header for them automatically. 00065 * 00066 * @author Stan Bailes 00067 */ 00068 public class HttpOutputStream extends ServletOutputStream { 00069 WorkerOutputStream os; 00070 HttpResponse res; 00071 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 00072 boolean committed = false; 00073 boolean autoFlush = true; 00074 00075 int bufferSize = 32 * 1024; 00076 00077 /** 00078 * Construct a new http output stream, attached to the specified 00079 * worker output stream. 00080 * 00081 * @param os the output stream. 00082 */ 00083 public void reset(WorkerOutputStream os) { 00084 this.os = os; 00085 this.bos.reset(); 00086 this.committed = false; 00087 } 00088 00089 /** 00090 * Write a byte to the stream 00091 * 00092 * @param b the byte 00093 */ 00094 public void write(int b) throws IOException { 00095 if (committed) { 00096 os.write(b); 00097 } else if (bos.size() >= bufferSize) { 00098 res.writeHeaders(); 00099 bos.writeTo(os); 00100 committed = true; 00101 os.write(b); 00102 } else { 00103 bos.write(b); 00104 } 00105 } 00106 00107 /** 00108 * Write a bunch of bytes in one shot. 00109 * 00110 * @param buf a buffer where those bytes live 00111 * @param off the position in the buffer of the first byte to write 00112 * @param len the number of bytes to write 00113 * 00114 * @exception IOException may be thrown 00115 */ 00116 public void write(byte[] buf, int off, int len) throws IOException { 00117 if (committed) { 00118 os.write(buf, off, len); 00119 } else if (bos.size() + len >= bufferSize) { 00120 res.writeHeaders(); 00121 bos.writeTo(os); 00122 committed = true; 00123 os.write(buf, off, len); 00124 } else { 00125 bos.write(buf, off, len); 00126 } 00127 } 00128 00129 /** 00130 * Flush any buffered output to tht client 00131 */ 00132 public final void flushBuffered() throws IOException { 00133 if (!committed) { 00134 int len = bos.size(); 00135 if (len > 0) { 00136 res.maybeSetContentLength(len); 00137 } 00138 res.writeHeaders(); 00139 bos.writeTo(os); 00140 committed = true; 00141 } 00142 os.flush(); 00143 } 00144 00145 /** 00146 * Flush the underlying stream 00147 */ 00148 public final void flush() throws IOException { 00149 flushBuffered(); 00150 } 00151 00152 /** 00153 * Close does nothing, to prevent clients from closing streams when they 00154 * really don't want to 00155 */ 00156 public final void close() throws IOException { 00157 flushBuffered(); 00158 } 00159 00160 public final void reallyClose() throws IOException { 00161 os.close(); 00162 } 00163 00164 /** 00165 * Discard any bytes written so far if possible, and return true if 00166 * we were able to do so. 00167 */ 00168 public boolean discard() { 00169 if (!committed) { 00170 bos.reset(); 00171 return true; 00172 } 00173 return false; 00174 } 00175 00176 /** 00177 * Set up the connection with the http response object so we can 00178 * control flushing the headers when we've reached our buffered 00179 * limit. 00180 * 00181 * @param res the http response object associated with this request 00182 */ 00183 void setResponse(HttpResponse res) { 00184 this.res = res; 00185 } 00186 00187 /** 00188 * Return the underlying output stream 00189 */ 00190 WorkerOutputStream getOutputStream() { return os; } 00191 00192 final void setBufferSize(int size) throws IllegalStateException { 00193 if (committed) { 00194 throw new IllegalStateException("Response already committed"); 00195 } 00196 this.bufferSize = size; 00197 } 00198 00199 final int getBufferSize() { 00200 return bufferSize; 00201 } 00202 00203 final boolean isCommitted() { 00204 return committed; 00205 } 00206 00207 final void reset() throws IllegalStateException { 00208 if (committed) { 00209 throw new IllegalStateException("Response already committed"); 00210 } 00211 bos.reset(); 00212 } 00213 00214 final boolean getAutoFlush() { 00215 return autoFlush; 00216 } 00217 00218 final void setAutoFlush(boolean autoFlush) { 00219 this.autoFlush = autoFlush; 00220 } 00221 00222 public int getRemaining() { 00223 return bufferSize - bos.size(); 00224 } 00225 }