diff --git a/src/main/java/com/squareup/okhttp/Address.java b/src/main/java/com/squareup/okhttp/Address.java index 4ab77beb7..430eff5ed 100644 --- a/src/main/java/com/squareup/okhttp/Address.java +++ b/src/main/java/com/squareup/okhttp/Address.java @@ -15,7 +15,7 @@ */ package com.squareup.okhttp; -import com.squareup.okhttp.internal.util.Objects; +import static com.squareup.okhttp.internal.Util.equal; import java.net.Proxy; import java.net.UnknownHostException; import javax.net.ssl.HostnameVerifier; @@ -91,11 +91,11 @@ public final class Address { @Override public boolean equals(Object other) { if (other instanceof Address) { Address that = (Address) other; - return Objects.equal(this.proxy, that.proxy) + return equal(this.proxy, that.proxy) && this.uriHost.equals(that.uriHost) && this.uriPort == that.uriPort - && Objects.equal(this.sslSocketFactory, that.sslSocketFactory) - && Objects.equal(this.hostnameVerifier, that.hostnameVerifier); + && equal(this.sslSocketFactory, that.sslSocketFactory) + && equal(this.hostnameVerifier, that.hostnameVerifier); } return false; } diff --git a/src/main/java/com/squareup/okhttp/ConnectionPool.java b/src/main/java/com/squareup/okhttp/ConnectionPool.java index ffb938de8..2d8fa5978 100644 --- a/src/main/java/com/squareup/okhttp/ConnectionPool.java +++ b/src/main/java/com/squareup/okhttp/ConnectionPool.java @@ -16,8 +16,8 @@ */ package com.squareup.okhttp; +import com.squareup.okhttp.internal.Platform; import com.squareup.okhttp.internal.io.IoUtils; -import com.squareup.okhttp.internal.util.Libcore; import java.net.SocketException; import java.util.ArrayList; import java.util.HashMap; @@ -87,14 +87,19 @@ public final class ConnectionPool { connectionPool.remove(address); connections = null; } - if (connection.isEligibleForRecycling()) { - // Since Socket is recycled, re-tag before using - // TODO: don't tag SPDY connections - Libcore.tagSocket(connection.getSocket()); - return connection; - } else { - // TODO: is the connection leaked here? + if (!connection.isEligibleForRecycling()) { + IoUtils.closeQuietly(connection); + continue; } + try { + Platform.get().tagSocket(connection.getSocket()); + } catch (SocketException e) { + // When unable to tag, skip recycling and close + Platform.get().logW("Unable to tagSocket(): " + e); + IoUtils.closeQuietly(connection); + continue; + } + return connection; } } return null; @@ -112,10 +117,10 @@ public final class ConnectionPool { } try { - Libcore.untagSocket(connection.getSocket()); + Platform.get().untagSocket(connection.getSocket()); } catch (SocketException e) { // When unable to remove tagging, skip recycling and close - Libcore.logW("Unable to untagSocket(): " + e); + Platform.get().logW("Unable to untagSocket(): " + e); IoUtils.closeQuietly(connection); return; } diff --git a/src/main/java/com/squareup/okhttp/TunnelRequest.java b/src/main/java/com/squareup/okhttp/TunnelRequest.java index 995f2b387..254e836ba 100644 --- a/src/main/java/com/squareup/okhttp/TunnelRequest.java +++ b/src/main/java/com/squareup/okhttp/TunnelRequest.java @@ -15,8 +15,8 @@ */ package com.squareup.okhttp; +import static com.squareup.okhttp.internal.Util.getDefaultPort; import com.squareup.okhttp.internal.net.http.RawHeaders; -import com.squareup.okhttp.internal.util.Libcore; /** * Routing and authentication information sent to an HTTP proxy to create a @@ -58,7 +58,7 @@ public final class TunnelRequest { result.setRequestLine("CONNECT " + host + ":" + port + " HTTP/1.1"); // Always set Host and User-Agent. - result.set("Host", port == Libcore.getDefaultPort("https") ? host : (host + ":" + port)); + result.set("Host", port == getDefaultPort("https") ? host : (host + ":" + port)); result.set("User-Agent", userAgent); // Copy over the Proxy-Authorization header if it exists. diff --git a/src/main/java/com/squareup/okhttp/internal/Platform.java b/src/main/java/com/squareup/okhttp/internal/Platform.java index db089cf54..6c5970ef3 100644 --- a/src/main/java/com/squareup/okhttp/internal/Platform.java +++ b/src/main/java/com/squareup/okhttp/internal/Platform.java @@ -24,6 +24,11 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.net.Socket; +import java.net.SocketException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -50,6 +55,20 @@ public class Platform { return PLATFORM; } + public void logW(String warning) { + System.out.println(warning); + } + + public void tagSocket(Socket socket) throws SocketException { + } + + public void untagSocket(Socket socket) throws SocketException { + } + + public URI toUriLenient(URL url) throws URISyntaxException { + return url.toURI(); // this isn't as good as the built-in toUriLenient + } + public void makeTlsTolerant(SSLSocket socket, String uriHost, boolean tlsTolerant) { if (!tlsTolerant) { socket.setEnabledProtocols(new String[]{"SSLv3"}); diff --git a/src/main/java/com/squareup/okhttp/internal/util/Libcore.java b/src/main/java/com/squareup/okhttp/internal/Util.java similarity index 70% rename from src/main/java/com/squareup/okhttp/internal/util/Libcore.java rename to src/main/java/com/squareup/okhttp/internal/Util.java index f3a92ba5d..9c4f0e874 100644 --- a/src/main/java/com/squareup/okhttp/internal/util/Libcore.java +++ b/src/main/java/com/squareup/okhttp/internal/Util.java @@ -14,34 +14,29 @@ * limitations under the License. */ -package com.squareup.okhttp.internal.util; +package com.squareup.okhttp.internal; -import java.io.File; -import java.io.IOException; -import java.net.Socket; -import java.net.SocketException; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.nio.ByteOrder; +import java.nio.charset.Charset; /** - * APIs for interacting with Android's core library. This mostly emulates the - * Android core library for interoperability with other runtimes. + * Junk drawer of utility methods. */ -public final class Libcore { +public final class Util { + public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; - private Libcore() { - } + /** A cheap and type-safe constant for the ISO-8859-1 Charset. */ + public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); - public static void deleteIfExists(File file) throws IOException { - // okhttp-changed: was Libcore.os.remove() in a try/catch block - file.delete(); - } + /** A cheap and type-safe constant for the US-ASCII Charset. */ + public static final Charset US_ASCII = Charset.forName("US-ASCII"); - public static void logW(String warning) { - // okhttp-changed: was System.logw() - System.out.println(warning); + /** A cheap and type-safe constant for the UTF-8 Charset. */ + public static final Charset UTF_8 = Charset.forName("UTF-8"); + + private Util() { } public static int getEffectivePort(URI uri) { @@ -74,16 +69,6 @@ public final class Libcore { } } - public static void tagSocket(Socket socket) { - } - - public static void untagSocket(Socket socket) throws SocketException { - } - - public static URI toUriLenient(URL url) throws URISyntaxException { - return url.toURI(); // this isn't as good as the built-in toUriLenient - } - public static void pokeInt(byte[] dst, int offset, int value, ByteOrder order) { if (order == ByteOrder.BIG_ENDIAN) { dst[offset++] = (byte) ((value >> 24) & 0xff); @@ -97,4 +82,11 @@ public final class Libcore { dst[offset ] = (byte) ((value >> 24) & 0xff); } } + + /** + * Returns true if two possibly-null objects are equal. + */ + public static boolean equal(Object a, Object b) { + return a == b || (a != null && a.equals(b)); + } } diff --git a/src/main/java/com/squareup/okhttp/internal/io/Base64.java b/src/main/java/com/squareup/okhttp/internal/io/Base64.java index e395052e6..16ced25bf 100644 --- a/src/main/java/com/squareup/okhttp/internal/io/Base64.java +++ b/src/main/java/com/squareup/okhttp/internal/io/Base64.java @@ -21,7 +21,7 @@ package com.squareup.okhttp.internal.io; -import com.squareup.okhttp.internal.util.EmptyArray; +import static com.squareup.okhttp.internal.Util.EMPTY_BYTE_ARRAY; import java.io.UnsupportedEncodingException; /** @@ -41,7 +41,7 @@ public final class Base64 { int length = len / 4 * 3; // return an empty array on empty or short input without padding if (length == 0) { - return EmptyArray.BYTE; + return EMPTY_BYTE_ARRAY; } // temporary array byte[] out = new byte[length]; diff --git a/src/main/java/com/squareup/okhttp/internal/io/DiskLruCache.java b/src/main/java/com/squareup/okhttp/internal/io/DiskLruCache.java index 2006257cc..0a029a8e3 100644 --- a/src/main/java/com/squareup/okhttp/internal/io/DiskLruCache.java +++ b/src/main/java/com/squareup/okhttp/internal/io/DiskLruCache.java @@ -16,8 +16,9 @@ package com.squareup.okhttp.internal.io; -import com.squareup.okhttp.internal.util.Charsets; -import com.squareup.okhttp.internal.util.Libcore; +import com.squareup.okhttp.internal.Platform; +import com.squareup.okhttp.internal.Util; +import static com.squareup.okhttp.internal.Util.UTF_8; import java.io.BufferedWriter; import java.io.Closeable; import java.io.EOFException; @@ -213,7 +214,7 @@ public final class DiskLruCache implements Closeable { cache.journalWriter = new BufferedWriter(new FileWriter(cache.journalFile, true)); return cache; } catch (IOException journalIsCorrupt) { - Libcore.logW("DiskLruCache " + directory + " is corrupt: " + Platform.get().logW("DiskLruCache " + directory + " is corrupt: " + journalIsCorrupt.getMessage() + ", removing"); cache.delete(); } @@ -228,7 +229,7 @@ public final class DiskLruCache implements Closeable { private void readJournal() throws IOException { StrictLineReader reader = new StrictLineReader(new FileInputStream(journalFile), - Charsets.US_ASCII); + Util.US_ASCII); try { String magic = reader.readLine(); String version = reader.readLine(); @@ -344,7 +345,7 @@ public final class DiskLruCache implements Closeable { } private static void deleteIfExists(File file) throws IOException { - Libcore.deleteIfExists(file); + file.delete(); } /** @@ -460,7 +461,7 @@ public final class DiskLruCache implements Closeable { } if (!entry.getDirtyFile(i).exists()) { editor.abort(); - Libcore.logW( + Platform.get().logW( "DiskLruCache: Newly created entry doesn't have file for index " + i); return; } @@ -609,7 +610,7 @@ public final class DiskLruCache implements Closeable { } private static String inputStreamToString(InputStream in) throws IOException { - return Streams.readFully(new InputStreamReader(in, Charsets.UTF_8)); + return Streams.readFully(new InputStreamReader(in, UTF_8)); } /** @@ -719,7 +720,7 @@ public final class DiskLruCache implements Closeable { public void set(int index, String value) throws IOException { Writer writer = null; try { - writer = new OutputStreamWriter(newOutputStream(index), Charsets.UTF_8); + writer = new OutputStreamWriter(newOutputStream(index), UTF_8); writer.write(value); } finally { IoUtils.closeQuietly(writer); diff --git a/src/main/java/com/squareup/okhttp/internal/io/Streams.java b/src/main/java/com/squareup/okhttp/internal/io/Streams.java index cf1465e6e..4352d9642 100644 --- a/src/main/java/com/squareup/okhttp/internal/io/Streams.java +++ b/src/main/java/com/squareup/okhttp/internal/io/Streams.java @@ -16,7 +16,7 @@ package com.squareup.okhttp.internal.io; -import com.squareup.okhttp.internal.util.Libcore; +import static com.squareup.okhttp.internal.Util.checkOffsetAndCount; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; @@ -76,7 +76,7 @@ public final class Streams { if (dst == null) { throw new NullPointerException("dst == null"); } - Libcore.checkOffsetAndCount(dst.length, offset, byteCount); + checkOffsetAndCount(dst.length, offset, byteCount); while (byteCount > 0) { int bytesRead = in.read(dst, offset, byteCount); if (bytesRead < 0) { diff --git a/src/main/java/com/squareup/okhttp/internal/io/StrictLineReader.java b/src/main/java/com/squareup/okhttp/internal/io/StrictLineReader.java index c9b290a90..dccf16f0b 100644 --- a/src/main/java/com/squareup/okhttp/internal/io/StrictLineReader.java +++ b/src/main/java/com/squareup/okhttp/internal/io/StrictLineReader.java @@ -16,7 +16,9 @@ package com.squareup.okhttp.internal.io; -import com.squareup.okhttp.internal.util.Charsets; +import static com.squareup.okhttp.internal.Util.ISO_8859_1; +import static com.squareup.okhttp.internal.Util.US_ASCII; +import static com.squareup.okhttp.internal.Util.UTF_8; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.EOFException; @@ -78,7 +80,7 @@ public class StrictLineReader implements Closeable { * @throws IllegalArgumentException for negative or zero {@code capacity}. */ public StrictLineReader(InputStream in, int capacity) { - this(in, capacity, Charsets.US_ASCII); + this(in, capacity, US_ASCII); } /** @@ -112,8 +114,7 @@ public class StrictLineReader implements Closeable { if (capacity < 0) { throw new IllegalArgumentException("capacity <= 0"); } - if (!(charset.equals(Charsets.US_ASCII) || charset.equals(Charsets.UTF_8) - || charset.equals(Charsets.ISO_8859_1))) { + if (!(charset.equals(US_ASCII) || charset.equals(UTF_8) || charset.equals(ISO_8859_1))) { throw new IllegalArgumentException("Unsupported encoding"); } diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/HttpEngine.java b/src/main/java/com/squareup/okhttp/internal/net/http/HttpEngine.java index 3918fbf3f..a58ea2313 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/HttpEngine.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/HttpEngine.java @@ -22,10 +22,12 @@ import com.squareup.okhttp.Connection; import com.squareup.okhttp.OkResponseCache; import com.squareup.okhttp.ResponseSource; import com.squareup.okhttp.TunnelRequest; +import com.squareup.okhttp.internal.Platform; +import static com.squareup.okhttp.internal.Util.EMPTY_BYTE_ARRAY; +import static com.squareup.okhttp.internal.Util.getDefaultPort; +import static com.squareup.okhttp.internal.Util.getEffectivePort; import com.squareup.okhttp.internal.io.IoUtils; import com.squareup.okhttp.internal.net.Dns; -import com.squareup.okhttp.internal.util.EmptyArray; -import com.squareup.okhttp.internal.util.Libcore; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -79,19 +81,9 @@ public class HttpEngine { return result; } @Override public InputStream getBody() throws IOException { - return new ByteArrayInputStream(EmptyArray.BYTE); + return new ByteArrayInputStream(EMPTY_BYTE_ARRAY); } }; - public static final int DEFAULT_CHUNK_LENGTH = 1024; - - public static final String OPTIONS = "OPTIONS"; - public static final String GET = "GET"; - public static final String HEAD = "HEAD"; - public static final String POST = "POST"; - public static final String PUT = "PUT"; - public static final String DELETE = "DELETE"; - public static final String TRACE = "TRACE"; - public static final int HTTP_CONTINUE = 100; protected final HttpURLConnectionImpl policy; @@ -162,7 +154,7 @@ public class HttpEngine { this.requestBodyOut = requestBodyOut; try { - uri = Libcore.toUriLenient(policy.getURL()); + uri = Platform.get().toUriLenient(policy.getURL()); } catch (URISyntaxException e) { throw new IOException(e); } @@ -286,7 +278,7 @@ public class HttpEngine { if (uriHost == null) { throw new UnknownHostException(uri.toString()); } - Address address = new Address(uriHost, Libcore.getEffectivePort(uri), + Address address = new Address(uriHost, getEffectivePort(uri), getSslSocketFactory(), getHostnameVerifier(), policy.getProxy()); routeSelector = new RouteSelector( address, uri, policy.proxySelector, policy.connectionPool, Dns.DEFAULT); @@ -330,7 +322,7 @@ public class HttpEngine { } boolean hasRequestBody() { - return method == POST || method == PUT; + return method.equals("POST") || method.equals("PUT"); } /** @@ -472,7 +464,7 @@ public class HttpEngine { int responseCode = responseHeaders.getHeaders().getResponseCode(); // HEAD requests never yield a body regardless of the response headers. - if (method == HEAD) { + if (method.equals("HEAD")) { return false; } @@ -612,7 +604,7 @@ public class HttpEngine { public static String getOriginAddress(URL url) { int port = url.getPort(); String result = url.getHost(); - if (port > 0 && port != Libcore.getDefaultPort(url.getProtocol())) { + if (port > 0 && port != getDefaultPort(url.getProtocol())) { result = result + ":" + port; } return result; diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/HttpResponseCache.java b/src/main/java/com/squareup/okhttp/internal/net/http/HttpResponseCache.java index 25f6b1a3f..757dd65fb 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/HttpResponseCache.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/HttpResponseCache.java @@ -17,13 +17,13 @@ package com.squareup.okhttp.internal.net.http; import com.squareup.okhttp.OkResponseCache; +import com.squareup.okhttp.ResponseSource; +import static com.squareup.okhttp.internal.Util.US_ASCII; +import static com.squareup.okhttp.internal.Util.UTF_8; import com.squareup.okhttp.internal.io.Base64; import com.squareup.okhttp.internal.io.DiskLruCache; import com.squareup.okhttp.internal.io.IoUtils; import com.squareup.okhttp.internal.io.StrictLineReader; -import com.squareup.okhttp.internal.util.Charsets; -import com.squareup.okhttp.internal.util.IntegralToString; -import com.squareup.okhttp.ResponseSource; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.File; @@ -62,6 +62,10 @@ import javax.net.ssl.SSLPeerUnverifiedException; * this. */ public final class HttpResponseCache extends ResponseCache implements OkResponseCache { + private static final char[] DIGITS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + // TODO: add APIs to iterate the cache? private static final int VERSION = 201105; private static final int ENTRY_METADATA = 0; @@ -85,7 +89,7 @@ public final class HttpResponseCache extends ResponseCache implements OkResponse try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] md5bytes = messageDigest.digest(uri.toString().getBytes("UTF-8")); - return IntegralToString.bytesToHexString(md5bytes, false); + return bytesToHexString(md5bytes); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } catch (UnsupportedEncodingException e) { @@ -93,6 +97,17 @@ public final class HttpResponseCache extends ResponseCache implements OkResponse } } + private static String bytesToHexString(byte[] bytes) { + char[] digits = DIGITS; + char[] buf = new char[bytes.length * 2]; + int c = 0; + for (byte b : bytes) { + buf[c++] = digits[(b >> 4) & 0xf]; + buf[c++] = digits[b & 0xf]; + } + return new String(buf); + } + @Override public CacheResponse get(URI uri, String requestMethod, Map> requestHeaders) { String key = uriToKey(uri); @@ -128,16 +143,16 @@ public final class HttpResponseCache extends ResponseCache implements OkResponse String requestMethod = httpConnection.getRequestMethod(); String key = uriToKey(uri); - if (requestMethod.equals(HttpEngine.POST) - || requestMethod.equals(HttpEngine.PUT) - || requestMethod.equals(HttpEngine.DELETE)) { + if (requestMethod.equals("POST") + || requestMethod.equals("PUT") + || requestMethod.equals("DELETE")) { try { cache.remove(key); } catch (IOException ignored) { // The cache cannot be written. } return null; - } else if (!requestMethod.equals(HttpEngine.GET)) { + } else if (!requestMethod.equals("GET")) { /* * Don't cache non-GET responses. We're technically allowed to cache * HEAD requests and some POST requests, but the complexity of doing @@ -373,7 +388,7 @@ public final class HttpResponseCache extends ResponseCache implements OkResponse */ public Entry(InputStream in) throws IOException { try { - StrictLineReader reader = new StrictLineReader(in, Charsets.US_ASCII); + StrictLineReader reader = new StrictLineReader(in, US_ASCII); uri = reader.readLine(); requestMethod = reader.readLine(); varyHeaders = new RawHeaders(); @@ -433,7 +448,7 @@ public final class HttpResponseCache extends ResponseCache implements OkResponse public void writeTo(DiskLruCache.Editor editor) throws IOException { OutputStream out = editor.newOutputStream(ENTRY_METADATA); - Writer writer = new BufferedWriter(new OutputStreamWriter(out, Charsets.UTF_8)); + Writer writer = new BufferedWriter(new OutputStreamWriter(out, UTF_8)); writer.write(uri + '\n'); writer.write(requestMethod + '\n'); diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/HttpTransport.java b/src/main/java/com/squareup/okhttp/internal/net/http/HttpTransport.java index 289958cd5..e47cd78f8 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/HttpTransport.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/HttpTransport.java @@ -17,8 +17,8 @@ package com.squareup.okhttp.internal.net.http; import com.squareup.okhttp.Connection; +import static com.squareup.okhttp.internal.Util.checkOffsetAndCount; import com.squareup.okhttp.internal.io.Streams; -import com.squareup.okhttp.internal.util.Libcore; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -45,6 +45,8 @@ public final class HttpTransport implements Transport { */ private static final int DISCARD_STREAM_TIMEOUT_MILLIS = 30; + public static final int DEFAULT_CHUNK_LENGTH = 1024; + private final HttpEngine httpEngine; private final InputStream socketIn; private final OutputStream socketOut; @@ -78,7 +80,7 @@ public final class HttpTransport implements Transport { if (chunked) { int chunkLength = httpEngine.policy.getChunkLength(); if (chunkLength == -1) { - chunkLength = HttpEngine.DEFAULT_CHUNK_LENGTH; + chunkLength = DEFAULT_CHUNK_LENGTH; } writeRequestHeaders(); return new ChunkedOutputStream(requestOut, chunkLength); @@ -244,7 +246,7 @@ public final class HttpTransport implements Transport { @Override public void write(byte[] buffer, int offset, int count) throws IOException { checkNotClosed(); - Libcore.checkOffsetAndCount(buffer.length, offset, count); + checkOffsetAndCount(buffer.length, offset, count); if (count > bytesRemaining) { throw new ProtocolException("expected " + bytesRemaining + " bytes but received " + count); @@ -312,7 +314,7 @@ public final class HttpTransport implements Transport { @Override public synchronized void write(byte[] buffer, int offset, int count) throws IOException { checkNotClosed(); - Libcore.checkOffsetAndCount(buffer.length, offset, count); + checkOffsetAndCount(buffer.length, offset, count); while (count > 0) { int numBytesWritten; @@ -397,7 +399,7 @@ public final class HttpTransport implements Transport { } @Override public int read(byte[] buffer, int offset, int count) throws IOException { - Libcore.checkOffsetAndCount(buffer.length, offset, count); + checkOffsetAndCount(buffer.length, offset, count); checkNotClosed(); if (bytesRemaining == 0) { return -1; @@ -447,7 +449,7 @@ public final class HttpTransport implements Transport { } @Override public int read(byte[] buffer, int offset, int count) throws IOException { - Libcore.checkOffsetAndCount(buffer.length, offset, count); + checkOffsetAndCount(buffer.length, offset, count); checkNotClosed(); if (!hasMoreChunks) { @@ -524,7 +526,7 @@ public final class HttpTransport implements Transport { } @Override public int read(byte[] buffer, int offset, int count) throws IOException { - Libcore.checkOffsetAndCount(buffer.length, offset, count); + checkOffsetAndCount(buffer.length, offset, count); checkNotClosed(); if (in == null || inputExhausted) { return -1; diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/HttpURLConnectionImpl.java b/src/main/java/com/squareup/okhttp/internal/net/http/HttpURLConnectionImpl.java index 2b4e88aac..6470e2e6e 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/HttpURLConnectionImpl.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/HttpURLConnectionImpl.java @@ -19,8 +19,8 @@ package com.squareup.okhttp.internal.net.http; import com.squareup.okhttp.Connection; import com.squareup.okhttp.ConnectionPool; +import static com.squareup.okhttp.internal.Util.getEffectivePort; import com.squareup.okhttp.internal.io.IoUtils; -import com.squareup.okhttp.internal.util.Libcore; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -254,10 +254,10 @@ public class HttpURLConnectionImpl extends HttpURLConnection { connected = true; try { if (doOutput) { - if (method == HttpEngine.GET) { + if (method.equals("GET")) { // they are requesting a stream to write to. This implies a POST method - method = HttpEngine.POST; - } else if (method != HttpEngine.POST && method != HttpEngine.PUT) { + method = "POST"; + } else if (!method.equals("POST") && !method.equals("PUT")) { // If the request method is neither POST nor PUT, then you're not writing throw new ProtocolException(method + " does not support writing"); } @@ -315,7 +315,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection { int responseCode = getResponseCode(); if (responseCode == HTTP_MULT_CHOICE || responseCode == HTTP_MOVED_PERM || responseCode == HTTP_MOVED_TEMP || responseCode == HTTP_SEE_OTHER) { - retryMethod = HttpEngine.GET; + retryMethod = "GET"; requestBody = null; } @@ -427,7 +427,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection { return Retry.NONE; // the scheme changed; don't retry. } if (previousUrl.getHost().equals(url.getHost()) - && Libcore.getEffectivePort(previousUrl) == Libcore.getEffectivePort(url)) { + && getEffectivePort(previousUrl) == getEffectivePort(url)) { return Retry.SAME_CONNECTION; } else { return Retry.DIFFERENT_CONNECTION; diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/HttpsURLConnectionImpl.java b/src/main/java/com/squareup/okhttp/internal/net/http/HttpsURLConnectionImpl.java index 8790b5c49..85602ab95 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/HttpsURLConnectionImpl.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/HttpsURLConnectionImpl.java @@ -19,7 +19,7 @@ package com.squareup.okhttp.internal.net.http; import com.squareup.okhttp.Connection; import com.squareup.okhttp.ConnectionPool; import com.squareup.okhttp.TunnelRequest; -import com.squareup.okhttp.internal.util.Libcore; +import static com.squareup.okhttp.internal.Util.getEffectivePort; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -453,7 +453,7 @@ public final class HttpsURLConnectionImpl extends HttpsURLConnection { } URL url = policy.getURL(); - return new TunnelRequest(url.getHost(), Libcore.getEffectivePort(url), userAgent, + return new TunnelRequest(url.getHost(), getEffectivePort(url), userAgent, requestHeaders.getProxyAuthorization()); } } diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/RawHeaders.java b/src/main/java/com/squareup/okhttp/internal/net/http/RawHeaders.java index 70a7adbac..0fa78ed10 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/RawHeaders.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/RawHeaders.java @@ -17,8 +17,8 @@ package com.squareup.okhttp.internal.net.http; +import com.squareup.okhttp.internal.Platform; import com.squareup.okhttp.internal.io.Streams; -import com.squareup.okhttp.internal.util.Libcore; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -203,7 +203,8 @@ public final class RawHeaders { * "Accept\r\n". For platform compatibility and HTTP compliance, we * print a warning and ignore null values. */ - Libcore.logW("Ignoring HTTP header field '" + fieldName + "' because its value is null"); + Platform.get().logW("Ignoring HTTP header field '" + + fieldName + "' because its value is null"); return; } namesAndValues.add(fieldName); diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/ResponseHeaders.java b/src/main/java/com/squareup/okhttp/internal/net/http/ResponseHeaders.java index 960dbfa70..2061960d7 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/ResponseHeaders.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/ResponseHeaders.java @@ -16,8 +16,8 @@ package com.squareup.okhttp.internal.net.http; -import com.squareup.okhttp.internal.util.Objects; import com.squareup.okhttp.ResponseSource; +import static com.squareup.okhttp.internal.Util.equal; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URI; @@ -368,7 +368,7 @@ public final class ResponseHeaders { public boolean varyMatches(Map> cachedRequest, Map> newRequest) { for (String field : varyFields) { - if (!Objects.equal(cachedRequest.get(field), newRequest.get(field))) { + if (!equal(cachedRequest.get(field), newRequest.get(field))) { return false; } } diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/RetryableOutputStream.java b/src/main/java/com/squareup/okhttp/internal/net/http/RetryableOutputStream.java index 7c8074570..5cca27a52 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/RetryableOutputStream.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/RetryableOutputStream.java @@ -16,7 +16,7 @@ package com.squareup.okhttp.internal.net.http; -import com.squareup.okhttp.internal.util.Libcore; +import static com.squareup.okhttp.internal.Util.checkOffsetAndCount; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -55,7 +55,7 @@ final class RetryableOutputStream extends AbstractHttpOutputStream { @Override public synchronized void write(byte[] buffer, int offset, int count) throws IOException { checkNotClosed(); - Libcore.checkOffsetAndCount(buffer.length, offset, count); + checkOffsetAndCount(buffer.length, offset, count); if (limit != -1 && content.size() > limit - count) { throw new ProtocolException("exceeded content-length limit of " + limit + " bytes"); } diff --git a/src/main/java/com/squareup/okhttp/internal/net/http/RouteSelector.java b/src/main/java/com/squareup/okhttp/internal/net/http/RouteSelector.java index 3e19fe744..1d6571c5c 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/http/RouteSelector.java +++ b/src/main/java/com/squareup/okhttp/internal/net/http/RouteSelector.java @@ -16,10 +16,10 @@ package com.squareup.okhttp.internal.net.http; import com.squareup.okhttp.Address; -import com.squareup.okhttp.ConnectionPool; import com.squareup.okhttp.Connection; +import com.squareup.okhttp.ConnectionPool; +import static com.squareup.okhttp.internal.Util.getEffectivePort; import com.squareup.okhttp.internal.net.Dns; -import com.squareup.okhttp.internal.util.Libcore; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -186,7 +186,7 @@ public final class RouteSelector { if (proxy.type() == Proxy.Type.DIRECT) { socketHost = uri.getHost(); - socketPort = Libcore.getEffectivePort(uri); + socketPort = getEffectivePort(uri); } else { SocketAddress proxyAddress = proxy.address(); if (!(proxyAddress instanceof InetSocketAddress)) { diff --git a/src/main/java/com/squareup/okhttp/internal/net/spdy/SpdyStream.java b/src/main/java/com/squareup/okhttp/internal/net/spdy/SpdyStream.java index 725509f5b..a0e5f3f1c 100644 --- a/src/main/java/com/squareup/okhttp/internal/net/spdy/SpdyStream.java +++ b/src/main/java/com/squareup/okhttp/internal/net/spdy/SpdyStream.java @@ -16,8 +16,9 @@ package com.squareup.okhttp.internal.net.spdy; +import static com.squareup.okhttp.internal.Util.checkOffsetAndCount; +import static com.squareup.okhttp.internal.Util.pokeInt; import com.squareup.okhttp.internal.io.Streams; -import com.squareup.okhttp.internal.util.Libcore; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; @@ -272,7 +273,7 @@ public final class SpdyStream { @Override public int read(byte[] b, int offset, int count) throws IOException { synchronized (SpdyStream.this) { checkNotClosed(); - Libcore.checkOffsetAndCount(b.length, offset, count); + checkOffsetAndCount(b.length, offset, count); while (pos == -1 && !finished) { try { @@ -399,7 +400,7 @@ public final class SpdyStream { } @Override public void write(byte[] bytes, int offset, int count) throws IOException { - Libcore.checkOffsetAndCount(bytes.length, offset, count); + checkOffsetAndCount(bytes.length, offset, count); checkNotClosed(); while (count > 0) { @@ -440,8 +441,8 @@ public final class SpdyStream { flags |= SpdyConnection.FLAG_FIN; } int length = pos - DATA_FRAME_HEADER_LENGTH; - Libcore.pokeInt(buffer, 0, id & 0x7fffffff, BIG_ENDIAN); - Libcore.pokeInt(buffer, 4, (flags & 0xff) << 24 | length & 0xffffff, BIG_ENDIAN); + pokeInt(buffer, 0, id & 0x7fffffff, BIG_ENDIAN); + pokeInt(buffer, 4, (flags & 0xff) << 24 | length & 0xffffff, BIG_ENDIAN); connection.writeFrame(buffer, 0, pos); pos = DATA_FRAME_HEADER_LENGTH; } diff --git a/src/main/java/com/squareup/okhttp/internal/util/Charsets.java b/src/main/java/com/squareup/okhttp/internal/util/Charsets.java deleted file mode 100644 index 38a619d8e..000000000 --- a/src/main/java/com/squareup/okhttp/internal/util/Charsets.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.squareup.okhttp.internal.util; - -import java.nio.charset.Charset; - -/** - * Provides convenient access to the most important built-in charsets. Saves a hash lookup and - * unnecessary handling of UnsupportedEncodingException at call sites, compared to using the - * charset's name. - */ -public final class Charsets { - /** - * A cheap and type-safe constant for the ISO-8859-1 Charset. - */ - public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); - - /** - * A cheap and type-safe constant for the US-ASCII Charset. - */ - public static final Charset US_ASCII = Charset.forName("US-ASCII"); - - /** - * A cheap and type-safe constant for the UTF-8 Charset. - */ - public static final Charset UTF_8 = Charset.forName("UTF-8"); - - private Charsets() { - } -} diff --git a/src/main/java/com/squareup/okhttp/internal/util/EmptyArray.java b/src/main/java/com/squareup/okhttp/internal/util/EmptyArray.java deleted file mode 100644 index fa24130f9..000000000 --- a/src/main/java/com/squareup/okhttp/internal/util/EmptyArray.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.squareup.okhttp.internal.util; - -public final class EmptyArray { - private EmptyArray() { - } - - public static final byte[] BYTE = new byte[0]; -} diff --git a/src/main/java/com/squareup/okhttp/internal/util/IntegralToString.java b/src/main/java/com/squareup/okhttp/internal/util/IntegralToString.java deleted file mode 100644 index fb24604e0..000000000 --- a/src/main/java/com/squareup/okhttp/internal/util/IntegralToString.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.squareup.okhttp.internal.util; - -/** - * Converts integral types to strings. This class is public but hidden so that it can also be - * used by java.util.Formatter to speed up %d. This class is in java.lang so that it can take - * advantage of the package-private String constructor. - * - * The most important methods are appendInt/appendLong and intToString(int)/longToString(int). - * The former are used in the implementation of StringBuilder, StringBuffer, and Formatter, while - * the latter are used by Integer.toString and Long.toString. - * - * The append methods take AbstractStringBuilder rather than Appendable because the latter requires - * CharSequences, while we only have raw char[]s. Since much of the savings come from not creating - * any garbage, we can't afford temporary CharSequence instances. - * - * One day the performance advantage of the binary/hex/octal specializations will be small enough - * that we can lose the duplication, but until then this class offers the full set. - */ -public final class IntegralToString { - /** - * The digits for every supported radix. - */ - private static final char[] DIGITS = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', - 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', - 'u', 'v', 'w', 'x', 'y', 'z' - }; - - private static final char[] UPPER_CASE_DIGITS = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', - 'U', 'V', 'W', 'X', 'Y', 'Z' - }; - - private IntegralToString() { - } - - public static String bytesToHexString(byte[] bytes, boolean upperCase) { - char[] digits = upperCase ? UPPER_CASE_DIGITS : DIGITS; - char[] buf = new char[bytes.length * 2]; - int c = 0; - for (byte b : bytes) { - buf[c++] = digits[(b >> 4) & 0xf]; - buf[c++] = digits[b & 0xf]; - } - return new String(buf); - } -} diff --git a/src/main/java/com/squareup/okhttp/internal/util/Objects.java b/src/main/java/com/squareup/okhttp/internal/util/Objects.java deleted file mode 100644 index 96da7a637..000000000 --- a/src/main/java/com/squareup/okhttp/internal/util/Objects.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.squareup.okhttp.internal.util; - -public final class Objects { - private Objects() { - } - - /** - * Returns true if two possibly-null objects are equal. - */ - public static boolean equal(Object a, Object b) { - return a == b || (a != null && a.equals(b)); - } - - public static int hashCode(Object o) { - return (o == null) ? 0 : o.hashCode(); - } -} diff --git a/src/test/java/com/squareup/okhttp/internal/io/StrictLineReaderTest.java b/src/test/java/com/squareup/okhttp/internal/io/StrictLineReaderTest.java index b7e2b2015..2241d1ae1 100644 --- a/src/test/java/com/squareup/okhttp/internal/io/StrictLineReaderTest.java +++ b/src/test/java/com/squareup/okhttp/internal/io/StrictLineReaderTest.java @@ -16,7 +16,7 @@ package com.squareup.okhttp.internal.io; -import com.squareup.okhttp.internal.util.Charsets; +import static com.squareup.okhttp.internal.Util.US_ASCII; import java.io.ByteArrayInputStream; import java.io.EOFException; import java.io.IOException; @@ -29,7 +29,7 @@ public class StrictLineReaderTest extends TestCase { try { // Testing with LineReader buffer capacity 32 to check some corner cases. StrictLineReader lineReader = new StrictLineReader(createTestInputStream(), 32, - Charsets.US_ASCII); + US_ASCII); InputStream refStream = createTestInputStream(); while (true) { try { diff --git a/src/test/java/com/squareup/okhttp/internal/net/spdy/SpdyConnectionTest.java b/src/test/java/com/squareup/okhttp/internal/net/spdy/SpdyConnectionTest.java index 9d1b1d1e5..4c609b906 100644 --- a/src/test/java/com/squareup/okhttp/internal/net/spdy/SpdyConnectionTest.java +++ b/src/test/java/com/squareup/okhttp/internal/net/spdy/SpdyConnectionTest.java @@ -16,6 +16,7 @@ package com.squareup.okhttp.internal.net.spdy; +import static com.squareup.okhttp.internal.Util.UTF_8; import static com.squareup.okhttp.internal.net.spdy.Settings.PERSIST_VALUE; import static com.squareup.okhttp.internal.net.spdy.SpdyConnection.FLAG_FIN; import static com.squareup.okhttp.internal.net.spdy.SpdyConnection.TYPE_DATA; @@ -25,7 +26,6 @@ import static com.squareup.okhttp.internal.net.spdy.SpdyConnection.TYPE_RST_STRE import static com.squareup.okhttp.internal.net.spdy.SpdyConnection.TYPE_SYN_REPLY; import static com.squareup.okhttp.internal.net.spdy.SpdyConnection.TYPE_SYN_STREAM; import static com.squareup.okhttp.internal.net.spdy.SpdyStream.RST_INVALID_STREAM; -import static com.squareup.okhttp.internal.util.Charsets.UTF_8; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream;