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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * $Revision $
michael@0 3 * ====================================================================
michael@0 4 *
michael@0 5 * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0 6 * contributor license agreements. See the NOTICE file distributed with
michael@0 7 * this work for additional information regarding copyright ownership.
michael@0 8 * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0 9 * (the "License"); you may not use this file except in compliance with
michael@0 10 * the License. You may obtain a copy of the License at
michael@0 11 *
michael@0 12 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 13 *
michael@0 14 * Unless required by applicable law or agreed to in writing, software
michael@0 15 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 17 * See the License for the specific language governing permissions and
michael@0 18 * limitations 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.conn;
michael@0 29
michael@0 30 import java.io.InputStream;
michael@0 31 import java.io.IOException;
michael@0 32
michael@0 33 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0 34
michael@0 35 /**
michael@0 36 * Basic implementation of {@link EofSensorWatcher}. The underlying connection
michael@0 37 * is released on close or EOF.
michael@0 38 *
michael@0 39 * @since 4.0
michael@0 40 */
michael@0 41 @NotThreadSafe
michael@0 42 public class BasicEofSensorWatcher implements EofSensorWatcher {
michael@0 43
michael@0 44 /** The connection to auto-release. */
michael@0 45 protected final ManagedClientConnection managedConn;
michael@0 46
michael@0 47 /** Whether to keep the connection alive. */
michael@0 48 protected final boolean attemptReuse;
michael@0 49
michael@0 50 /**
michael@0 51 * Creates a new watcher for auto-releasing a connection.
michael@0 52 *
michael@0 53 * @param conn the connection to auto-release
michael@0 54 * @param reuse whether the connection should be re-used
michael@0 55 */
michael@0 56 public BasicEofSensorWatcher(ManagedClientConnection conn,
michael@0 57 boolean reuse) {
michael@0 58 if (conn == null)
michael@0 59 throw new IllegalArgumentException
michael@0 60 ("Connection may not be null.");
michael@0 61
michael@0 62 managedConn = conn;
michael@0 63 attemptReuse = reuse;
michael@0 64 }
michael@0 65
michael@0 66 public boolean eofDetected(InputStream wrapped)
michael@0 67 throws IOException {
michael@0 68
michael@0 69 try {
michael@0 70 if (attemptReuse) {
michael@0 71 // there may be some cleanup required, such as
michael@0 72 // reading trailers after the response body:
michael@0 73 wrapped.close();
michael@0 74 managedConn.markReusable();
michael@0 75 }
michael@0 76 } finally {
michael@0 77 managedConn.releaseConnection();
michael@0 78 }
michael@0 79 return false;
michael@0 80 }
michael@0 81
michael@0 82 public boolean streamClosed(InputStream wrapped)
michael@0 83 throws IOException {
michael@0 84
michael@0 85 try {
michael@0 86 if (attemptReuse) {
michael@0 87 // this assumes that closing the stream will
michael@0 88 // consume the remainder of the response body:
michael@0 89 wrapped.close();
michael@0 90 managedConn.markReusable();
michael@0 91 }
michael@0 92 } finally {
michael@0 93 managedConn.releaseConnection();
michael@0 94 }
michael@0 95 return false;
michael@0 96 }
michael@0 97
michael@0 98 public boolean streamAbort(InputStream wrapped)
michael@0 99 throws IOException {
michael@0 100
michael@0 101 managedConn.abortConnection();
michael@0 102 return false;
michael@0 103 }
michael@0 104
michael@0 105 }

mercurial