Java Code Examples for java.net.Socket#getSoTimeout()

The following examples show how to use java.net.Socket#getSoTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpTransport.java    From IoTgo_Android_App with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 2
Source File: HttpTransport.java    From cordova-amazon-fireos with Apache License 2.0 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 3
Source File: HttpTransport.java    From reader with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 4
Source File: HttpTransport.java    From bluemix-parking-meter with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 5
Source File: HttpTransport.java    From phonegap-plugin-loading-spinner with Apache License 2.0 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 6
Source File: TcpClientDiscoverySpiFailureTimeoutSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**  */
@Override protected void writeToSocket(
    TcpDiscoveryAbstractMessage msg,
    Socket sock,
    int res,
    long timeout
) throws IOException {
    if (writeToSocketDelay > 0) {
        try {
            U.dumpStack(log, "Before sleep [msg=" + msg + ']');

            Thread.sleep(writeToSocketDelay);
        }
        catch (InterruptedException e) {
            // Nothing to do.
        }
    }

    if (sock.getSoTimeout() >= writeToSocketDelay)
        super.writeToSocket(msg, sock, res, timeout);
    else
        throw new SocketTimeoutException("Write to socket delay timeout exception.");
}
 
Example 7
Source File: TcpClientDiscoverySpiFailureTimeoutSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**  */
@Override protected void writeToSocket(
    Socket sock,
    TcpDiscoveryAbstractMessage msg,
    long timeout
) throws IOException, IgniteCheckedException {
    if (writeToSocketDelay > 0) {
        try {
            U.dumpStack(log, "Before sleep [msg=" + msg + ']');

            Thread.sleep(writeToSocketDelay);
        }
        catch (InterruptedException e) {
            // Nothing to do.
        }
    }

    if (sock.getSoTimeout() >= writeToSocketDelay)
        super.writeToSocket(sock, msg, timeout);
    else
        throw new SocketTimeoutException("Write to socket delay timeout exception.");
}
 
Example 8
Source File: TcpClientDiscoverySpiFailureTimeoutSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**  */
@Override protected void writeToSocket(Socket sock,
    OutputStream out,
    TcpDiscoveryAbstractMessage msg,
    long timeout) throws IOException, IgniteCheckedException {
    if (writeToSocketDelay > 0) {
        try {
            U.dumpStack(log, "Before sleep [msg=" + msg + ']');

            Thread.sleep(writeToSocketDelay);
        }
        catch (InterruptedException e) {
            // Nothing to do.
        }
    }

    if (sock.getSoTimeout() >= writeToSocketDelay)
        super.writeToSocket(sock, out, msg, timeout);
    else
        throw new SocketTimeoutException("Write to socket delay timeout exception.");
}
 
Example 9
Source File: HttpTransport.java    From cordova-android-chromeview with Apache License 2.0 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 10
Source File: HttpTransport.java    From CordovaYoutubeVideoPlayer with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 11
Source File: HttpTransport.java    From L.TileLayer.Cordova with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 12
Source File: HttpTransport.java    From phonegapbootcampsite with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example 13
Source File: ExtendedSSLSocketFactory.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
public Socket connectSocket(
        final int connectTimeout,
        final Socket socket,
        final HttpHost host,
        final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress,
        final HttpContext context) throws IOException {
    Args.notNull(host, "HTTP host");
    Args.notNull(remoteAddress, "Remote address");
    final Socket sock = socket != null ? socket : createSocket(context);
    if (localAddress != null) {
        sock.bind(localAddress);
    }
    try {
        if (connectTimeout > 0 && sock.getSoTimeout() == 0) {
            sock.setSoTimeout(connectTimeout);
        }
        sock.connect(remoteAddress, connectTimeout);
    } catch (final IOException ex) {
        try {
            sock.close();
        } catch (final IOException ignore) {
        }
        throw ex;
    }
    // Setup SSL layering if necessary
    if (sock instanceof SSLSocket) {
        final SSLSocket sslsock = (SSLSocket) sock;
        sslsock.startHandshake();
        verifyHostname(sslsock, host.getHostName());
        return sock;
    } else {
        return createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context);
    }
}
 
Example 14
Source File: SocketStream.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads bytes from the socket.
 *
 * @param buf byte buffer receiving the bytes
 * @param offset offset into the buffer
 * @param length number of bytes to read
 * @return number of bytes read or -1
 * @exception throws ClientDisconnectException if the connection is dropped
 */
@Override
public int readTimeout(byte []buf, int offset, int length, long timeout)
  throws IOException
{
  Socket s = _s;
    
  if (s == null) {
    return -1;
  }
  
  int oldTimeout = s.getSoTimeout();
  s.setSoTimeout((int) timeout);
  
  try {
    int result = read(buf, offset, length);
    
    if (result >= 0) {
      return result;
    }
    else if (_is == null || _is.available() < 0) {
      return -1;
    }
    else {
      return ReadStream.READ_TIMEOUT;
    }
  } finally {
    s.setSoTimeout(oldTimeout);
  }
}
 
Example 15
Source File: BHttpConnectionBase.java    From java-android-websocket-client with Apache License 2.0 5 votes vote down vote up
private int fillInputBuffer(final int timeout) throws IOException {
    final Socket socket = this.socketHolder.get();
    final int oldtimeout = socket.getSoTimeout();
    try {
        socket.setSoTimeout(timeout);
        return this.inbuffer.fillBuffer();
    } finally {
        socket.setSoTimeout(oldtimeout);
    }
}
 
Example 16
Source File: BHttpConnectionBase.java    From java-android-websocket-client with Apache License 2.0 5 votes vote down vote up
@Override
public int getSocketTimeout() {
    final Socket socket = this.socketHolder.get();
    if (socket != null) {
        try {
            return socket.getSoTimeout();
        } catch (final SocketException ignore) {
            return -1;
        }
    } else {
        return -1;
    }
}
 
Example 17
Source File: ServerHandShakeProcessor.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private static Version readClientVersion(ServerConnection connection)
    throws IOException, VersionException {
  
  Socket socket = connection.getSocket();
  int timeout = connection.getHandShakeTimeout();

  int soTimeout = -1;
  try {
    soTimeout = socket.getSoTimeout();
    socket.setSoTimeout(timeout);
    InputStream is = socket.getInputStream();
    short clientVersionOrdinal = Version.readOrdinalFromInputStream(is);
    if (clientVersionOrdinal == -1) {
      throw new EOFException(
        LocalizedStrings.ServerHandShakeProcessor_HANDSHAKEREADER_EOF_REACHED_BEFORE_CLIENT_VERSION_COULD_BE_READ.toLocalizedString()); 
    }
    Version clientVersion = null;      
    try{
      clientVersion = Version.fromOrdinal(clientVersionOrdinal, true);
    }
    catch (UnsupportedGFXDVersionException uve) {
      // Allows higher version of wan site to connect to server
      if(connection.getCommunicationMode() == Acceptor.GATEWAY_TO_GATEWAY 
          && ! (clientVersionOrdinal == Version.NOT_SUPPORTED_ORDINAL)) {
        return Acceptor.VERSION;
      } else {
        SocketAddress sa = socket.getRemoteSocketAddress();
        String sInfo = "";
        if (sa != null) {
          sInfo = " Client: " + sa.toString() + ".";
        }
        throw new UnsupportedVersionException(uve.getMessage() + sInfo);
      }
    }
  
    if (!clientVersion.compatibleWith(Acceptor.VERSION)) {
      throw new IncompatibleVersionException(clientVersion, Acceptor.VERSION);//we can throw this to restrict
    }                                                 // Backward Compatibilty Support to limited no of versions          
    return clientVersion;                             
  } finally {
    if (soTimeout != -1) {
      try {        
        socket.setSoTimeout(soTimeout);
      }
      catch (IOException ignore) {
      }
    }
  }
}
 
Example 18
Source File: ServerHandShakeProcessor.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private static Version readClientVersion(ServerConnection connection)
    throws IOException, VersionException {
  
  Socket socket = connection.getSocket();
  int timeout = connection.getHandShakeTimeout();

  int soTimeout = -1;
  try {
    soTimeout = socket.getSoTimeout();
    socket.setSoTimeout(timeout);
    InputStream is = socket.getInputStream();
    short clientVersionOrdinal = Version.readOrdinalFromInputStream(is);
    if (clientVersionOrdinal == -1) {
      throw new EOFException(
        LocalizedStrings.ServerHandShakeProcessor_HANDSHAKEREADER_EOF_REACHED_BEFORE_CLIENT_VERSION_COULD_BE_READ.toLocalizedString()); 
    }
    Version clientVersion = null;      
    try{
      clientVersion = Version.fromOrdinal(clientVersionOrdinal, true);
    }
    catch (UnsupportedGFXDVersionException uve) {
      // Allows higher version of wan site to connect to server
      if(connection.getCommunicationMode() == Acceptor.GATEWAY_TO_GATEWAY 
          && ! (clientVersionOrdinal == Version.NOT_SUPPORTED_ORDINAL)) {
        return Acceptor.VERSION;
      } else {
        SocketAddress sa = socket.getRemoteSocketAddress();
        String sInfo = "";
        if (sa != null) {
          sInfo = " Client: " + sa.toString() + ".";
        }
        throw new UnsupportedVersionException(uve.getMessage() + sInfo);
      }
    }
  
    if (!clientVersion.compatibleWith(Acceptor.VERSION)) {
      throw new IncompatibleVersionException(clientVersion, Acceptor.VERSION);//we can throw this to restrict
    }                                                 // Backward Compatibilty Support to limited no of versions          
    return clientVersion;                             
  } finally {
    if (soTimeout != -1) {
      try {        
        socket.setSoTimeout(soTimeout);
      }
      catch (IOException ignore) {
      }
    }
  }
}
 
Example 19
Source File: SocketInputStream.java    From stratosphere with Apache License 2.0 2 votes vote down vote up
/**
 * Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout())
 * :<br>
 * <br>
 * Create a new input stream with the given timeout. If the timeout
 * is zero, it will be treated as infinite timeout. The socket's
 * channel will be configured to be non-blocking.
 * 
 * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long)
 * @param socket
 *        should have a channel associated with it.
 * @throws IOException
 */
public SocketInputStream(Socket socket)
										throws IOException {
	this(socket.getChannel(), socket.getSoTimeout());
}
 
Example 20
Source File: SocketInputStream.java    From big-c with Apache License 2.0 2 votes vote down vote up
/**
 * Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout())
 * :<br><br>
 * 
 * Create a new input stream with the given timeout. If the timeout
 * is zero, it will be treated as infinite timeout. The socket's
 * channel will be configured to be non-blocking.
 * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long)
 *  
 * @param socket should have a channel associated with it.
 * @throws IOException
 */
public SocketInputStream(Socket socket) throws IOException {
  this(socket.getChannel(), socket.getSoTimeout());
}