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  
33  import org.apache.http.annotation.NotThreadSafe;
34  import org.apache.http.io.EofSensor;
35  import org.apache.http.params.HttpParams;
36  import org.apache.http.util.Args;
37  
38  /**
39   * {@link org.apache.http.io.SessionInputBuffer} implementation
40   * bound to a {@link Socket}.
41   *
42   * @since 4.0
43   *
44   * @deprecated (4.3) use {@link SessionInputBufferImpl}
45   */
46  @NotThreadSafe
47  @Deprecated
48  public class SocketInputBuffer extends AbstractSessionInputBuffer implements EofSensor {
49  
50      private final Socket socket;
51  
52      private boolean eof;
53  
54      /**
55       * Creates an instance of this class.
56       *
57       * @param socket the socket to read data from.
58       * @param buffersize the size of the internal buffer. If this number is less
59       *   than <code>0</code> it is set to the value of
60       *   {@link Socket#getReceiveBufferSize()}. If resultant number is less
61       *   than <code>1024</code> it is set to <code>1024</code>.
62       * @param params HTTP parameters.
63       */
64      public SocketInputBuffer(
65              final Socket socket,
66              final int buffersize,
67              final HttpParams params) throws IOException {
68          super();
69          Args.notNull(socket, "Socket");
70          this.socket = socket;
71          this.eof = false;
72          int n = buffersize;
73          if (n < 0) {
74              n = socket.getReceiveBufferSize();
75          }
76          if (n < 1024) {
77              n = 1024;
78          }
79          init(socket.getInputStream(), n, 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              } finally {
98                  socket.setSoTimeout(oldtimeout);
99              }
100         }
101         return result;
102     }
103 
104     public boolean isEof() {
105         return this.eof;
106     }
107 
108 }