001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseStream.java,v 1.6 2002/03/18 07:15:39 remm Exp $
003: * $Revision: 1.6 $
004: * $Date: 2002/03/18 07:15:39 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.connector;
065:
066: import java.io.IOException;
067: import java.io.OutputStream;
068: import javax.servlet.ServletOutputStream;
069: import org.apache.catalina.Response;
070: import org.apache.catalina.util.StringManager;
071:
072: /**
073: * Convenience implementation of <b>ServletOutputStream</b> that works with
074: * the standard ResponseBase implementation of <b>Response</b>. If the content
075: * length has been set on our associated Response, this implementation will
076: * enforce not writing more than that many bytes on the underlying stream.
077: *
078: * @author Craig R. McClanahan
079: * @version $Revision: 1.6 $ $Date: 2002/03/18 07:15:39 $
080: * @deprecated
081: */
082:
083: public class ResponseStream extends ServletOutputStream {
084:
085: // ----------------------------------------------------------- Constructors
086:
087: /**
088: * Construct a servlet output stream associated with the specified Request.
089: *
090: * @param response The associated response
091: */
092: public ResponseStream(Response response) {
093:
094: super ();
095: closed = false;
096: commit = false;
097: count = 0;
098: this .response = response;
099: this .stream = response.getStream();
100: this .suspended = response.isSuspended();
101:
102: }
103:
104: // ----------------------------------------------------- Instance Variables
105:
106: /**
107: * Has this stream been closed?
108: */
109: protected boolean closed = false;
110:
111: /**
112: * Should we commit the response when we are flushed?
113: */
114: protected boolean commit = false;
115:
116: /**
117: * The number of bytes which have already been written to this stream.
118: */
119: protected int count = 0;
120:
121: /**
122: * The content length past which we will not write, or -1 if there is
123: * no defined content length.
124: */
125: protected int length = -1;
126:
127: /**
128: * The Response with which this input stream is associated.
129: */
130: protected Response response = null;
131:
132: /**
133: * The localized strings for this package.
134: */
135: protected static StringManager sm = StringManager
136: .getManager(Constants.Package);
137:
138: /**
139: * The underlying output stream to which we should write data.
140: */
141: protected OutputStream stream = null;
142:
143: /**
144: * Has this response output been suspended?
145: */
146: protected boolean suspended = false;
147:
148: // ------------------------------------------------------------- Properties
149:
150: /**
151: * [Package Private] Return the "commit response on flush" flag.
152: */
153: boolean getCommit() {
154:
155: return (this .commit);
156:
157: }
158:
159: /**
160: * [Package Private] Set the "commit response on flush" flag.
161: *
162: * @param commit The new commit flag
163: */
164: void setCommit(boolean commit) {
165:
166: this .commit = commit;
167:
168: }
169:
170: /**
171: * Set the suspended flag.
172: */
173: void setSuspended(boolean suspended) {
174:
175: this .suspended = suspended;
176:
177: }
178:
179: /**
180: * Suspended flag accessor.
181: */
182: boolean isSuspended() {
183:
184: return (this .suspended);
185:
186: }
187:
188: // --------------------------------------------------------- Public Methods
189:
190: /**
191: * Close this output stream, causing any buffered data to be flushed and
192: * any further output data to throw an IOException.
193: */
194: public void close() throws IOException {
195:
196: if (suspended)
197: throw new IOException(sm
198: .getString("responseStream.suspended"));
199:
200: if (closed)
201: throw new IOException(sm
202: .getString("responseStream.close.closed"));
203:
204: response.getResponse().flushBuffer();
205: closed = true;
206:
207: }
208:
209: /**
210: * Flush any buffered data for this output stream, which also causes the
211: * response to be committed.
212: */
213: public void flush() throws IOException {
214:
215: if (suspended)
216: throw new IOException(sm
217: .getString("responseStream.suspended"));
218:
219: if (closed)
220: throw new IOException(sm
221: .getString("responseStream.flush.closed"));
222:
223: if (commit)
224: response.getResponse().flushBuffer();
225:
226: }
227:
228: /**
229: * Write the specified byte to our output stream.
230: *
231: * @param b The byte to be written
232: *
233: * @exception IOException if an input/output error occurs
234: */
235: public void write(int b) throws IOException {
236:
237: if (suspended)
238: return;
239:
240: if (closed)
241: throw new IOException(sm
242: .getString("responseStream.write.closed"));
243:
244: if ((length > 0) && (count >= length))
245: throw new IOException(sm
246: .getString("responseStream.write.count"));
247:
248: ((ResponseBase) response).write(b);
249: count++;
250:
251: }
252:
253: /**
254: * Write <code>b.length</code> bytes from the specified byte array
255: * to our output stream.
256: *
257: * @param b The byte array to be written
258: *
259: * @exception IOException if an input/output error occurs
260: */
261: public void write(byte b[]) throws IOException {
262:
263: if (suspended)
264: return;
265:
266: write(b, 0, b.length);
267:
268: }
269:
270: /**
271: * Write <code>len</code> bytes from the specified byte array, starting
272: * at the specified offset, to our output stream.
273: *
274: * @param b The byte array containing the bytes to be written
275: * @param off Zero-relative starting offset of the bytes to be written
276: * @param len The number of bytes to be written
277: *
278: * @exception IOException if an input/output error occurs
279: */
280: public void write(byte b[], int off, int len) throws IOException {
281:
282: if (suspended)
283: return;
284:
285: if (closed)
286: throw new IOException(sm
287: .getString("responseStream.write.closed"));
288:
289: int actual = len;
290: if ((length > 0) && ((count + len) >= length))
291: actual = length - count;
292: ((ResponseBase) response).write(b, off, actual);
293: count += actual;
294: if (actual < len)
295: throw new IOException(sm
296: .getString("responseStream.write.count"));
297:
298: }
299:
300: // -------------------------------------------------------- Package Methods
301:
302: /**
303: * Has this response stream been closed?
304: */
305: boolean closed() {
306:
307: return (this .closed);
308:
309: }
310:
311: /**
312: * Reset the count of bytes written to this stream to zero.
313: */
314: void reset() {
315:
316: count = 0;
317:
318: }
319:
320: }
|