001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/io/SocketInputBuffer.java $
003: * $Revision: 560358 $
004: * $Date: 2007-07-27 21:30:42 +0200 (Fri, 27 Jul 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.io;
033:
034: import java.io.IOException;
035: import java.io.InterruptedIOException;
036: import java.net.Socket;
037:
038: import org.apache.http.params.HttpParams;
039:
040: /**
041: * {@link Socket} bound session input buffer.
042: *
043: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
044: *
045: * @version $Revision: 560358 $
046: *
047: * @since 4.0
048: */
049: public class SocketInputBuffer extends AbstractSessionInputBuffer {
050:
051: static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass();
052:
053: /**
054: * Returns <code>SocketTimeoutExceptionClass<code> or <code>null</code> if the class
055: * does not exist.
056: *
057: * @return <code>SocketTimeoutExceptionClass<code>, or <code>null</code> if unavailable.
058: */
059: static private Class SocketTimeoutExceptionClass() {
060: try {
061: return Class.forName("java.net.SocketTimeoutException");
062: } catch (ClassNotFoundException e) {
063: return null;
064: }
065: }
066:
067: private static boolean isSocketTimeoutException(
068: final InterruptedIOException e) {
069: if (SOCKET_TIMEOUT_CLASS != null) {
070: return SOCKET_TIMEOUT_CLASS.isInstance(e);
071: } else {
072: return true;
073: }
074: }
075:
076: private final Socket socket;
077:
078: public SocketInputBuffer(final Socket socket, int buffersize,
079: final HttpParams params) throws IOException {
080: super ();
081: if (socket == null) {
082: throw new IllegalArgumentException("Socket may not be null");
083: }
084: this .socket = socket;
085: if (buffersize < 0) {
086: buffersize = socket.getReceiveBufferSize();
087: }
088: if (buffersize < 1024) {
089: buffersize = 1024;
090: }
091: init(socket.getInputStream(), buffersize, params);
092: }
093:
094: public boolean isDataAvailable(int timeout) throws IOException {
095: boolean result = hasBufferedData();
096: if (!result) {
097: int oldtimeout = this .socket.getSoTimeout();
098: try {
099: this .socket.setSoTimeout(timeout);
100: fillBuffer();
101: result = hasBufferedData();
102: } catch (InterruptedIOException e) {
103: if (!isSocketTimeoutException(e)) {
104: throw e;
105: }
106: } finally {
107: socket.setSoTimeout(oldtimeout);
108: }
109: }
110: return result;
111: }
112:
113: }
|