mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/EofSensorInputStream.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/EofSensorInputStream.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,295 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  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, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.conn;
    1.31 +
    1.32 +import java.io.InputStream;
    1.33 +import java.io.IOException;
    1.34 +
    1.35 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.36 +
    1.37 +/**
    1.38 + * A stream wrapper that triggers actions on {@link #close close()} and EOF.
    1.39 + * Primarily used to auto-release an underlying
    1.40 + * {@link ManagedClientConnection connection}
    1.41 + * when the response body is consumed or no longer needed.
    1.42 + * <p>
    1.43 + * This class is based on <code>AutoCloseInputStream</code> in HttpClient 3.1,
    1.44 + * but has notable differences. It does not allow mark/reset, distinguishes
    1.45 + * different kinds of event, and does not always close the underlying stream
    1.46 + * on EOF. That decision is left to the {@link EofSensorWatcher watcher}.
    1.47 + *
    1.48 + * @see EofSensorWatcher
    1.49 + *
    1.50 + * @since 4.0
    1.51 + */
    1.52 +// don't use FilterInputStream as the base class, we'd have to
    1.53 +// override markSupported(), mark(), and reset() to disable them
    1.54 +@NotThreadSafe
    1.55 +public class EofSensorInputStream extends InputStream implements ConnectionReleaseTrigger {
    1.56 +
    1.57 +    /**
    1.58 +     * The wrapped input stream, while accessible.
    1.59 +     * The value changes to <code>null</code> when the wrapped stream
    1.60 +     * becomes inaccessible.
    1.61 +     */
    1.62 +    protected InputStream wrappedStream;
    1.63 +
    1.64 +    /**
    1.65 +     * Indicates whether this stream itself is closed.
    1.66 +     * If it isn't, but {@link #wrappedStream wrappedStream}
    1.67 +     * is <code>null</code>, we're running in EOF mode.
    1.68 +     * All read operations will indicate EOF without accessing
    1.69 +     * the underlying stream. After closing this stream, read
    1.70 +     * operations will trigger an {@link IOException IOException}.
    1.71 +     *
    1.72 +     * @see #isReadAllowed isReadAllowed
    1.73 +     */
    1.74 +    private boolean selfClosed;
    1.75 +
    1.76 +    /** The watcher to be notified, if any. */
    1.77 +    private final EofSensorWatcher eofWatcher;
    1.78 +
    1.79 +    /**
    1.80 +     * Creates a new EOF sensor.
    1.81 +     * If no watcher is passed, the underlying stream will simply be
    1.82 +     * closed when EOF is detected or {@link #close close} is called.
    1.83 +     * Otherwise, the watcher decides whether the underlying stream
    1.84 +     * should be closed before detaching from it.
    1.85 +     *
    1.86 +     * @param in        the wrapped stream
    1.87 +     * @param watcher   the watcher for events, or <code>null</code> for
    1.88 +     *                  auto-close behavior without notification
    1.89 +     */
    1.90 +    public EofSensorInputStream(final InputStream in,
    1.91 +                                final EofSensorWatcher watcher) {
    1.92 +        if (in == null) {
    1.93 +            throw new IllegalArgumentException
    1.94 +                ("Wrapped stream may not be null.");
    1.95 +        }
    1.96 +
    1.97 +        wrappedStream = in;
    1.98 +        selfClosed = false;
    1.99 +        eofWatcher = watcher;
   1.100 +    }
   1.101 +
   1.102 +    /**
   1.103 +     * Checks whether the underlying stream can be read from.
   1.104 +     *
   1.105 +     * @return  <code>true</code> if the underlying stream is accessible,
   1.106 +     *          <code>false</code> if this stream is in EOF mode and
   1.107 +     *          detached from the underlying stream
   1.108 +     *
   1.109 +     * @throws IOException      if this stream is already closed
   1.110 +     */
   1.111 +    protected boolean isReadAllowed() throws IOException {
   1.112 +        if (selfClosed) {
   1.113 +            throw new IOException("Attempted read on closed stream.");
   1.114 +        }
   1.115 +        return (wrappedStream != null);
   1.116 +    }
   1.117 +
   1.118 +    @Override
   1.119 +    public int read() throws IOException {
   1.120 +        int l = -1;
   1.121 +
   1.122 +        if (isReadAllowed()) {
   1.123 +            try {
   1.124 +                l = wrappedStream.read();
   1.125 +                checkEOF(l);
   1.126 +            } catch (IOException ex) {
   1.127 +                checkAbort();
   1.128 +                throw ex;
   1.129 +            }
   1.130 +        }
   1.131 +
   1.132 +        return l;
   1.133 +    }
   1.134 +
   1.135 +    @Override
   1.136 +    public int read(byte[] b, int off, int len) throws IOException {
   1.137 +        int l = -1;
   1.138 +
   1.139 +        if (isReadAllowed()) {
   1.140 +            try {
   1.141 +                l = wrappedStream.read(b,  off,  len);
   1.142 +                checkEOF(l);
   1.143 +            } catch (IOException ex) {
   1.144 +                checkAbort();
   1.145 +                throw ex;
   1.146 +            }
   1.147 +        }
   1.148 +
   1.149 +        return l;
   1.150 +    }
   1.151 +
   1.152 +    @Override
   1.153 +    public int read(byte[] b) throws IOException {
   1.154 +        int l = -1;
   1.155 +
   1.156 +        if (isReadAllowed()) {
   1.157 +            try {
   1.158 +                l = wrappedStream.read(b);
   1.159 +                checkEOF(l);
   1.160 +            } catch (IOException ex) {
   1.161 +                checkAbort();
   1.162 +                throw ex;
   1.163 +            }
   1.164 +        }
   1.165 +        return l;
   1.166 +    }
   1.167 +
   1.168 +    @Override
   1.169 +    public int available() throws IOException {
   1.170 +        int a = 0; // not -1
   1.171 +
   1.172 +        if (isReadAllowed()) {
   1.173 +            try {
   1.174 +                a = wrappedStream.available();
   1.175 +                // no checkEOF() here, available() can't trigger EOF
   1.176 +            } catch (IOException ex) {
   1.177 +                checkAbort();
   1.178 +                throw ex;
   1.179 +            }
   1.180 +        }
   1.181 +
   1.182 +        return a;
   1.183 +    }
   1.184 +
   1.185 +    @Override
   1.186 +    public void close() throws IOException {
   1.187 +        // tolerate multiple calls to close()
   1.188 +        selfClosed = true;
   1.189 +        checkClose();
   1.190 +    }
   1.191 +
   1.192 +    /**
   1.193 +     * Detects EOF and notifies the watcher.
   1.194 +     * This method should only be called while the underlying stream is
   1.195 +     * still accessible. Use {@link #isReadAllowed isReadAllowed} to
   1.196 +     * check that condition.
   1.197 +     * <br/>
   1.198 +     * If EOF is detected, the watcher will be notified and this stream
   1.199 +     * is detached from the underlying stream. This prevents multiple
   1.200 +     * notifications from this stream.
   1.201 +     *
   1.202 +     * @param eof       the result of the calling read operation.
   1.203 +     *                  A negative value indicates that EOF is reached.
   1.204 +     *
   1.205 +     * @throws IOException
   1.206 +     *          in case of an IO problem on closing the underlying stream
   1.207 +     */
   1.208 +    protected void checkEOF(int eof) throws IOException {
   1.209 +
   1.210 +        if ((wrappedStream != null) && (eof < 0)) {
   1.211 +            try {
   1.212 +                boolean scws = true; // should close wrapped stream?
   1.213 +                if (eofWatcher != null)
   1.214 +                    scws = eofWatcher.eofDetected(wrappedStream);
   1.215 +                if (scws)
   1.216 +                    wrappedStream.close();
   1.217 +            } finally {
   1.218 +                wrappedStream = null;
   1.219 +            }
   1.220 +        }
   1.221 +    }
   1.222 +
   1.223 +    /**
   1.224 +     * Detects stream close and notifies the watcher.
   1.225 +     * There's not much to detect since this is called by {@link #close close}.
   1.226 +     * The watcher will only be notified if this stream is closed
   1.227 +     * for the first time and before EOF has been detected.
   1.228 +     * This stream will be detached from the underlying stream to prevent
   1.229 +     * multiple notifications to the watcher.
   1.230 +     *
   1.231 +     * @throws IOException
   1.232 +     *          in case of an IO problem on closing the underlying stream
   1.233 +     */
   1.234 +    protected void checkClose() throws IOException {
   1.235 +
   1.236 +        if (wrappedStream != null) {
   1.237 +            try {
   1.238 +                boolean scws = true; // should close wrapped stream?
   1.239 +                if (eofWatcher != null)
   1.240 +                    scws = eofWatcher.streamClosed(wrappedStream);
   1.241 +                if (scws)
   1.242 +                    wrappedStream.close();
   1.243 +            } finally {
   1.244 +                wrappedStream = null;
   1.245 +            }
   1.246 +        }
   1.247 +    }
   1.248 +
   1.249 +    /**
   1.250 +     * Detects stream abort and notifies the watcher.
   1.251 +     * There's not much to detect since this is called by
   1.252 +     * {@link #abortConnection abortConnection}.
   1.253 +     * The watcher will only be notified if this stream is aborted
   1.254 +     * for the first time and before EOF has been detected or the
   1.255 +     * stream has been {@link #close closed} gracefully.
   1.256 +     * This stream will be detached from the underlying stream to prevent
   1.257 +     * multiple notifications to the watcher.
   1.258 +     *
   1.259 +     * @throws IOException
   1.260 +     *          in case of an IO problem on closing the underlying stream
   1.261 +     */
   1.262 +    protected void checkAbort() throws IOException {
   1.263 +
   1.264 +        if (wrappedStream != null) {
   1.265 +            try {
   1.266 +                boolean scws = true; // should close wrapped stream?
   1.267 +                if (eofWatcher != null)
   1.268 +                    scws = eofWatcher.streamAbort(wrappedStream);
   1.269 +                if (scws)
   1.270 +                    wrappedStream.close();
   1.271 +            } finally {
   1.272 +                wrappedStream = null;
   1.273 +            }
   1.274 +        }
   1.275 +    }
   1.276 +
   1.277 +    /**
   1.278 +     * Same as {@link #close close()}.
   1.279 +     */
   1.280 +    public void releaseConnection() throws IOException {
   1.281 +        close();
   1.282 +    }
   1.283 +
   1.284 +    /**
   1.285 +     * Aborts this stream.
   1.286 +     * This is a special version of {@link #close close()} which prevents
   1.287 +     * re-use of the underlying connection, if any. Calling this method
   1.288 +     * indicates that there should be no attempt to read until the end of
   1.289 +     * the stream.
   1.290 +     */
   1.291 +    public void abortConnection() throws IOException {
   1.292 +        // tolerate multiple calls
   1.293 +        selfClosed = true;
   1.294 +        checkAbort();
   1.295 +    }
   1.296 +
   1.297 +}
   1.298 +

mercurial