1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/ContentLengthInputStream.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,228 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.io; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.io.InputStream; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.ConnectionClosedException; 1.37 +import ch.boye.httpclientandroidlib.io.BufferInfo; 1.38 +import ch.boye.httpclientandroidlib.io.SessionInputBuffer; 1.39 + 1.40 +/** 1.41 + * Input stream that cuts off after a defined number of bytes. This class 1.42 + * is used to receive content of HTTP messages where the end of the content 1.43 + * entity is determined by the value of the <code>Content-Length header</code>. 1.44 + * Entities transferred using this stream can be maximum {@link Long#MAX_VALUE} 1.45 + * long. 1.46 + * <p> 1.47 + * Note that this class NEVER closes the underlying stream, even when close 1.48 + * gets called. Instead, it will read until the "end" of its limit on 1.49 + * close, which allows for the seamless execution of subsequent HTTP 1.1 1.50 + * requests, while not requiring the client to remember to read the entire 1.51 + * contents of the response. 1.52 + * 1.53 + * 1.54 + * @since 4.0 1.55 + */ 1.56 +public class ContentLengthInputStream extends InputStream { 1.57 + 1.58 + private static final int BUFFER_SIZE = 2048; 1.59 + /** 1.60 + * The maximum number of bytes that can be read from the stream. Subsequent 1.61 + * read operations will return -1. 1.62 + */ 1.63 + private long contentLength; 1.64 + 1.65 + /** The current position */ 1.66 + private long pos = 0; 1.67 + 1.68 + /** True if the stream is closed. */ 1.69 + private boolean closed = false; 1.70 + 1.71 + /** 1.72 + * Wrapped input stream that all calls are delegated to. 1.73 + */ 1.74 + private SessionInputBuffer in = null; 1.75 + 1.76 + /** 1.77 + * Wraps a session input buffer and cuts off output after a defined number 1.78 + * of bytes. 1.79 + * 1.80 + * @param in The session input buffer 1.81 + * @param contentLength The maximum number of bytes that can be read from 1.82 + * the stream. Subsequent read operations will return -1. 1.83 + */ 1.84 + public ContentLengthInputStream(final SessionInputBuffer in, long contentLength) { 1.85 + super(); 1.86 + if (in == null) { 1.87 + throw new IllegalArgumentException("Input stream may not be null"); 1.88 + } 1.89 + if (contentLength < 0) { 1.90 + throw new IllegalArgumentException("Content length may not be negative"); 1.91 + } 1.92 + this.in = in; 1.93 + this.contentLength = contentLength; 1.94 + } 1.95 + 1.96 + /** 1.97 + * <p>Reads until the end of the known length of content.</p> 1.98 + * 1.99 + * <p>Does not close the underlying socket input, but instead leaves it 1.100 + * primed to parse the next response.</p> 1.101 + * @throws IOException If an IO problem occurs. 1.102 + */ 1.103 + public void close() throws IOException { 1.104 + if (!closed) { 1.105 + try { 1.106 + if (pos < contentLength) { 1.107 + byte buffer[] = new byte[BUFFER_SIZE]; 1.108 + while (read(buffer) >= 0) { 1.109 + } 1.110 + } 1.111 + } finally { 1.112 + // close after above so that we don't throw an exception trying 1.113 + // to read after closed! 1.114 + closed = true; 1.115 + } 1.116 + } 1.117 + } 1.118 + 1.119 + public int available() throws IOException { 1.120 + if (this.in instanceof BufferInfo) { 1.121 + int len = ((BufferInfo) this.in).length(); 1.122 + return Math.min(len, (int) (this.contentLength - this.pos)); 1.123 + } else { 1.124 + return 0; 1.125 + } 1.126 + } 1.127 + 1.128 + /** 1.129 + * Read the next byte from the stream 1.130 + * @return The next byte or -1 if the end of stream has been reached. 1.131 + * @throws IOException If an IO problem occurs 1.132 + * @see java.io.InputStream#read() 1.133 + */ 1.134 + public int read() throws IOException { 1.135 + if (closed) { 1.136 + throw new IOException("Attempted read from closed stream."); 1.137 + } 1.138 + 1.139 + if (pos >= contentLength) { 1.140 + return -1; 1.141 + } 1.142 + int b = this.in.read(); 1.143 + if (b == -1) { 1.144 + if (pos < contentLength) { 1.145 + throw new ConnectionClosedException( 1.146 + "Premature end of Content-Length delimited message body (expected: " 1.147 + + contentLength + "; received: " + pos); 1.148 + } 1.149 + } else { 1.150 + pos++; 1.151 + } 1.152 + return b; 1.153 + } 1.154 + 1.155 + /** 1.156 + * Does standard {@link InputStream#read(byte[], int, int)} behavior, but 1.157 + * also notifies the watcher when the contents have been consumed. 1.158 + * 1.159 + * @param b The byte array to fill. 1.160 + * @param off Start filling at this position. 1.161 + * @param len The number of bytes to attempt to read. 1.162 + * @return The number of bytes read, or -1 if the end of content has been 1.163 + * reached. 1.164 + * 1.165 + * @throws java.io.IOException Should an error occur on the wrapped stream. 1.166 + */ 1.167 + public int read (byte[] b, int off, int len) throws java.io.IOException { 1.168 + if (closed) { 1.169 + throw new IOException("Attempted read from closed stream."); 1.170 + } 1.171 + 1.172 + if (pos >= contentLength) { 1.173 + return -1; 1.174 + } 1.175 + 1.176 + if (pos + len > contentLength) { 1.177 + len = (int) (contentLength - pos); 1.178 + } 1.179 + int count = this.in.read(b, off, len); 1.180 + if (count == -1 && pos < contentLength) { 1.181 + throw new ConnectionClosedException( 1.182 + "Premature end of Content-Length delimited message body (expected: " 1.183 + + contentLength + "; received: " + pos); 1.184 + } 1.185 + if (count > 0) { 1.186 + pos += count; 1.187 + } 1.188 + return count; 1.189 + } 1.190 + 1.191 + 1.192 + /** 1.193 + * Read more bytes from the stream. 1.194 + * @param b The byte array to put the new data in. 1.195 + * @return The number of bytes read into the buffer. 1.196 + * @throws IOException If an IO problem occurs 1.197 + * @see java.io.InputStream#read(byte[]) 1.198 + */ 1.199 + public int read(byte[] b) throws IOException { 1.200 + return read(b, 0, b.length); 1.201 + } 1.202 + 1.203 + /** 1.204 + * Skips and discards a number of bytes from the input stream. 1.205 + * @param n The number of bytes to skip. 1.206 + * @return The actual number of bytes skipped. <= 0 if no bytes 1.207 + * are skipped. 1.208 + * @throws IOException If an error occurs while skipping bytes. 1.209 + * @see InputStream#skip(long) 1.210 + */ 1.211 + public long skip(long n) throws IOException { 1.212 + if (n <= 0) { 1.213 + return 0; 1.214 + } 1.215 + byte[] buffer = new byte[BUFFER_SIZE]; 1.216 + // make sure we don't skip more bytes than are 1.217 + // still available 1.218 + long remaining = Math.min(n, this.contentLength - this.pos); 1.219 + // skip and keep track of the bytes actually skipped 1.220 + long count = 0; 1.221 + while (remaining > 0) { 1.222 + int l = read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining)); 1.223 + if (l == -1) { 1.224 + break; 1.225 + } 1.226 + count += l; 1.227 + remaining -= l; 1.228 + } 1.229 + return count; 1.230 + } 1.231 +}