1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/* 2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java $ 3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 652091 $ 4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-04-29 13:41:07 -0700 (Tue, 29 Apr 2008) $ 5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ==================================================================== 7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one 8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or more contributor license agreements. See the NOTICE file 9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed with this work for additional information 10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * regarding copyright ownership. The ASF licenses this file 11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * to you under the Apache License, Version 2.0 (the 12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "License"); you may not use this file except in compliance 13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * with the License. You may obtain a copy of the License at 14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing, 18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * software distributed under the License is distributed on an 19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * KIND, either express or implied. See the License for the 21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * specific language governing permissions and limitations 22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * under the License. 23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ==================================================================== 24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many 26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation. For more 27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see 28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>. 29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl.io; 33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.IOException; 35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.InputStream; 36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.io.SessionInputBuffer; 38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/** 40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Stream that cuts off after a specified number of bytes. 41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Note that this class NEVER closes the underlying stream, even when close 42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * gets called. Instead, it will read until the "end" of its chunking on 43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * close, which allows for the seamless execution of subsequent HTTP 1.1 44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * requests, while not requiring the client to remember to read the entire 45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * contents of the response. 46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>Implementation note: Choices abound. One approach would pass 48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * through the {@link InputStream#mark} and {@link InputStream#reset} calls to 49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the underlying stream. That's tricky, though, because you then have to 50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * start duplicating the work of keeping track of how much a reset rewinds. 51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Further, you have to watch out for the "readLimit", and since the semantics 52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * for the readLimit leave room for differing implementations, you might get 53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * into a lot of trouble.</p> 54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>Alternatively, you could make this class extend 56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * {@link java.io.BufferedInputStream} 57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * and then use the protected members of that class to avoid duplicated effort. 58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * That solution has the side effect of adding yet another possible layer of 59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * buffering.</p> 60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>Then, there is the simple choice, which this takes - simply don't 62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * support {@link InputStream#mark} and {@link InputStream#reset}. That choice 63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * has the added benefit of keeping this class very simple.</p> 64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Ortwin Glueck 66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Eric Johnson 67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0 70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class ContentLengthInputStream extends InputStream { 72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BUFFER_SIZE = 2048; 74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The maximum number of bytes that can be read from the stream. Subsequent 76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * read operations will return -1. 77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private long contentLength; 79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** The current position */ 81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private long pos = 0; 82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** True if the stream is closed. */ 84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private boolean closed = false; 85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Wrapped input stream that all calls are delegated to. 88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private SessionInputBuffer in = null; 90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Creates a new length limited stream 93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param in The session input buffer to wrap 95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param contentLength The maximum number of bytes that can be read from 96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the stream. Subsequent read operations will return -1. 97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public ContentLengthInputStream(final SessionInputBuffer in, long contentLength) { 99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project super(); 100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (in == null) { 101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalArgumentException("Input stream may not be null"); 102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (contentLength < 0) { 104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalArgumentException("Content length may not be negative"); 105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project this.in = in; 107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project this.contentLength = contentLength; 108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>Reads until the end of the known length of content.</p> 112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>Does not close the underlying socket input, but instead leaves it 114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * primed to parse the next response.</p> 115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws IOException If an IO problem occurs. 116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void close() throws IOException { 118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (!closed) { 119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project try { 120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project byte buffer[] = new byte[BUFFER_SIZE]; 121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project while (read(buffer) >= 0) { 122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } finally { 124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // close after above so that we don't throw an exception trying 125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // to read after closed! 126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project closed = true; 127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Read the next byte from the stream 134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return The next byte or -1 if the end of stream has been reached. 135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws IOException If an IO problem occurs 136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see java.io.InputStream#read() 137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public int read() throws IOException { 139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (closed) { 140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IOException("Attempted read from closed stream."); 141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (pos >= contentLength) { 144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return -1; 145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project pos++; 147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return this.in.read(); 148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Does standard {@link InputStream#read(byte[], int, int)} behavior, but 152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * also notifies the watcher when the contents have been consumed. 153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param b The byte array to fill. 155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param off Start filling at this position. 156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param len The number of bytes to attempt to read. 157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return The number of bytes read, or -1 if the end of content has been 158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * reached. 159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws java.io.IOException Should an error occur on the wrapped stream. 161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public int read (byte[] b, int off, int len) throws java.io.IOException { 163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (closed) { 164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IOException("Attempted read from closed stream."); 165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (pos >= contentLength) { 168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return -1; 169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (pos + len > contentLength) { 172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project len = (int) (contentLength - pos); 173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project int count = this.in.read(b, off, len); 175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project pos += count; 176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return count; 177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Read more bytes from the stream. 182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param b The byte array to put the new data in. 183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return The number of bytes read into the buffer. 184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws IOException If an IO problem occurs 185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see java.io.InputStream#read(byte[]) 186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public int read(byte[] b) throws IOException { 188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return read(b, 0, b.length); 189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Skips and discards a number of bytes from the input stream. 193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param n The number of bytes to skip. 194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return The actual number of bytes skipped. <= 0 if no bytes 195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * are skipped. 196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws IOException If an error occurs while skipping bytes. 197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see InputStream#skip(long) 198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public long skip(long n) throws IOException { 200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (n <= 0) { 201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return 0; 202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project byte[] buffer = new byte[BUFFER_SIZE]; 204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // make sure we don't skip more bytes than are 205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // still available 206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project long remaining = Math.min(n, this.contentLength - this.pos); 207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // skip and keep track of the bytes actually skipped 208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project long count = 0; 209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project while (remaining > 0) { 210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project int l = read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining)); 211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (l == -1) { 212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project break; 213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project count += l; 215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project remaining -= l; 216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project this.pos += count; 218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return count; 219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} 221