001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/RequestStream.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.InputStream;
067: import java.io.IOException;
068: import javax.servlet.ServletInputStream;
069: import org.apache.catalina.Request;
070: import org.apache.catalina.util.StringManager;
071:
072: /**
073: * Convenience implementation of <b>ServletInputStream</b> that works with
074: * the standard implementations of <b>Request</b>. If the content length has
075: * been set on our associated Request, this implementation will enforce
076: * not reading 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 RequestStream extends ServletInputStream {
084:
085: // ----------------------------------------------------------- Constructors
086:
087: /**
088: * Construct a servlet input stream associated with the specified Request.
089: *
090: * @param request The associated request
091: */
092: public RequestStream(Request request) {
093:
094: super ();
095: closed = false;
096: count = 0;
097: length = request.getRequest().getContentLength();
098: stream = request.getStream();
099:
100: }
101:
102: // ----------------------------------------------------- Instance Variables
103:
104: /**
105: * Has this stream been closed?
106: */
107: protected boolean closed = false;
108:
109: /**
110: * The number of bytes which have already been returned by this stream.
111: */
112: protected int count = 0;
113:
114: /**
115: * The content length past which we will not read, or -1 if there is
116: * no defined content length.
117: */
118: protected int length = -1;
119:
120: /**
121: * The localized strings for this package.
122: */
123: protected static StringManager sm = StringManager
124: .getManager(Constants.Package);
125:
126: /**
127: * The underlying input stream from which we should read data.
128: */
129: protected InputStream stream = null;
130:
131: // --------------------------------------------------------- Public Methods
132:
133: /**
134: * Close this input stream. No physical level I-O is performed, but
135: * any further attempt to read from this stream will throw an IOException.
136: * If a content length has been set but not all of the bytes have yet been
137: * consumed, the remaining bytes will be swallowed.
138: */
139: public void close() throws IOException {
140:
141: if (closed)
142: throw new IOException(sm
143: .getString("requestStream.close.closed"));
144:
145: if (length > 0) {
146: while (count < length) {
147: int b = read();
148: if (b < 0)
149: break;
150: }
151: }
152:
153: closed = true;
154:
155: }
156:
157: /**
158: * Read and return a single byte from this input stream, or -1 if end of
159: * file has been encountered.
160: *
161: * @exception IOException if an input/output error occurs
162: */
163: public int read() throws IOException {
164:
165: // Has this stream been closed?
166: if (closed)
167: throw new IOException(sm
168: .getString("requestStream.read.closed"));
169:
170: // Have we read the specified content length already?
171: if ((length >= 0) && (count >= length))
172: return (-1); // End of file indicator
173:
174: // Read and count the next byte, then return it
175: int b = stream.read();
176: if (b >= 0)
177: count++;
178: return (b);
179:
180: }
181:
182: /**
183: * Read some number of bytes from the input stream, and store them
184: * into the buffer array b. The number of bytes actually read is
185: * returned as an integer. This method blocks until input data is
186: * available, end of file is detected, or an exception is thrown.
187: *
188: * @param b The buffer into which the data is read
189: *
190: * @exception IOException if an input/output error occurs
191: */
192: public int read(byte b[]) throws IOException {
193:
194: return (read(b, 0, b.length));
195:
196: }
197:
198: /**
199: * Read up to <code>len</code> bytes of data from the input stream
200: * into an array of bytes. An attempt is made to read as many as
201: * <code>len</code> bytes, but a smaller number may be read,
202: * possibly zero. The number of bytes actually read is returned as
203: * an integer. This method blocks until input data is available,
204: * end of file is detected, or an exception is thrown.
205: *
206: * @param b The buffer into which the data is read
207: * @param off The start offset into array <code>b</code> at which
208: * the data is written
209: * @param len The maximum number of bytes to read
210: *
211: * @exception IOException if an input/output error occurs
212: */
213: public int read(byte b[], int off, int len) throws IOException {
214:
215: int toRead = len;
216: if (length > 0) {
217: if (count >= length)
218: return (-1);
219: if ((count + len) > length)
220: toRead = length - count;
221: }
222: int actuallyRead = super.read(b, off, toRead);
223: return (actuallyRead);
224:
225: }
226:
227: }
|