mirror of
https://github.com/square/okhttp.git
synced 2025-08-07 12:42:57 +03:00
Run IntelliJ's analysis on OkHttp.
This made a few hundred suggestions, many of which are thoughtful but I'm deliberately ignoring anyway. The following fixes are good ideas.
This commit is contained in:
@@ -78,7 +78,7 @@ public final class CacheAdapter implements InternalCache {
|
||||
// cacheable or the client should be careful about caching it.
|
||||
}
|
||||
|
||||
@Override public void update(Response cached, Response network) throws IOException {
|
||||
@Override public void update(Response cached, Response network) {
|
||||
// This method is treated as optional and there is no obvious way of implementing it with
|
||||
// ResponseCache. Updating headers is useful if the server changes the metadata for a resource
|
||||
// (e.g. max age) to extend or truncate the life of that resource in the cache. If the metadata
|
||||
|
@@ -74,7 +74,7 @@ public final class OkApacheClient implements HttpClient {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private static HttpResponse transformResponse(Response response) throws IOException {
|
||||
private static HttpResponse transformResponse(Response response) {
|
||||
int code = response.code();
|
||||
String message = response.message();
|
||||
BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);
|
||||
|
@@ -274,7 +274,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
||||
* Returns true if the body in question probably contains human readable text. Uses a small sample
|
||||
* of code points to detect unicode control characters commonly used in binary file signatures.
|
||||
*/
|
||||
static boolean isPlaintext(Buffer buffer) throws EOFException {
|
||||
static boolean isPlaintext(Buffer buffer) {
|
||||
try {
|
||||
Buffer prefix = new Buffer();
|
||||
long byteCount = buffer.size() < 64 ? buffer.size() : 64;
|
||||
|
@@ -18,7 +18,6 @@ package okhttp3;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
/**
|
||||
@@ -26,41 +25,35 @@ import javax.net.SocketFactory;
|
||||
* overriding {@link #configureSocket(java.net.Socket)}.
|
||||
*/
|
||||
public class DelegatingSocketFactory extends SocketFactory {
|
||||
|
||||
private final SocketFactory delegate;
|
||||
|
||||
public DelegatingSocketFactory(SocketFactory delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket createSocket() throws IOException {
|
||||
@Override public Socket createSocket() throws IOException {
|
||||
Socket socket = delegate.createSocket();
|
||||
return configureSocket(socket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
|
||||
@Override public Socket createSocket(String host, int port) throws IOException {
|
||||
Socket socket = delegate.createSocket(host, port);
|
||||
return configureSocket(socket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
|
||||
throws IOException, UnknownHostException {
|
||||
@Override public Socket createSocket(String host, int port, InetAddress localAddress,
|
||||
int localPort) throws IOException {
|
||||
Socket socket = delegate.createSocket(host, port, localAddress, localPort);
|
||||
return configureSocket(socket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket createSocket(InetAddress host, int port) throws IOException {
|
||||
@Override public Socket createSocket(InetAddress host, int port) throws IOException {
|
||||
Socket socket = delegate.createSocket(host, port);
|
||||
return configureSocket(socket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket createSocket(InetAddress host, int port, InetAddress localAddress, int localPort)
|
||||
throws IOException {
|
||||
@Override public Socket createSocket(InetAddress host, int port, InetAddress localAddress,
|
||||
int localPort) throws IOException {
|
||||
Socket socket = delegate.createSocket(host, port, localAddress, localPort);
|
||||
return configureSocket(socket);
|
||||
}
|
||||
|
@@ -49,7 +49,7 @@ public class FallbackTestClientSocketFactory extends DelegatingSSLSocketFactory
|
||||
}
|
||||
|
||||
@Override public void setEnabledCipherSuites(String[] suites) {
|
||||
List<String> enabledCipherSuites = new ArrayList<String>(suites.length);
|
||||
List<String> enabledCipherSuites = new ArrayList<>(suites.length);
|
||||
for (String suite : suites) {
|
||||
if (!suite.equals(TLS_FALLBACK_SCSV)) {
|
||||
enabledCipherSuites.add(suite);
|
||||
|
@@ -2094,7 +2094,7 @@ public final class URLConnectionTest {
|
||||
}
|
||||
|
||||
@Test public void redirectWithProxySelector() throws Exception {
|
||||
final List<URI> proxySelectionRequests = new ArrayList<URI>();
|
||||
final List<URI> proxySelectionRequests = new ArrayList<>();
|
||||
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||
.proxySelector(new ProxySelector() {
|
||||
@Override public List<Proxy> select(URI uri) {
|
||||
@@ -3680,7 +3680,7 @@ public final class URLConnectionTest {
|
||||
}
|
||||
|
||||
private static class RecordingTrustManager implements X509TrustManager {
|
||||
private final List<String> calls = new ArrayList<String>();
|
||||
private final List<String> calls = new ArrayList<>();
|
||||
private final X509TrustManager delegate;
|
||||
|
||||
public RecordingTrustManager(X509TrustManager delegate) {
|
||||
@@ -3702,7 +3702,7 @@ public final class URLConnectionTest {
|
||||
}
|
||||
|
||||
private String certificatesToString(X509Certificate[] certificates) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
List<String> result = new ArrayList<>();
|
||||
for (X509Certificate certificate : certificates) {
|
||||
result.add(certificate.getSubjectDN() + " " + certificate.getSerialNumber());
|
||||
}
|
||||
|
@@ -75,32 +75,32 @@ public class OptionalMethodTest {
|
||||
}
|
||||
|
||||
private final static OptionalMethod<BaseClass> STRING_METHOD_RETURNS_ANY =
|
||||
new OptionalMethod<BaseClass>(null, "stringMethod");
|
||||
new OptionalMethod<>(null, "stringMethod");
|
||||
private final static OptionalMethod<BaseClass> STRING_METHOD_RETURNS_STRING =
|
||||
new OptionalMethod<BaseClass>(String.class, "stringMethod");
|
||||
new OptionalMethod<>(String.class, "stringMethod");
|
||||
private final static OptionalMethod<BaseClass> STRING_METHOD_RETURNS_INT =
|
||||
new OptionalMethod<BaseClass>(Integer.TYPE, "stringMethod");
|
||||
new OptionalMethod<>(Integer.TYPE, "stringMethod");
|
||||
private final static OptionalMethod<BaseClass> VOID_METHOD_RETURNS_ANY =
|
||||
new OptionalMethod<BaseClass>(null, "voidMethod");
|
||||
new OptionalMethod<>(null, "voidMethod");
|
||||
private final static OptionalMethod<BaseClass> VOID_METHOD_RETURNS_VOID =
|
||||
new OptionalMethod<BaseClass>(Void.TYPE, "voidMethod");
|
||||
new OptionalMethod<>(Void.TYPE, "voidMethod");
|
||||
private final static OptionalMethod<BaseClass> SUBCLASS_METHOD_RETURNS_ANY =
|
||||
new OptionalMethod<BaseClass>(null, "subclassMethod");
|
||||
new OptionalMethod<>(null, "subclassMethod");
|
||||
private final static OptionalMethod<BaseClass> SUBCLASS_METHOD_RETURNS_STRING =
|
||||
new OptionalMethod<BaseClass>(String.class, "subclassMethod");
|
||||
new OptionalMethod<>(String.class, "subclassMethod");
|
||||
private final static OptionalMethod<BaseClass> SUBCLASS_METHOD_RETURNS_INT =
|
||||
new OptionalMethod<BaseClass>(Integer.TYPE, "subclassMethod");
|
||||
new OptionalMethod<>(Integer.TYPE, "subclassMethod");
|
||||
private final static OptionalMethod<BaseClass> METHOD_WITH_ARGS_WRONG_PARAMS =
|
||||
new OptionalMethod<BaseClass>(null, "methodWithArgs", Integer.class);
|
||||
new OptionalMethod<>(null, "methodWithArgs", Integer.class);
|
||||
private final static OptionalMethod<BaseClass> METHOD_WITH_ARGS_CORRECT_PARAMS =
|
||||
new OptionalMethod<BaseClass>(null, "methodWithArgs", String.class);
|
||||
new OptionalMethod<>(null, "methodWithArgs", String.class);
|
||||
|
||||
private final static OptionalMethod<BaseClass> THROWS_EXCEPTION =
|
||||
new OptionalMethod<BaseClass>(null, "throwsException");
|
||||
new OptionalMethod<>(null, "throwsException");
|
||||
private final static OptionalMethod<BaseClass> THROWS_RUNTIME_EXCEPTION =
|
||||
new OptionalMethod<BaseClass>(null, "throwsRuntimeException");
|
||||
new OptionalMethod<>(null, "throwsRuntimeException");
|
||||
private final static OptionalMethod<BaseClass> NON_PUBLIC =
|
||||
new OptionalMethod<BaseClass>(null, "nonPublic");
|
||||
new OptionalMethod<>(null, "nonPublic");
|
||||
|
||||
@Test
|
||||
public void isSupported() throws Exception {
|
||||
|
@@ -593,8 +593,6 @@ public final class OkHttpURLConnection extends HttpURLConnection implements Call
|
||||
@Override public Response intercept(Chain chain) throws IOException {
|
||||
try {
|
||||
return chain.proceed(chain.request());
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
} catch (Error | RuntimeException e) {
|
||||
throw new UnexpectedException(e);
|
||||
}
|
||||
|
@@ -137,7 +137,7 @@ public final class URLEncodingTest {
|
||||
@Override public void remove(Request request) throws IOException {
|
||||
}
|
||||
|
||||
@Override public void update(Response cached, Response network) throws IOException {
|
||||
@Override public void update(Response cached, Response network) {
|
||||
}
|
||||
|
||||
@Override public void trackConditionalCacheHit() {
|
||||
|
@@ -152,7 +152,7 @@ public final class Cache implements Closeable, Flushable {
|
||||
Cache.this.remove(request);
|
||||
}
|
||||
|
||||
@Override public void update(Response cached, Response network) throws IOException {
|
||||
@Override public void update(Response cached, Response network) {
|
||||
Cache.this.update(cached, network);
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ public final class Cache implements Closeable, Flushable {
|
||||
return response;
|
||||
}
|
||||
|
||||
private CacheRequest put(Response response) throws IOException {
|
||||
private CacheRequest put(Response response) {
|
||||
String requestMethod = response.request().method();
|
||||
|
||||
if (HttpMethod.invalidatesCache(response.request().method())) {
|
||||
@@ -432,7 +432,7 @@ public final class Cache implements Closeable, Flushable {
|
||||
private boolean done;
|
||||
private Sink body;
|
||||
|
||||
public CacheRequestImpl(final DiskLruCache.Editor editor) throws IOException {
|
||||
public CacheRequestImpl(final DiskLruCache.Editor editor) {
|
||||
this.editor = editor;
|
||||
this.cacheOut = editor.newSink(ENTRY_BODY);
|
||||
this.body = new ForwardingSink(cacheOut) {
|
||||
|
@@ -359,7 +359,6 @@ public final class CipherSuite {
|
||||
// public static final CipherSuite TLS_ECDHE_ECDSA_WITH_AES_256_CCM = of("TLS_ECDHE_ECDSA_WITH_AES_256_CCM", 0xc0ad, 7251, MAX_VALUE, MAX_VALUE);
|
||||
// public static final CipherSuite TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 = of("TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8", 0xc0ae, 7251, MAX_VALUE, MAX_VALUE);
|
||||
// public static final CipherSuite TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 = of("TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8", 0xc0af, 7251, MAX_VALUE, MAX_VALUE);
|
||||
;
|
||||
|
||||
final String javaName;
|
||||
|
||||
|
@@ -29,10 +29,8 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -244,11 +242,6 @@ public final class Util {
|
||||
return Collections.unmodifiableList(Arrays.asList(elements.clone()));
|
||||
}
|
||||
|
||||
/** Returns an immutable copy of {@code map}. */
|
||||
public static <K, V> Map<K, V> immutableMap(Map<K, V> map) {
|
||||
return Collections.unmodifiableMap(new LinkedHashMap<>(map));
|
||||
}
|
||||
|
||||
public static ThreadFactory threadFactory(final String name, final boolean daemon) {
|
||||
return new ThreadFactory() {
|
||||
@Override public Thread newThread(Runnable runnable) {
|
||||
|
@@ -258,7 +258,7 @@ public final class CacheInterceptor implements Interceptor {
|
||||
}
|
||||
|
||||
/** Combines cached headers with a network headers as defined by RFC 2616, 13.5.3. */
|
||||
private static Headers combine(Headers cachedHeaders, Headers networkHeaders) throws IOException {
|
||||
private static Headers combine(Headers cachedHeaders, Headers networkHeaders) {
|
||||
Headers.Builder result = new Headers.Builder();
|
||||
|
||||
for (int i = 0, size = cachedHeaders.size(); i < size; i++) {
|
||||
|
@@ -872,7 +872,7 @@ public final class DiskLruCache implements Closeable, Flushable {
|
||||
* Returns an unbuffered input stream to read the last committed value, or null if no value has
|
||||
* been committed.
|
||||
*/
|
||||
public Source newSource(int index) throws IOException {
|
||||
public Source newSource(int index) {
|
||||
synchronized (DiskLruCache.this) {
|
||||
if (done) {
|
||||
throw new IllegalStateException();
|
||||
@@ -893,7 +893,7 @@ public final class DiskLruCache implements Closeable, Flushable {
|
||||
* output stream encounters errors when writing to the filesystem, this edit will be aborted
|
||||
* when {@link #commit} is called. The returned output stream does not throw IOExceptions.
|
||||
*/
|
||||
public Sink newSink(int index) throws IOException {
|
||||
public Sink newSink(int index) {
|
||||
synchronized (DiskLruCache.this) {
|
||||
if (done) {
|
||||
throw new IllegalStateException();
|
||||
|
@@ -39,7 +39,7 @@ public interface InternalCache {
|
||||
* {@code network}. The cached response body is not updated. If the stored response has changed
|
||||
* since {@code cached} was returned, this does nothing.
|
||||
*/
|
||||
void update(Response cached, Response network) throws IOException;
|
||||
void update(Response cached, Response network);
|
||||
|
||||
/** Track an conditional GET that was satisfied by this cache. */
|
||||
void trackConditionalCacheHit();
|
||||
|
@@ -145,7 +145,7 @@ public final class RealConnection extends FramedConnection.Listener implements C
|
||||
throw new ProtocolException("Too many tunnel connections attempted: " + maxAttempts);
|
||||
}
|
||||
|
||||
connectSocket(connectTimeout, readTimeout, writeTimeout, connectionSpecSelector);
|
||||
connectSocket(connectTimeout, readTimeout);
|
||||
tunnelRequest = createTunnel(readTimeout, writeTimeout, tunnelRequest, url);
|
||||
|
||||
if (tunnelRequest == null) break; // Tunnel successfully created.
|
||||
@@ -164,12 +164,11 @@ public final class RealConnection extends FramedConnection.Listener implements C
|
||||
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */
|
||||
private void buildConnection(int connectTimeout, int readTimeout, int writeTimeout,
|
||||
ConnectionSpecSelector connectionSpecSelector) throws IOException {
|
||||
connectSocket(connectTimeout, readTimeout, writeTimeout, connectionSpecSelector);
|
||||
connectSocket(connectTimeout, readTimeout);
|
||||
establishProtocol(readTimeout, writeTimeout, connectionSpecSelector);
|
||||
}
|
||||
|
||||
private void connectSocket(int connectTimeout, int readTimeout, int writeTimeout,
|
||||
ConnectionSpecSelector connectionSpecSelector) throws IOException {
|
||||
private void connectSocket(int connectTimeout, int readTimeout) throws IOException {
|
||||
Proxy proxy = route.proxy();
|
||||
Address address = route.address();
|
||||
|
||||
@@ -331,7 +330,7 @@ public final class RealConnection extends FramedConnection.Listener implements C
|
||||
* is sent unencrypted to the proxy server, so tunnels include only the minimum set of headers.
|
||||
* This avoids sending potentially sensitive data like HTTP cookies to the proxy unencrypted.
|
||||
*/
|
||||
private Request createTunnelRequest() throws IOException {
|
||||
private Request createTunnelRequest() {
|
||||
return new Request.Builder()
|
||||
.url(route.address().url())
|
||||
.header("Host", Util.hostHeader(route.address().url(), true))
|
||||
@@ -340,11 +339,6 @@ public final class RealConnection extends FramedConnection.Listener implements C
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Returns true if {@link #connect} has been attempted on this connection. */
|
||||
boolean isConnected() {
|
||||
return protocol != null;
|
||||
}
|
||||
|
||||
@Override public Route route() {
|
||||
return route;
|
||||
}
|
||||
|
@@ -88,8 +88,7 @@ public final class StreamAllocation {
|
||||
this.routeSelector = new RouteSelector(address, routeDatabase());
|
||||
}
|
||||
|
||||
public HttpStream newStream(OkHttpClient client, boolean doExtensiveHealthChecks)
|
||||
throws IOException {
|
||||
public HttpStream newStream(OkHttpClient client, boolean doExtensiveHealthChecks) {
|
||||
int connectTimeout = client.connectTimeoutMillis();
|
||||
int readTimeout = client.readTimeoutMillis();
|
||||
int writeTimeout = client.writeTimeoutMillis();
|
||||
|
@@ -62,7 +62,7 @@ public enum ErrorCode {
|
||||
public final int spdyRstCode;
|
||||
public final int spdyGoAwayCode;
|
||||
|
||||
private ErrorCode(int httpCode, int spdyRstCode, int spdyGoAwayCode) {
|
||||
ErrorCode(int httpCode, int spdyRstCode, int spdyGoAwayCode) {
|
||||
this.httpCode = httpCode;
|
||||
this.spdyRstCode = spdyRstCode;
|
||||
this.spdyGoAwayCode = spdyGoAwayCode;
|
||||
|
@@ -126,7 +126,7 @@ public final class FramedConnection implements Closeable {
|
||||
// Visible for testing
|
||||
final Reader readerRunnable;
|
||||
|
||||
private FramedConnection(Builder builder) throws IOException {
|
||||
private FramedConnection(Builder builder) {
|
||||
protocol = builder.protocol;
|
||||
pushObserver = builder.pushObserver;
|
||||
client = builder.client;
|
||||
@@ -529,7 +529,7 @@ public final class FramedConnection implements Closeable {
|
||||
* @param client true if this peer initiated the connection; false if this peer accepted the
|
||||
* connection.
|
||||
*/
|
||||
public Builder(boolean client) throws IOException {
|
||||
public Builder(boolean client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
|
@@ -371,8 +371,7 @@ final class Hpack {
|
||||
private static final int SETTINGS_HEADER_TABLE_SIZE = 4096;
|
||||
|
||||
private final Buffer out;
|
||||
private final Map<ByteString, Integer> headerStringToDynamicIndex =
|
||||
new LinkedHashMap<ByteString, Integer>();
|
||||
private final Map<ByteString, Integer> headerStringToDynamicIndex = new LinkedHashMap<>();
|
||||
|
||||
private int headerTableSizeSetting;
|
||||
private int maxDynamicTableByteCount;
|
||||
@@ -494,7 +493,7 @@ final class Hpack {
|
||||
}
|
||||
|
||||
// http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-12#section-4.1.1
|
||||
void writeInt(int value, int prefixMask, int bits) throws IOException {
|
||||
void writeInt(int value, int prefixMask, int bits) {
|
||||
// Write the raw value for a single byte value.
|
||||
if (value < prefixMask) {
|
||||
out.writeByte(bits | value);
|
||||
|
@@ -124,7 +124,7 @@ class Huffman {
|
||||
return (int) ((len + 7) >> 3);
|
||||
}
|
||||
|
||||
byte[] decode(byte[] buf) throws IOException {
|
||||
byte[] decode(byte[] buf) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Node node = root;
|
||||
int current = 0;
|
||||
|
@@ -87,7 +87,7 @@ public final class Http1xStream implements HttpStream {
|
||||
this.sink = sink;
|
||||
}
|
||||
|
||||
@Override public Sink createRequestBody(Request request, long contentLength) throws IOException {
|
||||
@Override public Sink createRequestBody(Request request, long contentLength) {
|
||||
if ("chunked".equalsIgnoreCase(request.header("Transfer-Encoding"))) {
|
||||
// Stream a request body of unknown length.
|
||||
return newChunkedSink();
|
||||
|
@@ -119,7 +119,7 @@ public final class Http2xStream implements HttpStream {
|
||||
this.framedConnection = framedConnection;
|
||||
}
|
||||
|
||||
@Override public Sink createRequestBody(Request request, long contentLength) throws IOException {
|
||||
@Override public Sink createRequestBody(Request request, long contentLength) {
|
||||
return stream.getSink();
|
||||
}
|
||||
|
||||
|
@@ -38,10 +38,6 @@ public final class HttpHeaders {
|
||||
private HttpHeaders() {
|
||||
}
|
||||
|
||||
public static long contentLength(Request request) {
|
||||
return contentLength(request.headers());
|
||||
}
|
||||
|
||||
public static long contentLength(Response response) {
|
||||
return contentLength(response.headers());
|
||||
}
|
||||
|
@@ -30,7 +30,7 @@ public interface HttpStream {
|
||||
int DISCARD_STREAM_TIMEOUT_MILLIS = 100;
|
||||
|
||||
/** Returns an output stream where the request body can be streamed. */
|
||||
Sink createRequestBody(Request request, long contentLength) throws IOException;
|
||||
Sink createRequestBody(Request request, long contentLength);
|
||||
|
||||
/** This should update the HTTP engine's sentRequestMillis field. */
|
||||
void writeRequestHeaders(Request request) throws IOException;
|
||||
|
Reference in New Issue
Block a user