Home | History | Annotate | Download | only in conn
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/BasicEofSensorWatcher.java $
      3  * $Revision $
      4  * $Date: 2008-06-27 12:49:20 -0700 (Fri, 27 Jun 2008) $
      5  *
      6  * ====================================================================
      7  *
      8  *  Licensed to the Apache Software Foundation (ASF) under one or more
      9  *  contributor license agreements.  See the NOTICE file distributed with
     10  *  this work for additional information regarding copyright ownership.
     11  *  The ASF licenses this file to You under the Apache License, Version 2.0
     12  *  (the "License"); you may not use this file except in compliance with
     13  *  the License.  You may obtain a copy of the License at
     14  *
     15  *      http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  *  Unless required by applicable law or agreed to in writing, software
     18  *  distributed under the License is distributed on an "AS IS" BASIS,
     19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  *  See the License for the specific language governing permissions and
     21  *  limitations under the License.
     22  * ====================================================================
     23  *
     24  * This software consists of voluntary contributions made by many
     25  * individuals on behalf of the Apache Software Foundation.  For more
     26  * information on the Apache Software Foundation, please see
     27  * <http://www.apache.org/>.
     28  *
     29  */
     30 
     31 package org.apache.http.conn;
     32 
     33 import java.io.InputStream;
     34 import java.io.IOException;
     35 
     36 
     37 /**
     38  * Basic implementation of {@link EofSensorWatcher EofSensorWatcher}.
     39  * The underlying connection is released on close or EOF.
     40  *
     41  * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
     42  *
     43  *
     44  * <!-- empty lines to avoid svn diff problems -->
     45  * @version $Revision: 672367 $
     46  *
     47  * @since 4.0
     48  *
     49  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     50  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     51  *     for further details.
     52  */
     53 @Deprecated
     54 public class BasicEofSensorWatcher implements EofSensorWatcher {
     55 
     56 
     57     /** The connection to auto-release. */
     58     protected ManagedClientConnection managedConn;
     59 
     60     /** Whether to keep the connection alive. */
     61     protected boolean attemptReuse;
     62 
     63 
     64 
     65     /**
     66      * Creates a new watcher for auto-releasing a connection.
     67      *
     68      * @param conn      the connection to auto-release
     69      * @param reuse     whether the connection should be re-used
     70      */
     71     public BasicEofSensorWatcher(ManagedClientConnection conn,
     72                                  boolean reuse) {
     73         if (conn == null)
     74             throw new IllegalArgumentException
     75                 ("Connection may not be null.");
     76 
     77         managedConn = conn;
     78         attemptReuse = reuse;
     79     }
     80 
     81 
     82     // non-javadoc, see interface EofSensorWatcher
     83     public boolean eofDetected(InputStream wrapped)
     84         throws IOException {
     85 
     86         try {
     87             if (attemptReuse) {
     88                 // there may be some cleanup required, such as
     89                 // reading trailers after the response body:
     90                 wrapped.close();
     91                 managedConn.markReusable();
     92             }
     93         } finally {
     94             managedConn.releaseConnection();
     95         }
     96         return false;
     97     }
     98 
     99 
    100     // non-javadoc, see interface EofSensorWatcher
    101     public boolean streamClosed(InputStream wrapped)
    102         throws IOException {
    103 
    104         try {
    105             if (attemptReuse) {
    106                 // this assumes that closing the stream will
    107                 // consume the remainder of the response body:
    108                 wrapped.close();
    109                 managedConn.markReusable();
    110             }
    111         } finally {
    112             managedConn.releaseConnection();
    113         }
    114         return false;
    115     }
    116 
    117 
    118     // non-javadoc, see interface EofSensorWatcher
    119     public boolean streamAbort(InputStream wrapped)
    120         throws IOException {
    121 
    122         managedConn.abortConnection();
    123         return false;
    124     }
    125 
    126 } // class BasicEofSensorWatcher
    127