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 | * |
michael@0 | 4 | * Licensed to the Apache Software Foundation (ASF) under one or more |
michael@0 | 5 | * contributor license agreements. See the NOTICE file distributed with |
michael@0 | 6 | * this work for additional information regarding copyright ownership. |
michael@0 | 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
michael@0 | 8 | * (the "License"); you may not use this file except in compliance with |
michael@0 | 9 | * 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, software |
michael@0 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 16 | * See the License for the specific language governing permissions and |
michael@0 | 17 | * limitations under the License. |
michael@0 | 18 | * ==================================================================== |
michael@0 | 19 | * |
michael@0 | 20 | * This software consists of voluntary contributions made by many |
michael@0 | 21 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 22 | * information on the Apache Software Foundation, please see |
michael@0 | 23 | * <http://www.apache.org/>. |
michael@0 | 24 | * |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | package ch.boye.httpclientandroidlib.conn; |
michael@0 | 28 | |
michael@0 | 29 | import java.io.InputStream; |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
michael@0 | 33 | |
michael@0 | 34 | /** |
michael@0 | 35 | * A stream wrapper that triggers actions on {@link #close close()} and EOF. |
michael@0 | 36 | * Primarily used to auto-release an underlying |
michael@0 | 37 | * {@link ManagedClientConnection connection} |
michael@0 | 38 | * when the response body is consumed or no longer needed. |
michael@0 | 39 | * <p> |
michael@0 | 40 | * This class is based on <code>AutoCloseInputStream</code> in HttpClient 3.1, |
michael@0 | 41 | * but has notable differences. It does not allow mark/reset, distinguishes |
michael@0 | 42 | * different kinds of event, and does not always close the underlying stream |
michael@0 | 43 | * on EOF. That decision is left to the {@link EofSensorWatcher watcher}. |
michael@0 | 44 | * |
michael@0 | 45 | * @see EofSensorWatcher |
michael@0 | 46 | * |
michael@0 | 47 | * @since 4.0 |
michael@0 | 48 | */ |
michael@0 | 49 | // don't use FilterInputStream as the base class, we'd have to |
michael@0 | 50 | // override markSupported(), mark(), and reset() to disable them |
michael@0 | 51 | @NotThreadSafe |
michael@0 | 52 | public class EofSensorInputStream extends InputStream implements ConnectionReleaseTrigger { |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * The wrapped input stream, while accessible. |
michael@0 | 56 | * The value changes to <code>null</code> when the wrapped stream |
michael@0 | 57 | * becomes inaccessible. |
michael@0 | 58 | */ |
michael@0 | 59 | protected InputStream wrappedStream; |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * Indicates whether this stream itself is closed. |
michael@0 | 63 | * If it isn't, but {@link #wrappedStream wrappedStream} |
michael@0 | 64 | * is <code>null</code>, we're running in EOF mode. |
michael@0 | 65 | * All read operations will indicate EOF without accessing |
michael@0 | 66 | * the underlying stream. After closing this stream, read |
michael@0 | 67 | * operations will trigger an {@link IOException IOException}. |
michael@0 | 68 | * |
michael@0 | 69 | * @see #isReadAllowed isReadAllowed |
michael@0 | 70 | */ |
michael@0 | 71 | private boolean selfClosed; |
michael@0 | 72 | |
michael@0 | 73 | /** The watcher to be notified, if any. */ |
michael@0 | 74 | private final EofSensorWatcher eofWatcher; |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Creates a new EOF sensor. |
michael@0 | 78 | * If no watcher is passed, the underlying stream will simply be |
michael@0 | 79 | * closed when EOF is detected or {@link #close close} is called. |
michael@0 | 80 | * Otherwise, the watcher decides whether the underlying stream |
michael@0 | 81 | * should be closed before detaching from it. |
michael@0 | 82 | * |
michael@0 | 83 | * @param in the wrapped stream |
michael@0 | 84 | * @param watcher the watcher for events, or <code>null</code> for |
michael@0 | 85 | * auto-close behavior without notification |
michael@0 | 86 | */ |
michael@0 | 87 | public EofSensorInputStream(final InputStream in, |
michael@0 | 88 | final EofSensorWatcher watcher) { |
michael@0 | 89 | if (in == null) { |
michael@0 | 90 | throw new IllegalArgumentException |
michael@0 | 91 | ("Wrapped stream may not be null."); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | wrappedStream = in; |
michael@0 | 95 | selfClosed = false; |
michael@0 | 96 | eofWatcher = watcher; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | /** |
michael@0 | 100 | * Checks whether the underlying stream can be read from. |
michael@0 | 101 | * |
michael@0 | 102 | * @return <code>true</code> if the underlying stream is accessible, |
michael@0 | 103 | * <code>false</code> if this stream is in EOF mode and |
michael@0 | 104 | * detached from the underlying stream |
michael@0 | 105 | * |
michael@0 | 106 | * @throws IOException if this stream is already closed |
michael@0 | 107 | */ |
michael@0 | 108 | protected boolean isReadAllowed() throws IOException { |
michael@0 | 109 | if (selfClosed) { |
michael@0 | 110 | throw new IOException("Attempted read on closed stream."); |
michael@0 | 111 | } |
michael@0 | 112 | return (wrappedStream != null); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | @Override |
michael@0 | 116 | public int read() throws IOException { |
michael@0 | 117 | int l = -1; |
michael@0 | 118 | |
michael@0 | 119 | if (isReadAllowed()) { |
michael@0 | 120 | try { |
michael@0 | 121 | l = wrappedStream.read(); |
michael@0 | 122 | checkEOF(l); |
michael@0 | 123 | } catch (IOException ex) { |
michael@0 | 124 | checkAbort(); |
michael@0 | 125 | throw ex; |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | return l; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | @Override |
michael@0 | 133 | public int read(byte[] b, int off, int len) throws IOException { |
michael@0 | 134 | int l = -1; |
michael@0 | 135 | |
michael@0 | 136 | if (isReadAllowed()) { |
michael@0 | 137 | try { |
michael@0 | 138 | l = wrappedStream.read(b, off, len); |
michael@0 | 139 | checkEOF(l); |
michael@0 | 140 | } catch (IOException ex) { |
michael@0 | 141 | checkAbort(); |
michael@0 | 142 | throw ex; |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | return l; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | @Override |
michael@0 | 150 | public int read(byte[] b) throws IOException { |
michael@0 | 151 | int l = -1; |
michael@0 | 152 | |
michael@0 | 153 | if (isReadAllowed()) { |
michael@0 | 154 | try { |
michael@0 | 155 | l = wrappedStream.read(b); |
michael@0 | 156 | checkEOF(l); |
michael@0 | 157 | } catch (IOException ex) { |
michael@0 | 158 | checkAbort(); |
michael@0 | 159 | throw ex; |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | return l; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | @Override |
michael@0 | 166 | public int available() throws IOException { |
michael@0 | 167 | int a = 0; // not -1 |
michael@0 | 168 | |
michael@0 | 169 | if (isReadAllowed()) { |
michael@0 | 170 | try { |
michael@0 | 171 | a = wrappedStream.available(); |
michael@0 | 172 | // no checkEOF() here, available() can't trigger EOF |
michael@0 | 173 | } catch (IOException ex) { |
michael@0 | 174 | checkAbort(); |
michael@0 | 175 | throw ex; |
michael@0 | 176 | } |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | return a; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | @Override |
michael@0 | 183 | public void close() throws IOException { |
michael@0 | 184 | // tolerate multiple calls to close() |
michael@0 | 185 | selfClosed = true; |
michael@0 | 186 | checkClose(); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | /** |
michael@0 | 190 | * Detects EOF and notifies the watcher. |
michael@0 | 191 | * This method should only be called while the underlying stream is |
michael@0 | 192 | * still accessible. Use {@link #isReadAllowed isReadAllowed} to |
michael@0 | 193 | * check that condition. |
michael@0 | 194 | * <br/> |
michael@0 | 195 | * If EOF is detected, the watcher will be notified and this stream |
michael@0 | 196 | * is detached from the underlying stream. This prevents multiple |
michael@0 | 197 | * notifications from this stream. |
michael@0 | 198 | * |
michael@0 | 199 | * @param eof the result of the calling read operation. |
michael@0 | 200 | * A negative value indicates that EOF is reached. |
michael@0 | 201 | * |
michael@0 | 202 | * @throws IOException |
michael@0 | 203 | * in case of an IO problem on closing the underlying stream |
michael@0 | 204 | */ |
michael@0 | 205 | protected void checkEOF(int eof) throws IOException { |
michael@0 | 206 | |
michael@0 | 207 | if ((wrappedStream != null) && (eof < 0)) { |
michael@0 | 208 | try { |
michael@0 | 209 | boolean scws = true; // should close wrapped stream? |
michael@0 | 210 | if (eofWatcher != null) |
michael@0 | 211 | scws = eofWatcher.eofDetected(wrappedStream); |
michael@0 | 212 | if (scws) |
michael@0 | 213 | wrappedStream.close(); |
michael@0 | 214 | } finally { |
michael@0 | 215 | wrappedStream = null; |
michael@0 | 216 | } |
michael@0 | 217 | } |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | /** |
michael@0 | 221 | * Detects stream close and notifies the watcher. |
michael@0 | 222 | * There's not much to detect since this is called by {@link #close close}. |
michael@0 | 223 | * The watcher will only be notified if this stream is closed |
michael@0 | 224 | * for the first time and before EOF has been detected. |
michael@0 | 225 | * This stream will be detached from the underlying stream to prevent |
michael@0 | 226 | * multiple notifications to the watcher. |
michael@0 | 227 | * |
michael@0 | 228 | * @throws IOException |
michael@0 | 229 | * in case of an IO problem on closing the underlying stream |
michael@0 | 230 | */ |
michael@0 | 231 | protected void checkClose() throws IOException { |
michael@0 | 232 | |
michael@0 | 233 | if (wrappedStream != null) { |
michael@0 | 234 | try { |
michael@0 | 235 | boolean scws = true; // should close wrapped stream? |
michael@0 | 236 | if (eofWatcher != null) |
michael@0 | 237 | scws = eofWatcher.streamClosed(wrappedStream); |
michael@0 | 238 | if (scws) |
michael@0 | 239 | wrappedStream.close(); |
michael@0 | 240 | } finally { |
michael@0 | 241 | wrappedStream = null; |
michael@0 | 242 | } |
michael@0 | 243 | } |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | /** |
michael@0 | 247 | * Detects stream abort and notifies the watcher. |
michael@0 | 248 | * There's not much to detect since this is called by |
michael@0 | 249 | * {@link #abortConnection abortConnection}. |
michael@0 | 250 | * The watcher will only be notified if this stream is aborted |
michael@0 | 251 | * for the first time and before EOF has been detected or the |
michael@0 | 252 | * stream has been {@link #close closed} gracefully. |
michael@0 | 253 | * This stream will be detached from the underlying stream to prevent |
michael@0 | 254 | * multiple notifications to the watcher. |
michael@0 | 255 | * |
michael@0 | 256 | * @throws IOException |
michael@0 | 257 | * in case of an IO problem on closing the underlying stream |
michael@0 | 258 | */ |
michael@0 | 259 | protected void checkAbort() throws IOException { |
michael@0 | 260 | |
michael@0 | 261 | if (wrappedStream != null) { |
michael@0 | 262 | try { |
michael@0 | 263 | boolean scws = true; // should close wrapped stream? |
michael@0 | 264 | if (eofWatcher != null) |
michael@0 | 265 | scws = eofWatcher.streamAbort(wrappedStream); |
michael@0 | 266 | if (scws) |
michael@0 | 267 | wrappedStream.close(); |
michael@0 | 268 | } finally { |
michael@0 | 269 | wrappedStream = null; |
michael@0 | 270 | } |
michael@0 | 271 | } |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | /** |
michael@0 | 275 | * Same as {@link #close close()}. |
michael@0 | 276 | */ |
michael@0 | 277 | public void releaseConnection() throws IOException { |
michael@0 | 278 | close(); |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | /** |
michael@0 | 282 | * Aborts this stream. |
michael@0 | 283 | * This is a special version of {@link #close close()} which prevents |
michael@0 | 284 | * re-use of the underlying connection, if any. Calling this method |
michael@0 | 285 | * indicates that there should be no attempt to read until the end of |
michael@0 | 286 | * the stream. |
michael@0 | 287 | */ |
michael@0 | 288 | public void abortConnection() throws IOException { |
michael@0 | 289 | // tolerate multiple calls |
michael@0 | 290 | selfClosed = true; |
michael@0 | 291 | checkAbort(); |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | } |
michael@0 | 295 |