1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/SocketInputBuffer.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with 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, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * 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.impl.io; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.io.InterruptedIOException; 1.35 +import java.net.Socket; 1.36 + 1.37 +import ch.boye.httpclientandroidlib.io.EofSensor; 1.38 +import ch.boye.httpclientandroidlib.io.SessionInputBuffer; 1.39 +import ch.boye.httpclientandroidlib.params.HttpParams; 1.40 + 1.41 +/** 1.42 + * {@link SessionInputBuffer} implementation bound to a {@link Socket}. 1.43 + * <p> 1.44 + * The following parameters can be used to customize the behavior of this 1.45 + * class: 1.46 + * <ul> 1.47 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li> 1.48 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li> 1.49 + * </ul> 1.50 + * 1.51 + * @since 4.0 1.52 + */ 1.53 +public class SocketInputBuffer extends AbstractSessionInputBuffer implements EofSensor { 1.54 + 1.55 + static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass(); 1.56 + 1.57 + /** 1.58 + * Returns <code>SocketTimeoutExceptionClass<code> or <code>null</code> if the class 1.59 + * does not exist. 1.60 + * 1.61 + * @return <code>SocketTimeoutExceptionClass<code>, or <code>null</code> if unavailable. 1.62 + */ 1.63 + static private Class SocketTimeoutExceptionClass() { 1.64 + try { 1.65 + return Class.forName("java.net.SocketTimeoutException"); 1.66 + } catch (ClassNotFoundException e) { 1.67 + return null; 1.68 + } 1.69 + } 1.70 + 1.71 + private static boolean isSocketTimeoutException(final InterruptedIOException e) { 1.72 + if (SOCKET_TIMEOUT_CLASS != null) { 1.73 + return SOCKET_TIMEOUT_CLASS.isInstance(e); 1.74 + } else { 1.75 + return true; 1.76 + } 1.77 + } 1.78 + 1.79 + private final Socket socket; 1.80 + 1.81 + private boolean eof; 1.82 + 1.83 + /** 1.84 + * Creates an instance of this class. 1.85 + * 1.86 + * @param socket the socket to read data from. 1.87 + * @param buffersize the size of the internal buffer. If this number is less 1.88 + * than <code>0</code> it is set to the value of 1.89 + * {@link Socket#getReceiveBufferSize()}. If resultant number is less 1.90 + * than <code>1024</code> it is set to <code>1024</code>. 1.91 + * @param params HTTP parameters. 1.92 + */ 1.93 + public SocketInputBuffer( 1.94 + final Socket socket, 1.95 + int buffersize, 1.96 + final HttpParams params) throws IOException { 1.97 + super(); 1.98 + if (socket == null) { 1.99 + throw new IllegalArgumentException("Socket may not be null"); 1.100 + } 1.101 + this.socket = socket; 1.102 + this.eof = false; 1.103 + if (buffersize < 0) { 1.104 + buffersize = socket.getReceiveBufferSize(); 1.105 + } 1.106 + if (buffersize < 1024) { 1.107 + buffersize = 1024; 1.108 + } 1.109 + init(socket.getInputStream(), buffersize, params); 1.110 + } 1.111 + 1.112 + protected int fillBuffer() throws IOException { 1.113 + int i = super.fillBuffer(); 1.114 + this.eof = i == -1; 1.115 + return i; 1.116 + } 1.117 + 1.118 + public boolean isDataAvailable(int timeout) throws IOException { 1.119 + boolean result = hasBufferedData(); 1.120 + if (!result) { 1.121 + int oldtimeout = this.socket.getSoTimeout(); 1.122 + try { 1.123 + this.socket.setSoTimeout(timeout); 1.124 + fillBuffer(); 1.125 + result = hasBufferedData(); 1.126 + } catch (InterruptedIOException e) { 1.127 + if (!isSocketTimeoutException(e)) { 1.128 + throw e; 1.129 + } 1.130 + } finally { 1.131 + socket.setSoTimeout(oldtimeout); 1.132 + } 1.133 + } 1.134 + return result; 1.135 + } 1.136 + 1.137 + public boolean isEof() { 1.138 + return this.eof; 1.139 + } 1.140 + 1.141 +}