From 78308c0c6ab52a8c333184bd2f0af933a81463bc Mon Sep 17 00:00:00 2001 From: yifeizhuang Date: Mon, 4 Apr 2022 17:37:28 -0700 Subject: [PATCH] okhttp: Fix okio 2.x API incompatibility (#9054) --- .../grpc/okhttp/internal/proxy/HttpUrl.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/proxy/HttpUrl.java b/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/proxy/HttpUrl.java index a24ae2f12c..1a03812f26 100644 --- a/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/proxy/HttpUrl.java +++ b/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/proxy/HttpUrl.java @@ -18,6 +18,7 @@ */ package io.grpc.okhttp.internal.proxy; +import java.io.EOFException; import java.net.IDN; import java.net.InetAddress; import java.net.UnknownHostException; @@ -433,8 +434,7 @@ public final class HttpUrl { } static void canonicalize(Buffer out, String input, int pos, int limit, - String encodeSet, boolean alreadyEncoded, boolean plusIsSpace, boolean asciiOnly) - throws Exception { + String encodeSet, boolean alreadyEncoded, boolean plusIsSpace, boolean asciiOnly) { Buffer utf8Buffer = null; // Lazily allocated. int codePoint; for (int i = pos; i < limit; i += Character.charCount(codePoint)) { @@ -456,10 +456,15 @@ public final class HttpUrl { } utf8Buffer.writeUtf8CodePoint(codePoint); while (!utf8Buffer.exhausted()) { - int b = utf8Buffer.readByte() & 0xff; - out.writeByte('%'); - out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]); - out.writeByte(HEX_DIGITS[b & 0xf]); + try { + fakeEofExceptionMethod(); // Okio 2.x can throw EOFException from readByte() + int b = utf8Buffer.readByte() & 0xff; + out.writeByte('%'); + out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]); + out.writeByte(HEX_DIGITS[b & 0xf]); + } catch (EOFException e) { + throw new IndexOutOfBoundsException(e.getMessage()); + } } } else { // This character doesn't need encoding. Just copy it over. @@ -467,4 +472,6 @@ public final class HttpUrl { } } } + + private static void fakeEofExceptionMethod() throws EOFException {} }