Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl.io; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | import java.io.InputStream; |
michael@0 | 32 | |
michael@0 | 33 | import ch.boye.httpclientandroidlib.ConnectionClosedException; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.io.BufferInfo; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.io.SessionInputBuffer; |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * Input stream that cuts off after a defined number of bytes. This class |
michael@0 | 39 | * is used to receive content of HTTP messages where the end of the content |
michael@0 | 40 | * entity is determined by the value of the <code>Content-Length header</code>. |
michael@0 | 41 | * Entities transferred using this stream can be maximum {@link Long#MAX_VALUE} |
michael@0 | 42 | * long. |
michael@0 | 43 | * <p> |
michael@0 | 44 | * Note that this class NEVER closes the underlying stream, even when close |
michael@0 | 45 | * gets called. Instead, it will read until the "end" of its limit on |
michael@0 | 46 | * close, which allows for the seamless execution of subsequent HTTP 1.1 |
michael@0 | 47 | * requests, while not requiring the client to remember to read the entire |
michael@0 | 48 | * contents of the response. |
michael@0 | 49 | * |
michael@0 | 50 | * |
michael@0 | 51 | * @since 4.0 |
michael@0 | 52 | */ |
michael@0 | 53 | public class ContentLengthInputStream extends InputStream { |
michael@0 | 54 | |
michael@0 | 55 | private static final int BUFFER_SIZE = 2048; |
michael@0 | 56 | /** |
michael@0 | 57 | * The maximum number of bytes that can be read from the stream. Subsequent |
michael@0 | 58 | * read operations will return -1. |
michael@0 | 59 | */ |
michael@0 | 60 | private long contentLength; |
michael@0 | 61 | |
michael@0 | 62 | /** The current position */ |
michael@0 | 63 | private long pos = 0; |
michael@0 | 64 | |
michael@0 | 65 | /** True if the stream is closed. */ |
michael@0 | 66 | private boolean closed = false; |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Wrapped input stream that all calls are delegated to. |
michael@0 | 70 | */ |
michael@0 | 71 | private SessionInputBuffer in = null; |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Wraps a session input buffer and cuts off output after a defined number |
michael@0 | 75 | * of bytes. |
michael@0 | 76 | * |
michael@0 | 77 | * @param in The session input buffer |
michael@0 | 78 | * @param contentLength The maximum number of bytes that can be read from |
michael@0 | 79 | * the stream. Subsequent read operations will return -1. |
michael@0 | 80 | */ |
michael@0 | 81 | public ContentLengthInputStream(final SessionInputBuffer in, long contentLength) { |
michael@0 | 82 | super(); |
michael@0 | 83 | if (in == null) { |
michael@0 | 84 | throw new IllegalArgumentException("Input stream may not be null"); |
michael@0 | 85 | } |
michael@0 | 86 | if (contentLength < 0) { |
michael@0 | 87 | throw new IllegalArgumentException("Content length may not be negative"); |
michael@0 | 88 | } |
michael@0 | 89 | this.in = in; |
michael@0 | 90 | this.contentLength = contentLength; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * <p>Reads until the end of the known length of content.</p> |
michael@0 | 95 | * |
michael@0 | 96 | * <p>Does not close the underlying socket input, but instead leaves it |
michael@0 | 97 | * primed to parse the next response.</p> |
michael@0 | 98 | * @throws IOException If an IO problem occurs. |
michael@0 | 99 | */ |
michael@0 | 100 | public void close() throws IOException { |
michael@0 | 101 | if (!closed) { |
michael@0 | 102 | try { |
michael@0 | 103 | if (pos < contentLength) { |
michael@0 | 104 | byte buffer[] = new byte[BUFFER_SIZE]; |
michael@0 | 105 | while (read(buffer) >= 0) { |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | } finally { |
michael@0 | 109 | // close after above so that we don't throw an exception trying |
michael@0 | 110 | // to read after closed! |
michael@0 | 111 | closed = true; |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | public int available() throws IOException { |
michael@0 | 117 | if (this.in instanceof BufferInfo) { |
michael@0 | 118 | int len = ((BufferInfo) this.in).length(); |
michael@0 | 119 | return Math.min(len, (int) (this.contentLength - this.pos)); |
michael@0 | 120 | } else { |
michael@0 | 121 | return 0; |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | /** |
michael@0 | 126 | * Read the next byte from the stream |
michael@0 | 127 | * @return The next byte or -1 if the end of stream has been reached. |
michael@0 | 128 | * @throws IOException If an IO problem occurs |
michael@0 | 129 | * @see java.io.InputStream#read() |
michael@0 | 130 | */ |
michael@0 | 131 | public int read() throws IOException { |
michael@0 | 132 | if (closed) { |
michael@0 | 133 | throw new IOException("Attempted read from closed stream."); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | if (pos >= contentLength) { |
michael@0 | 137 | return -1; |
michael@0 | 138 | } |
michael@0 | 139 | int b = this.in.read(); |
michael@0 | 140 | if (b == -1) { |
michael@0 | 141 | if (pos < contentLength) { |
michael@0 | 142 | throw new ConnectionClosedException( |
michael@0 | 143 | "Premature end of Content-Length delimited message body (expected: " |
michael@0 | 144 | + contentLength + "; received: " + pos); |
michael@0 | 145 | } |
michael@0 | 146 | } else { |
michael@0 | 147 | pos++; |
michael@0 | 148 | } |
michael@0 | 149 | return b; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | /** |
michael@0 | 153 | * Does standard {@link InputStream#read(byte[], int, int)} behavior, but |
michael@0 | 154 | * also notifies the watcher when the contents have been consumed. |
michael@0 | 155 | * |
michael@0 | 156 | * @param b The byte array to fill. |
michael@0 | 157 | * @param off Start filling at this position. |
michael@0 | 158 | * @param len The number of bytes to attempt to read. |
michael@0 | 159 | * @return The number of bytes read, or -1 if the end of content has been |
michael@0 | 160 | * reached. |
michael@0 | 161 | * |
michael@0 | 162 | * @throws java.io.IOException Should an error occur on the wrapped stream. |
michael@0 | 163 | */ |
michael@0 | 164 | public int read (byte[] b, int off, int len) throws java.io.IOException { |
michael@0 | 165 | if (closed) { |
michael@0 | 166 | throw new IOException("Attempted read from closed stream."); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | if (pos >= contentLength) { |
michael@0 | 170 | return -1; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | if (pos + len > contentLength) { |
michael@0 | 174 | len = (int) (contentLength - pos); |
michael@0 | 175 | } |
michael@0 | 176 | int count = this.in.read(b, off, len); |
michael@0 | 177 | if (count == -1 && pos < contentLength) { |
michael@0 | 178 | throw new ConnectionClosedException( |
michael@0 | 179 | "Premature end of Content-Length delimited message body (expected: " |
michael@0 | 180 | + contentLength + "; received: " + pos); |
michael@0 | 181 | } |
michael@0 | 182 | if (count > 0) { |
michael@0 | 183 | pos += count; |
michael@0 | 184 | } |
michael@0 | 185 | return count; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | |
michael@0 | 189 | /** |
michael@0 | 190 | * Read more bytes from the stream. |
michael@0 | 191 | * @param b The byte array to put the new data in. |
michael@0 | 192 | * @return The number of bytes read into the buffer. |
michael@0 | 193 | * @throws IOException If an IO problem occurs |
michael@0 | 194 | * @see java.io.InputStream#read(byte[]) |
michael@0 | 195 | */ |
michael@0 | 196 | public int read(byte[] b) throws IOException { |
michael@0 | 197 | return read(b, 0, b.length); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | /** |
michael@0 | 201 | * Skips and discards a number of bytes from the input stream. |
michael@0 | 202 | * @param n The number of bytes to skip. |
michael@0 | 203 | * @return The actual number of bytes skipped. <= 0 if no bytes |
michael@0 | 204 | * are skipped. |
michael@0 | 205 | * @throws IOException If an error occurs while skipping bytes. |
michael@0 | 206 | * @see InputStream#skip(long) |
michael@0 | 207 | */ |
michael@0 | 208 | public long skip(long n) throws IOException { |
michael@0 | 209 | if (n <= 0) { |
michael@0 | 210 | return 0; |
michael@0 | 211 | } |
michael@0 | 212 | byte[] buffer = new byte[BUFFER_SIZE]; |
michael@0 | 213 | // make sure we don't skip more bytes than are |
michael@0 | 214 | // still available |
michael@0 | 215 | long remaining = Math.min(n, this.contentLength - this.pos); |
michael@0 | 216 | // skip and keep track of the bytes actually skipped |
michael@0 | 217 | long count = 0; |
michael@0 | 218 | while (remaining > 0) { |
michael@0 | 219 | int l = read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining)); |
michael@0 | 220 | if (l == -1) { |
michael@0 | 221 | break; |
michael@0 | 222 | } |
michael@0 | 223 | count += l; |
michael@0 | 224 | remaining -= l; |
michael@0 | 225 | } |
michael@0 | 226 | return count; |
michael@0 | 227 | } |
michael@0 | 228 | } |