/** * Attempts to exhaust {@code source}, returning true if successful. This is useful when reading a * complete source is helpful, such as when doing so completes a cache body or frees a socket * connection for reuse. */ public static boolean discard(Source source, int timeout, TimeUnit timeUnit) { try { return skipAll(source, timeout, timeUnit); } catch (IOException e) { return false; } }
public void addConnectException(IOException e) { addSuppressedIfPossible(firstException, e); lastException = e; } }
/** * Sets the default connect timeout for new connections. A value of 0 means no timeout, * otherwise values must be between 1 and {@link Integer#MAX_VALUE} when converted to * milliseconds. * * <p>The connect timeout is applied when connecting a TCP socket to the target host. * The default value is 10 seconds. */ public Builder connectTimeout(long timeout, TimeUnit unit) { connectTimeout = checkDuration("timeout", timeout, unit); return this; }
public void close() { for (Source in : sources) { Util.closeQuietly(in); } } }
private String defaultUserAgent() { String agent = Util.getSystemProperty("http.agent", null); return agent != null ? toHumanReadableAscii(agent) : Version.userAgent(); }
FormBody(List<String> encodedNames, List<String> encodedValues) { this.encodedNames = Util.immutableList(encodedNames); this.encodedValues = Util.immutableList(encodedValues); }
/** Equivalent to {@code string.substring(pos, limit).trim()}. */ public static String trimSubstring(String string, int pos, int limit) { int start = skipLeadingAsciiWhitespace(string, pos, limit); int end = skipTrailingAsciiWhitespace(string, start, limit); return string.substring(start, end); }
public NamedRunnable(String format, Object... args) { this.name = Util.format(format, args); }
private RouteDatabase routeDatabase() { return Internal.instance.routeDatabase(connectionPool); }
@Override public Interceptor.Chain withConnectTimeout(int timeout, TimeUnit unit) { int millis = checkDuration("timeout", timeout, unit); return new RealInterceptorChain(interceptors, streamAllocation, httpCodec, connection, index, request, call, eventListener, millis, readTimeout, writeTimeout); }
@Override public final void run() { String oldName = Thread.currentThread().getName(); Thread.currentThread().setName(name); try { execute(); } finally { Thread.currentThread().setName(oldName); } }
Request(Builder builder) { this.url = builder.url; this.method = builder.method; this.headers = builder.headers.build(); this.body = builder.body; this.tags = Util.immutableMap(builder.tags); }
static boolean percentEncoded(String encoded, int pos, int limit) { return pos + 2 < limit && encoded.charAt(pos) == '%' && decodeHexDigit(encoded.charAt(pos + 1)) != -1 && decodeHexDigit(encoded.charAt(pos + 2)) != -1; }
public void cancel() { // Close the raw socket so we don't end up doing synchronous I/O. closeQuietly(rawSocket); }
public Builder connectionSpecs(List<ConnectionSpec> connectionSpecs) { this.connectionSpecs = Util.immutableList(connectionSpecs); return this; }
/** * Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise * values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds. * * <p>The write timeout is applied for individual write IO operations. * The default value is 10 seconds. * * @see Sink#timeout() */ public Builder writeTimeout(long timeout, TimeUnit unit) { writeTimeout = checkDuration("timeout", timeout, unit); return this; }
@Override public void close() throws IOException { if (fileOperator == null) return; // Already closed. fileOperator = null; RandomAccessFile fileToClose = null; synchronized (Relay.this) { sourceCount--; if (sourceCount == 0) { fileToClose = file; file = null; } } if (fileToClose != null) { closeQuietly(fileToClose); } } }
/** * Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise * values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds. * * <p>The read timeout is applied to both the TCP socket and for individual read IO operations * including on {@link Source} of the {@link Response}. The default value is 10 seconds. * * @see Socket#setSoTimeout(int) * @see Source#timeout() */ public Builder readTimeout(long timeout, TimeUnit unit) { readTimeout = checkDuration("timeout", timeout, unit); return this; }
/** * Sets the default timeout for complete calls. A value of 0 means no timeout, otherwise values * must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds. * * <p>The call timeout spans the entire call: resolving DNS, connecting, writing the request * body, server processing, and reading the response body. If the call requires redirects or * retries all must complete within one timeout period. * * <p>The default value is 0 which imposes no timeout. */ public Builder callTimeout(long timeout, TimeUnit unit) { callTimeout = checkDuration("timeout", timeout, unit); return this; }
/** * Sets the interval between HTTP/2 and web socket pings initiated by this client. Use this to * automatically send ping frames until either the connection fails or it is closed. This keeps * the connection alive and may detect connectivity failures. * * <p>If the server does not respond to each ping with a pong within {@code interval}, this * client will assume that connectivity has been lost. When this happens on a web socket the * connection is canceled and its listener is {@linkplain WebSocketListener#onFailure notified * of the failure}. When it happens on an HTTP/2 connection the connection is closed and any * calls it is carrying {@linkplain java.io.IOException will fail with an IOException}. * * <p>The default value of 0 disables client-initiated pings. */ public Builder pingInterval(long interval, TimeUnit unit) { pingInterval = checkDuration("interval", interval, unit); return this; }