diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/okio/ByteString.java b/okhttp/src/main/java/com/squareup/okhttp/internal/okio/ByteString.java index 0721caeea..2397aabcb 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/okio/ByteString.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/okio/ByteString.java @@ -19,6 +19,7 @@ import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; @@ -58,16 +59,24 @@ public final class ByteString { /** Returns a new byte string containing the {@code UTF-8} bytes of {@code s}. */ public static ByteString encodeUtf8(String s) { - ByteString byteString = new ByteString(s.getBytes(Util.UTF_8)); - byteString.utf8 = s; - return byteString; + try { + ByteString byteString = new ByteString(s.getBytes(Util.UTF_8)); + byteString.utf8 = s; + return byteString; + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } /** Constructs a new {@code String} by decoding the bytes as {@code UTF-8}. */ public String utf8() { String result = utf8; // We don't care if we double-allocate in racy code. - return result != null ? result : (utf8 = new String(data, Util.UTF_8)); + try { + return result != null ? result : (utf8 = new String(data, Util.UTF_8)); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } /** diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/okio/InflaterSource.java b/okhttp/src/main/java/com/squareup/okhttp/internal/okio/InflaterSource.java index aa6f5ba5d..91dcb90af 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/okio/InflaterSource.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/okio/InflaterSource.java @@ -76,7 +76,9 @@ public final class InflaterSource implements Source { } if (sourceExhausted) throw new EOFException("source exhausted prematurely"); } catch (DataFormatException e) { - throw new IOException(e); + IOException io = new IOException(); + io.initCause(e); + throw io; } } } diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/okio/OkBuffer.java b/okhttp/src/main/java/com/squareup/okhttp/internal/okio/OkBuffer.java index 6e0b5ff6e..2b2949f6a 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/okio/OkBuffer.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/okio/OkBuffer.java @@ -18,6 +18,7 @@ package com.squareup.okhttp.internal.okio; import java.io.EOFException; import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -253,19 +254,27 @@ public final class OkBuffer implements BufferedSource, BufferedSink, Cloneable { Segment head = this.head; if (head.pos + byteCount > head.limit) { // If the string spans multiple segments, delegate to readBytes(). - return new String(readBytes(byteCount), UTF_8); + try { + return new String(readBytes(byteCount), UTF_8); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } - String result = new String(head.data, head.pos, (int) byteCount, UTF_8); - head.pos += byteCount; - this.size -= byteCount; + try { + String result = new String(head.data, head.pos, (int) byteCount, UTF_8); + head.pos += byteCount; + this.size -= byteCount; - if (head.pos == head.limit) { - this.head = head.pop(); - SegmentPool.INSTANCE.recycle(head); + if (head.pos == head.limit) { + this.head = head.pop(); + SegmentPool.INSTANCE.recycle(head); + } + + return result; + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); } - - return result; } @Override public String readUtf8Line(boolean throwOnEof) throws EOFException { @@ -369,8 +378,12 @@ public final class OkBuffer implements BufferedSource, BufferedSink, Cloneable { /** Encodes {@code string} as UTF-8 and appends the bytes to this. */ @Override public OkBuffer writeUtf8(String string) { - byte[] data = string.getBytes(UTF_8); - return write(data, 0, data.length); + try { + byte[] data = string.getBytes(UTF_8); + return write(data, 0, data.length); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } @Override public OkBuffer write(byte[] source) { diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/okio/Util.java b/okhttp/src/main/java/com/squareup/okhttp/internal/okio/Util.java index a626c343b..d3a60e9eb 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/okio/Util.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/okio/Util.java @@ -15,11 +15,10 @@ */ package com.squareup.okhttp.internal.okio; -import java.nio.charset.Charset; final class Util { /** A cheap and type-safe constant for the UTF-8 Charset. */ - public static final Charset UTF_8 = Charset.forName("UTF-8"); + public static final String UTF_8 = "UTF-8"; private Util() { }