View Javadoc

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  import org.apache.http.util.Args;
39  
40  /**
41   * {@link SessionInputBuffer} implementation bound to a {@link Socket}.
42   *
43   * @since 4.0
44   *
45   * @deprecated (4.3) use {@link SessionInputBufferImpl}
46   */
47  @NotThreadSafe
48  @Deprecated
49  public class SocketInputBuffer extends AbstractSessionInputBuffer implements EofSensor {
50  
51      private final Socket socket;
52  
53      private boolean eof;
54  
55      /**
56       * Creates an instance of this class.
57       *
58       * @param socket the socket to read data from.
59       * @param buffersize the size of the internal buffer. If this number is less
60       *   than <code>0</code> it is set to the value of
61       *   {@link Socket#getReceiveBufferSize()}. If resultant number is less
62       *   than <code>1024</code> it is set to <code>1024</code>.
63       * @param params HTTP parameters.
64       */
65      public SocketInputBuffer(
66              final Socket socket,
67              int buffersize,
68              final HttpParams params) throws IOException {
69          super();
70          Args.notNull(socket, "Socket");
71          this.socket = socket;
72          this.eof = false;
73          if (buffersize < 0) {
74              buffersize = socket.getReceiveBufferSize();
75          }
76          if (buffersize < 1024) {
77              buffersize = 1024;
78          }
79          init(socket.getInputStream(), buffersize, params);
80      }
81  
82      @Override
83      protected int fillBuffer() throws IOException {
84          final int i = super.fillBuffer();
85          this.eof = i == -1;
86          return i;
87      }
88  
89      public boolean isDataAvailable(final int timeout) throws IOException {
90          boolean result = hasBufferedData();
91          if (!result) {
92              final int oldtimeout = this.socket.getSoTimeout();
93              try {
94                  this.socket.setSoTimeout(timeout);
95                  fillBuffer();
96                  result = hasBufferedData();
97              } catch (final SocketTimeoutException ex) {
98                  throw ex;
99              } finally {
100                 socket.setSoTimeout(oldtimeout);
101             }
102         }
103         return result;
104     }
105 
106     public boolean isEof() {
107         return this.eof;
108     }
109 
110 }