001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java,v 1.12 2004/10/04 22:05:44 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import java.io.IOException;
034: import java.io.InputStream;
035:
036: /**
037: * Cuts the wrapped InputStream off after a specified number of bytes.
038: *
039: * <p>Implementation note: Choices abound. One approach would pass
040: * through the {@link InputStream#mark} and {@link InputStream#reset} calls to
041: * the underlying stream. That's tricky, though, because you then have to
042: * start duplicating the work of keeping track of how much a reset rewinds.
043: * Further, you have to watch out for the "readLimit", and since the semantics
044: * for the readLimit leave room for differing implementations, you might get
045: * into a lot of trouble.</p>
046: *
047: * <p>Alternatively, you could make this class extend {@link java.io.BufferedInputStream}
048: * and then use the protected members of that class to avoid duplicated effort.
049: * That solution has the side effect of adding yet another possible layer of
050: * buffering.</p>
051: *
052: * <p>Then, there is the simple choice, which this takes - simply don't
053: * support {@link InputStream#mark} and {@link InputStream#reset}. That choice
054: * has the added benefit of keeping this class very simple.</p>
055: *
056: * @author Ortwin Glueck
057: * @author Eric Johnson
058: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
059: * @since 2.0
060: */
061: public class ContentLengthInputStream extends InputStream {
062:
063: /**
064: * The maximum number of bytes that can be read from the stream. Subsequent
065: * read operations will return -1.
066: */
067: private long contentLength;
068:
069: /** The current position */
070: private long pos = 0;
071:
072: /** True if the stream is closed. */
073: private boolean closed = false;
074:
075: /**
076: * Wrapped input stream that all calls are delegated to.
077: */
078: private InputStream wrappedStream = null;
079:
080: /**
081: * @deprecated use {@link #ContentLengthInputStream(InputStream, long)}
082: *
083: * Creates a new length limited stream
084: *
085: * @param in The stream to wrap
086: * @param contentLength The maximum number of bytes that can be read from
087: * the stream. Subsequent read operations will return -1.
088: */
089: public ContentLengthInputStream(InputStream in, int contentLength) {
090: this (in, (long) contentLength);
091: }
092:
093: /**
094: * Creates a new length limited stream
095: *
096: * @param in The stream to wrap
097: * @param contentLength The maximum number of bytes that can be read from
098: * the stream. Subsequent read operations will return -1.
099: *
100: * @since 3.0
101: */
102: public ContentLengthInputStream(InputStream in, long contentLength) {
103: super ();
104: this .wrappedStream = in;
105: this .contentLength = contentLength;
106: }
107:
108: /**
109: * <p>Reads until the end of the known length of content.</p>
110: *
111: * <p>Does not close the underlying socket input, but instead leaves it
112: * primed to parse the next response.</p>
113: * @throws IOException If an IO problem occurs.
114: */
115: public void close() throws IOException {
116: if (!closed) {
117: try {
118: ChunkedInputStream.exhaustInputStream(this );
119: } finally {
120: // close after above so that we don't throw an exception trying
121: // to read after closed!
122: closed = true;
123: }
124: }
125: }
126:
127: /**
128: * Read the next byte from the stream
129: * @return The next byte or -1 if the end of stream has been reached.
130: * @throws IOException If an IO problem occurs
131: * @see java.io.InputStream#read()
132: */
133: public int read() throws IOException {
134: if (closed) {
135: throw new IOException("Attempted read from closed stream.");
136: }
137:
138: if (pos >= contentLength) {
139: return -1;
140: }
141: pos++;
142: return this .wrappedStream.read();
143: }
144:
145: /**
146: * Does standard {@link InputStream#read(byte[], int, int)} behavior, but
147: * also notifies the watcher when the contents have been consumed.
148: *
149: * @param b The byte array to fill.
150: * @param off Start filling at this position.
151: * @param len The number of bytes to attempt to read.
152: * @return The number of bytes read, or -1 if the end of content has been
153: * reached.
154: *
155: * @throws java.io.IOException Should an error occur on the wrapped stream.
156: */
157: public int read(byte[] b, int off, int len)
158: throws java.io.IOException {
159: if (closed) {
160: throw new IOException("Attempted read from closed stream.");
161: }
162:
163: if (pos >= contentLength) {
164: return -1;
165: }
166:
167: if (pos + len > contentLength) {
168: len = (int) (contentLength - pos);
169: }
170: int count = this .wrappedStream.read(b, off, len);
171: pos += count;
172: return count;
173: }
174:
175: /**
176: * Read more bytes from the stream.
177: * @param b The byte array to put the new data in.
178: * @return The number of bytes read into the buffer.
179: * @throws IOException If an IO problem occurs
180: * @see java.io.InputStream#read(byte[])
181: */
182: public int read(byte[] b) throws IOException {
183: return read(b, 0, b.length);
184: }
185:
186: /**
187: * Skips and discards a number of bytes from the input stream.
188: * @param n The number of bytes to skip.
189: * @return The actual number of bytes skipped. <= 0 if no bytes
190: * are skipped.
191: * @throws IOException If an error occurs while skipping bytes.
192: * @see InputStream#skip(long)
193: */
194: public long skip(long n) throws IOException {
195: // make sure we don't skip more bytes than are
196: // still available
197: long length = Math.min(n, contentLength - pos);
198: // skip and keep track of the bytes actually skipped
199: length = this .wrappedStream.skip(length);
200: // only add the skipped bytes to the current position
201: // if bytes were actually skipped
202: if (length > 0) {
203: pos += length;
204: }
205: return length;
206: }
207:
208: public int available() throws IOException {
209: if (this .closed) {
210: return 0;
211: }
212: int avail = this .wrappedStream.available();
213: if (this .pos + avail > this .contentLength) {
214: avail = (int) (this.contentLength - this.pos);
215: }
216: return avail;
217: }
218:
219: }
|