mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/BasicEofSensorWatcher.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/BasicEofSensorWatcher.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/*
     1.5 + * $Revision $
     1.6 + * ====================================================================
     1.7 + *
     1.8 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.9 + *  contributor license agreements.  See the NOTICE file distributed with
    1.10 + *  this work for additional information regarding copyright ownership.
    1.11 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.12 + *  (the "License"); you may not use this file except in compliance with
    1.13 + *  the License.  You may obtain a copy of the License at
    1.14 + *
    1.15 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.16 + *
    1.17 + *  Unless required by applicable law or agreed to in writing, software
    1.18 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.19 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.20 + *  See the License for the specific language governing permissions and
    1.21 + *  limitations 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.conn;
    1.32 +
    1.33 +import java.io.InputStream;
    1.34 +import java.io.IOException;
    1.35 +
    1.36 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.37 +
    1.38 +/**
    1.39 + * Basic implementation of {@link EofSensorWatcher}. The underlying connection
    1.40 + * is released on close or EOF.
    1.41 + *
    1.42 + * @since 4.0
    1.43 + */
    1.44 +@NotThreadSafe
    1.45 +public class BasicEofSensorWatcher implements EofSensorWatcher {
    1.46 +
    1.47 +    /** The connection to auto-release. */
    1.48 +    protected final ManagedClientConnection managedConn;
    1.49 +
    1.50 +    /** Whether to keep the connection alive. */
    1.51 +    protected final boolean attemptReuse;
    1.52 +
    1.53 +    /**
    1.54 +     * Creates a new watcher for auto-releasing a connection.
    1.55 +     *
    1.56 +     * @param conn      the connection to auto-release
    1.57 +     * @param reuse     whether the connection should be re-used
    1.58 +     */
    1.59 +    public BasicEofSensorWatcher(ManagedClientConnection conn,
    1.60 +                                 boolean reuse) {
    1.61 +        if (conn == null)
    1.62 +            throw new IllegalArgumentException
    1.63 +                ("Connection may not be null.");
    1.64 +
    1.65 +        managedConn = conn;
    1.66 +        attemptReuse = reuse;
    1.67 +    }
    1.68 +
    1.69 +    public boolean eofDetected(InputStream wrapped)
    1.70 +        throws IOException {
    1.71 +
    1.72 +        try {
    1.73 +            if (attemptReuse) {
    1.74 +                // there may be some cleanup required, such as
    1.75 +                // reading trailers after the response body:
    1.76 +                wrapped.close();
    1.77 +                managedConn.markReusable();
    1.78 +            }
    1.79 +        } finally {
    1.80 +            managedConn.releaseConnection();
    1.81 +        }
    1.82 +        return false;
    1.83 +    }
    1.84 +
    1.85 +    public boolean streamClosed(InputStream wrapped)
    1.86 +        throws IOException {
    1.87 +
    1.88 +        try {
    1.89 +            if (attemptReuse) {
    1.90 +                // this assumes that closing the stream will
    1.91 +                // consume the remainder of the response body:
    1.92 +                wrapped.close();
    1.93 +                managedConn.markReusable();
    1.94 +            }
    1.95 +        } finally {
    1.96 +            managedConn.releaseConnection();
    1.97 +        }
    1.98 +        return false;
    1.99 +    }
   1.100 +
   1.101 +    public boolean streamAbort(InputStream wrapped)
   1.102 +        throws IOException {
   1.103 +
   1.104 +        managedConn.abortConnection();
   1.105 +        return false;
   1.106 +    }
   1.107 +
   1.108 +}

mercurial