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 */
27
28 package org.apache.http.impl.io;
29
30 import java.io.IOException;
31 import java.net.Socket;
32 import java.net.SocketTimeoutException;
33
34 import org.apache.http.annotation.NotThreadSafe;
35 import org.apache.http.io.EofSensor;
36 import org.apache.http.io.SessionInputBuffer;
37 import org.apache.http.params.HttpParams;
38
39 /**
40 * {@link SessionInputBuffer} implementation bound to a {@link Socket}.
41 * <p>
42 * The following parameters can be used to customize the behavior of this
43 * class:
44 * <ul>
45 * <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
46 * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
47 * <li>{@link org.apache.http.params.CoreConnectionPNames#MIN_CHUNK_LIMIT}</li>
48 * </ul>
49 *
50 * @since 4.0
51 */
52 @NotThreadSafe
53 public class SocketInputBuffer extends AbstractSessionInputBuffer implements EofSensor {
54
55 private final Socket socket;
56
57 private boolean eof;
58
59 /**
60 * Creates an instance of this class.
61 *
62 * @param socket the socket to read data from.
63 * @param buffersize the size of the internal buffer. If this number is less
64 * than <code>0</code> it is set to the value of
65 * {@link Socket#getReceiveBufferSize()}. If resultant number is less
66 * than <code>1024</code> it is set to <code>1024</code>.
67 * @param params HTTP parameters.
68 */
69 public SocketInputBuffer(
70 final Socket socket,
71 int buffersize,
72 final HttpParams params) throws IOException {
73 super();
74 if (socket == null) {
75 throw new IllegalArgumentException("Socket may not be null");
76 }
77 this.socket = socket;
78 this.eof = false;
79 if (buffersize < 0) {
80 buffersize = socket.getReceiveBufferSize();
81 }
82 if (buffersize < 1024) {
83 buffersize = 1024;
84 }
85 init(socket.getInputStream(), buffersize, params);
86 }
87
88 @Override
89 protected int fillBuffer() throws IOException {
90 int i = super.fillBuffer();
91 this.eof = i == -1;
92 return i;
93 }
94
95 public boolean isDataAvailable(int timeout) throws IOException {
96 boolean result = hasBufferedData();
97 if (!result) {
98 int oldtimeout = this.socket.getSoTimeout();
99 try {
100 this.socket.setSoTimeout(timeout);
101 fillBuffer();
102 result = hasBufferedData();
103 } catch (SocketTimeoutException ex) {
104 throw ex;
105 } finally {
106 socket.setSoTimeout(oldtimeout);
107 }
108 }
109 return result;
110 }
111
112 public boolean isEof() {
113 return this.eof;
114 }
115
116 }