Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
28 package ch.boye.httpclientandroidlib.impl.io;
30 import java.io.IOException;
31 import java.io.InterruptedIOException;
32 import java.net.Socket;
34 import ch.boye.httpclientandroidlib.io.EofSensor;
35 import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
36 import ch.boye.httpclientandroidlib.params.HttpParams;
38 /**
39 * {@link SessionInputBuffer} implementation bound to a {@link Socket}.
40 * <p>
41 * The following parameters can be used to customize the behavior of this
42 * class:
43 * <ul>
44 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
45 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
46 * </ul>
47 *
48 * @since 4.0
49 */
50 public class SocketInputBuffer extends AbstractSessionInputBuffer implements EofSensor {
52 static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass();
54 /**
55 * Returns <code>SocketTimeoutExceptionClass<code> or <code>null</code> if the class
56 * does not exist.
57 *
58 * @return <code>SocketTimeoutExceptionClass<code>, or <code>null</code> if unavailable.
59 */
60 static private Class SocketTimeoutExceptionClass() {
61 try {
62 return Class.forName("java.net.SocketTimeoutException");
63 } catch (ClassNotFoundException e) {
64 return null;
65 }
66 }
68 private static boolean isSocketTimeoutException(final InterruptedIOException e) {
69 if (SOCKET_TIMEOUT_CLASS != null) {
70 return SOCKET_TIMEOUT_CLASS.isInstance(e);
71 } else {
72 return true;
73 }
74 }
76 private final Socket socket;
78 private boolean eof;
80 /**
81 * Creates an instance of this class.
82 *
83 * @param socket the socket to read data from.
84 * @param buffersize the size of the internal buffer. If this number is less
85 * than <code>0</code> it is set to the value of
86 * {@link Socket#getReceiveBufferSize()}. If resultant number is less
87 * than <code>1024</code> it is set to <code>1024</code>.
88 * @param params HTTP parameters.
89 */
90 public SocketInputBuffer(
91 final Socket socket,
92 int buffersize,
93 final HttpParams params) throws IOException {
94 super();
95 if (socket == null) {
96 throw new IllegalArgumentException("Socket may not be null");
97 }
98 this.socket = socket;
99 this.eof = false;
100 if (buffersize < 0) {
101 buffersize = socket.getReceiveBufferSize();
102 }
103 if (buffersize < 1024) {
104 buffersize = 1024;
105 }
106 init(socket.getInputStream(), buffersize, params);
107 }
109 protected int fillBuffer() throws IOException {
110 int i = super.fillBuffer();
111 this.eof = i == -1;
112 return i;
113 }
115 public boolean isDataAvailable(int timeout) throws IOException {
116 boolean result = hasBufferedData();
117 if (!result) {
118 int oldtimeout = this.socket.getSoTimeout();
119 try {
120 this.socket.setSoTimeout(timeout);
121 fillBuffer();
122 result = hasBufferedData();
123 } catch (InterruptedIOException e) {
124 if (!isSocketTimeoutException(e)) {
125 throw e;
126 }
127 } finally {
128 socket.setSoTimeout(oldtimeout);
129 }
130 }
131 return result;
132 }
134 public boolean isEof() {
135 return this.eof;
136 }
138 }