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

The following examples show how to use java.net.Socket#getSoTimeout() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * 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 Project: phonegapbootcampsite   File: HttpTransport.java    License: 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 3
Source Project: L.TileLayer.Cordova   File: HttpTransport.java    License: 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 Project: IoTgo_Android_App   File: HttpTransport.java    License: 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
/**
 * 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
/**  */
@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 7
/**  */
@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
/**  */
@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 9
/**
 * 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 Project: bluemix-parking-meter   File: HttpTransport.java    License: 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 Project: reader   File: HttpTransport.java    License: 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
/**
 * 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
@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 14
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 15
/**
 * 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 16
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 17
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
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 Project: big-c   File: SocketInputStream.java    License: 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 Project: stratosphere   File: SocketInputStream.java    License: 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());
}