From 4b0c09eb6f931c7ba9a5735c81af115a302ed336 Mon Sep 17 00:00:00 2001 From: Venil Noronha Date: Fri, 29 Apr 2016 00:19:13 +0530 Subject: [PATCH] Fixes #2522 - Format String using Locale.US. --- .../okhttp3/mockwebserver/MockWebServer.java | 2 +- .../mockwebserver/MockWebServerTest.java | 11 +++++----- okcurl/src/main/java/okhttp3/curl/Main.java | 3 ++- .../src/test/java/okhttp3/CallTest.java | 2 +- .../test/java/okhttp3/URLConnectionTest.java | 2 +- .../okhttp3/UrlComponentEncodingTester.java | 21 ++++++++++--------- .../java/okhttp3/WebPlatformUrlTestData.java | 3 ++- .../internal/http/RecordingProxySelector.java | 3 ++- .../internal/ws/WebSocketReaderTest.java | 3 ++- .../internal/ws/WebSocketWriterTest.java | 7 ++++--- .../main/java/okhttp3/ws/WebSocketCall.java | 2 +- okhttp/src/main/java/okhttp3/Headers.java | 5 +++-- .../java/okhttp3/internal/NamedRunnable.java | 2 +- .../src/main/java/okhttp3/internal/Util.java | 5 +++++ .../internal/framed/FramedConnection.java | 2 +- .../java/okhttp3/internal/framed/Header.java | 3 ++- .../java/okhttp3/internal/framed/Http2.java | 2 +- .../java/okhttp3/internal/framed/Spdy3.java | 2 +- 18 files changed, 47 insertions(+), 33 deletions(-) diff --git a/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.java b/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.java index 8ff4708d723a..bfddfa89e610 100644 --- a/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.java +++ b/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.java @@ -649,7 +649,7 @@ private void handleWebSocketUpgrade(Socket socket, BufferedSource source, Buffer ThreadPoolExecutor replyExecutor = new ThreadPoolExecutor(1, 1, 1, SECONDS, new LinkedBlockingDeque(), - Util.threadFactory(String.format("MockWebServer %s WebSocket", request.getPath()), + Util.threadFactory(Util.format("MockWebServer %s WebSocket", request.getPath()), true)); replyExecutor.allowCoreThreadTimeOut(true); final RealWebSocket webSocket = diff --git a/mockwebserver/src/test/java/okhttp3/mockwebserver/MockWebServerTest.java b/mockwebserver/src/test/java/okhttp3/mockwebserver/MockWebServerTest.java index 9a1b0daf8f96..b951635a8a28 100644 --- a/mockwebserver/src/test/java/okhttp3/mockwebserver/MockWebServerTest.java +++ b/mockwebserver/src/test/java/okhttp3/mockwebserver/MockWebServerTest.java @@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import okhttp3.Headers; +import okhttp3.internal.Util; import org.junit.After; import org.junit.Rule; import org.junit.Test; @@ -231,8 +232,8 @@ public final class MockWebServerTest { long elapsedNanos = System.nanoTime() - startNanos; long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos); - assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500); - assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000); + assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500); + assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000); } /** @@ -257,8 +258,8 @@ public final class MockWebServerTest { long elapsedNanos = System.nanoTime() - startNanos; long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos); - assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500); - assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000); + assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500); + assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000); } /** Delay the response body by sleeping 1s. */ @@ -273,7 +274,7 @@ public final class MockWebServerTest { assertEquals('A', in.read()); long elapsedNanos = System.nanoTime() - startNanos; long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos); - assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 1000); + assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 1000); in.close(); } diff --git a/okcurl/src/main/java/okhttp3/curl/Main.java b/okcurl/src/main/java/okhttp3/curl/Main.java index 5b2f371b0eba..15e4f030d198 100644 --- a/okcurl/src/main/java/okhttp3/curl/Main.java +++ b/okcurl/src/main/java/okhttp3/curl/Main.java @@ -45,6 +45,7 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import okhttp3.internal.Util; import okhttp3.internal.framed.Http2; import okhttp3.internal.http.StatusLine; import okio.BufferedSource; @@ -277,7 +278,7 @@ private static void enableHttp2FrameLogging() { handler.setLevel(Level.FINE); handler.setFormatter(new SimpleFormatter() { @Override public String format(LogRecord record) { - return String.format("%s%n", record.getMessage()); + return Util.format("%s%n", record.getMessage()); } }); frameLogger.addHandler(handler); diff --git a/okhttp-tests/src/test/java/okhttp3/CallTest.java b/okhttp-tests/src/test/java/okhttp3/CallTest.java index c097fb6bbee1..ac0c106a3951 100644 --- a/okhttp-tests/src/test/java/okhttp3/CallTest.java +++ b/okhttp-tests/src/test/java/okhttp3/CallTest.java @@ -734,7 +734,7 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc // Timed out as expected. long elapsedNanos = System.nanoTime() - startNanos; long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(elapsedNanos); - assertTrue(String.format("Timed out: %sms", elapsedMillis), elapsedMillis < 500); + assertTrue(Util.format("Timed out: %sms", elapsedMillis), elapsedMillis < 500); } finally { bodySource.close(); } diff --git a/okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java b/okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java index c50838c2fac9..f3c69dc51e71 100644 --- a/okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java +++ b/okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java @@ -1412,7 +1412,7 @@ private void testEarlyDisconnectDoesntHarmPooling(TransferKind transferKind) thr // If we're working correctly, this should be greater than 100ms, but less than double that. // Previously we had a bug where we would download the entire response body as long as no // individual read took longer than 100ms. - assertTrue(String.format("Time to close: %sms", elapsedMillis), elapsedMillis < 500); + assertTrue(Util.format("Time to close: %sms", elapsedMillis), elapsedMillis < 500); // Do another request to confirm that the discarded connection was not pooled. assertContent("A", urlFactory.open(server.url("/").url())); diff --git a/okhttp-tests/src/test/java/okhttp3/UrlComponentEncodingTester.java b/okhttp-tests/src/test/java/okhttp3/UrlComponentEncodingTester.java index 1645acc90718..56611be40a7b 100644 --- a/okhttp-tests/src/test/java/okhttp3/UrlComponentEncodingTester.java +++ b/okhttp-tests/src/test/java/okhttp3/UrlComponentEncodingTester.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; +import okhttp3.internal.Util; import okio.Buffer; import okio.ByteString; @@ -231,7 +232,7 @@ private void testParseAlreadyEncoded(int codePoint, Encoding encoding, Component String urlString = component.urlString(encoded); HttpUrl url = HttpUrl.parse(urlString); if (!component.encodedValue(url).equals(encoded)) { - fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding)); + fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding)); } } @@ -242,7 +243,7 @@ private void testEncodeAndDecode(int codePoint, Component component) { HttpUrl url = builder.build(); String actual = component.get(url); if (!expected.equals(actual)) { - fail(String.format("Roundtrip %s %#x %s", component, codePoint, url)); + fail(Util.format("Roundtrip %s %#x %s", component, codePoint, url)); } } @@ -255,7 +256,7 @@ private void testParseOriginal(int codePoint, Encoding encoding, Component compo String s = component.encodedValue(url); if (!s.equals(encoded)) { - fail(String.format("Encoding %s %#02x using %s", component, codePoint, encoding)); + fail(Util.format("Encoding %s %#02x using %s", component, codePoint, encoding)); } } @@ -264,7 +265,7 @@ private void testToUrl(int codePoint, Encoding encoding, Component component) { HttpUrl httpUrl = HttpUrl.parse(component.urlString(encoded)); URL javaNetUrl = httpUrl.url(); if (!javaNetUrl.toString().equals(javaNetUrl.toString())) { - fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding)); + fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding)); } } @@ -273,7 +274,7 @@ private void testFromUrl(int codePoint, Encoding encoding, Component component) HttpUrl httpUrl = HttpUrl.parse(component.urlString(encoded)); HttpUrl toAndFromJavaNetUrl = HttpUrl.get(httpUrl.url()); if (!toAndFromJavaNetUrl.equals(httpUrl)) { - fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding)); + fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding)); } } @@ -287,18 +288,18 @@ private void testUri( if (uriEscaped) { // The URI has more escaping than the HttpURL. Check that the decoded values still match. if (uri.toString().equals(httpUrl.toString())) { - fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding)); + fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding)); } if (!component.get(toAndFromUri).equals(string)) { - fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding)); + fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding)); } } else { // Check that the URI and HttpURL have the exact same escaping. if (!toAndFromUri.equals(httpUrl)) { - fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding)); + fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding)); } if (!uri.toString().equals(httpUrl.toString())) { - fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding)); + fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding)); } } } @@ -315,7 +316,7 @@ public String encode(int codePoint) { ByteString utf8 = ByteString.encodeUtf8(IDENTITY.encode(codePoint)); Buffer percentEncoded = new Buffer(); for (int i = 0; i < utf8.size(); i++) { - percentEncoded.writeUtf8(String.format("%%%02X", utf8.getByte(i) & 0xff)); + percentEncoded.writeUtf8(Util.format("%%%02X", utf8.getByte(i) & 0xff)); } return percentEncoded.readUtf8(); } diff --git a/okhttp-tests/src/test/java/okhttp3/WebPlatformUrlTestData.java b/okhttp-tests/src/test/java/okhttp3/WebPlatformUrlTestData.java index 881811139da7..72f92f9abf9a 100644 --- a/okhttp-tests/src/test/java/okhttp3/WebPlatformUrlTestData.java +++ b/okhttp-tests/src/test/java/okhttp3/WebPlatformUrlTestData.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import okhttp3.internal.Util; import okio.Buffer; import okio.BufferedSource; @@ -81,7 +82,7 @@ private void set(String name, String value) { } @Override public String toString() { - return String.format("Parsing: <%s> against <%s>", input, base); + return Util.format("Parsing: <%s> against <%s>", input, base); } public static List load(BufferedSource source) throws IOException { diff --git a/okhttp-tests/src/test/java/okhttp3/internal/http/RecordingProxySelector.java b/okhttp-tests/src/test/java/okhttp3/internal/http/RecordingProxySelector.java index 5b4b630d32b5..33a51500e0a1 100644 --- a/okhttp-tests/src/test/java/okhttp3/internal/http/RecordingProxySelector.java +++ b/okhttp-tests/src/test/java/okhttp3/internal/http/RecordingProxySelector.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import okhttp3.internal.Util; import static org.junit.Assert.assertEquals; @@ -45,7 +46,7 @@ public void assertRequests(URI... expectedUris) { @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { InetSocketAddress socketAddress = (InetSocketAddress) sa; failures.add( - String.format("%s %s:%d %s", uri, socketAddress.getHostName(), socketAddress.getPort(), + Util.format("%s %s:%d %s", uri, socketAddress.getHostName(), socketAddress.getPort(), ioe.getMessage())); } } diff --git a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java b/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java index 358b35990a77..6bb7ef86d83a 100644 --- a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java +++ b/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java @@ -22,6 +22,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Pattern; import okhttp3.ResponseBody; +import okhttp3.internal.Util; import okhttp3.ws.WebSocketRecorder; import okhttp3.ws.WebSocketRecorder.MessageDelegate; import okio.Buffer; @@ -385,7 +386,7 @@ public final class WebSocketReaderTest { data.write(ByteString.decodeHex("880203ed")); // Close with code 1005 data.write(ByteString.decodeHex("880203ee")); // Close with code 1006 for (int i = 1012; i <= 2999; i++) { - data.write(ByteString.decodeHex("8802" + String.format("%04X", i))); // Close with code 'i' + data.write(ByteString.decodeHex("8802" + Util.format("%04X", i))); // Close with code 'i' } int count = 0; diff --git a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java b/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java index 66475fb7b03f..bdeff21c1538 100644 --- a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java +++ b/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.Random; import okhttp3.RequestBody; +import okhttp3.internal.Util; import okio.Buffer; import okio.BufferedSink; import okio.ByteString; @@ -99,7 +100,7 @@ public final class WebSocketWriterTest { sink.close(); assertData("817e"); - assertData(String.format("%04x", length)); + assertData(Util.format("%04x", length)); assertData(bytes); assertTrue(data.exhausted()); } @@ -199,7 +200,7 @@ public final class WebSocketWriterTest { // Write directly to the unbuffered sink. This ensures it will become single frame. sink.write(payload.clone(), byteCount); assertData("027e"); // 'e' == 4-byte follow-up length. - assertData(String.format("%04X", payload.completeSegmentByteCount())); + assertData(Util.format("%04X", payload.completeSegmentByteCount())); assertData(payload.readByteArray()); sink.close(); @@ -219,7 +220,7 @@ public final class WebSocketWriterTest { // Write directly to the unbuffered sink. This ensures it will become single frame. sink.write(payload.clone(), byteCount); assertData("027f"); // 'f' == 16-byte follow-up length. - assertData(String.format("%016X", byteCount)); + assertData(Util.format("%016X", byteCount)); assertData(payload.readByteArray(byteCount)); sink.close(); diff --git a/okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java b/okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java index 8e5c5ce0559e..46f8181036e4 100644 --- a/okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java +++ b/okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java @@ -160,7 +160,7 @@ static RealWebSocket create(StreamAllocation streamAllocation, Response response String url = response.request().url().toString(); ThreadPoolExecutor replyExecutor = new ThreadPoolExecutor(1, 1, 1, SECONDS, new LinkedBlockingDeque(), - Util.threadFactory(String.format("OkHttp %s WebSocket", url), true)); + Util.threadFactory(Util.format("OkHttp %s WebSocket", url), true)); replyExecutor.allowCoreThreadTimeOut(true); return new StreamWebSocket(streamAllocation, random, replyExecutor, listener, url); diff --git a/okhttp/src/main/java/okhttp3/Headers.java b/okhttp/src/main/java/okhttp3/Headers.java index 992569a50100..840a272d3434 100644 --- a/okhttp/src/main/java/okhttp3/Headers.java +++ b/okhttp/src/main/java/okhttp3/Headers.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; +import okhttp3.internal.Util; import okhttp3.internal.http.HttpDate; /** @@ -271,7 +272,7 @@ private void checkNameAndValue(String name, String value) { for (int i = 0, length = name.length(); i < length; i++) { char c = name.charAt(i); if (c <= '\u001f' || c >= '\u007f') { - throw new IllegalArgumentException(String.format( + throw new IllegalArgumentException(Util.format( "Unexpected char %#04x at %d in header name: %s", (int) c, i, name)); } } @@ -279,7 +280,7 @@ private void checkNameAndValue(String name, String value) { for (int i = 0, length = value.length(); i < length; i++) { char c = value.charAt(i); if (c <= '\u001f' || c >= '\u007f') { - throw new IllegalArgumentException(String.format( + throw new IllegalArgumentException(Util.format( "Unexpected char %#04x at %d in %s value: %s", (int) c, i, name, value)); } } diff --git a/okhttp/src/main/java/okhttp3/internal/NamedRunnable.java b/okhttp/src/main/java/okhttp3/internal/NamedRunnable.java index b5a41036044f..ff9729e2af52 100644 --- a/okhttp/src/main/java/okhttp3/internal/NamedRunnable.java +++ b/okhttp/src/main/java/okhttp3/internal/NamedRunnable.java @@ -22,7 +22,7 @@ public abstract class NamedRunnable implements Runnable { protected final String name; public NamedRunnable(String format, Object... args) { - this.name = String.format(format, args); + this.name = Util.format(format, args); } @Override public final void run() { diff --git a/okhttp/src/main/java/okhttp3/internal/Util.java b/okhttp/src/main/java/okhttp3/internal/Util.java index 4dc139f4fb08..f2b1e70d46e3 100644 --- a/okhttp/src/main/java/okhttp3/internal/Util.java +++ b/okhttp/src/main/java/okhttp3/internal/Util.java @@ -446,4 +446,9 @@ private static boolean containsInvalidHostnameAsciiCodes(String hostnameAscii) { public static boolean verifyAsIpAddress(String host) { return VERIFY_AS_IP_ADDRESS.matcher(host).matches(); } + + /** Returns a {@link Locale#US} formatted {@link String}. */ + public static String format(String format, Object... args) { + return String.format(Locale.US, format, args); + } } diff --git a/okhttp/src/main/java/okhttp3/internal/framed/FramedConnection.java b/okhttp/src/main/java/okhttp3/internal/framed/FramedConnection.java index 4a6a8e7d0943..362d63ad393f 100644 --- a/okhttp/src/main/java/okhttp3/internal/framed/FramedConnection.java +++ b/okhttp/src/main/java/okhttp3/internal/framed/FramedConnection.java @@ -155,7 +155,7 @@ private FramedConnection(Builder builder) throws IOException { // Like newSingleThreadExecutor, except lazy creates the thread. pushExecutor = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), - Util.threadFactory(String.format("OkHttp %s Push Observer", hostname), true)); + Util.threadFactory(Util.format("OkHttp %s Push Observer", hostname), true)); // 1 less than SPDY http://tools.ietf.org/html/draft-ietf-httpbis-http2-17#section-6.9.2 peerSettings.set(Settings.INITIAL_WINDOW_SIZE, 0, 65535); peerSettings.set(Settings.MAX_FRAME_SIZE, 0, Http2.INITIAL_MAX_FRAME_SIZE); diff --git a/okhttp/src/main/java/okhttp3/internal/framed/Header.java b/okhttp/src/main/java/okhttp3/internal/framed/Header.java index 953671eb3281..866edc2591f0 100644 --- a/okhttp/src/main/java/okhttp3/internal/framed/Header.java +++ b/okhttp/src/main/java/okhttp3/internal/framed/Header.java @@ -15,6 +15,7 @@ */ package okhttp3.internal.framed; +import okhttp3.internal.Util; import okio.ByteString; /** HTTP header: the name is an ASCII string, but the value can be UTF-8. */ @@ -66,6 +67,6 @@ public Header(ByteString name, ByteString value) { } @Override public String toString() { - return String.format("%s: %s", name.utf8(), value.utf8()); + return Util.format("%s: %s", name.utf8(), value.utf8()); } } diff --git a/okhttp/src/main/java/okhttp3/internal/framed/Http2.java b/okhttp/src/main/java/okhttp3/internal/framed/Http2.java index a6c177cc608b..8ad6c5224e7f 100644 --- a/okhttp/src/main/java/okhttp3/internal/framed/Http2.java +++ b/okhttp/src/main/java/okhttp3/internal/framed/Http2.java @@ -26,8 +26,8 @@ import okio.Source; import okio.Timeout; -import static java.lang.String.format; import static java.util.logging.Level.FINE; +import static okhttp3.internal.Util.format; import static okhttp3.internal.framed.Http2.FrameLogger.formatHeader; import static okio.ByteString.EMPTY; diff --git a/okhttp/src/main/java/okhttp3/internal/framed/Spdy3.java b/okhttp/src/main/java/okhttp3/internal/framed/Spdy3.java index 72053e006638..37e1796c5c2a 100644 --- a/okhttp/src/main/java/okhttp3/internal/framed/Spdy3.java +++ b/okhttp/src/main/java/okhttp3/internal/framed/Spdy3.java @@ -275,7 +275,7 @@ private void readSettings(Handler handler, int flags, int length) throws IOExcep } private static IOException ioException(String message, Object... args) throws IOException { - throw new IOException(String.format(message, args)); + throw new IOException(Util.format(message, args)); } @Override public void close() throws IOException {