From dcb3c1443587cde3bc8b791a791eeb23e5ef7fe1 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Mon, 14 Dec 2015 23:45:19 -0500 Subject: [PATCH] Remove IOException from source() and streams. --- .../okhttp/internal/huc/JavaApiConverter.java | 29 ++++++++----------- .../internal/huc/JavaApiConverterTest.java | 3 +- .../internal/huc/ResponseCacheTest.java | 3 +- .../okhttp/internal/ws/WebSocketReader.java | 2 +- .../com/squareup/okhttp/ResponseBody.java | 6 ++-- .../com/squareup/okhttp/recipes/Progress.java | 2 +- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/okhttp-android-support/src/main/java/com/squareup/okhttp/internal/huc/JavaApiConverter.java b/okhttp-android-support/src/main/java/com/squareup/okhttp/internal/huc/JavaApiConverter.java index 4f02f9c0a..8701af033 100644 --- a/okhttp-android-support/src/main/java/com/squareup/okhttp/internal/huc/JavaApiConverter.java +++ b/okhttp-android-support/src/main/java/com/squareup/okhttp/internal/huc/JavaApiConverter.java @@ -474,10 +474,9 @@ public final class JavaApiConverter { * Creates an OkHttp Response.Body containing the supplied information. */ private static ResponseBody createOkBody(final Headers okHeaders, - final CacheResponse cacheResponse) { + final CacheResponse cacheResponse) throws IOException { + final BufferedSource body = Okio.buffer(Okio.source(cacheResponse.getBody())); return new ResponseBody() { - private BufferedSource body; - @Override public MediaType contentType() { String contentTypeHeader = okHeaders.get("Content-Type"); @@ -488,11 +487,7 @@ public final class JavaApiConverter { public long contentLength() { return OkHeaders.contentLength(okHeaders); } - @Override public BufferedSource source() throws IOException { - if (body == null) { - InputStream is = cacheResponse.getBody(); - body = Okio.buffer(Okio.source(is)); - } + @Override public BufferedSource source() { return body; } }; @@ -501,13 +496,13 @@ public final class JavaApiConverter { /** * Creates an OkHttp Response.Body containing the supplied information. */ - private static ResponseBody createOkBody(final URLConnection urlConnection) { + private static ResponseBody createOkBody(final URLConnection urlConnection) throws IOException { if (!urlConnection.getDoInput()) { return null; } - return new ResponseBody() { - private BufferedSource body; + final BufferedSource body = Okio.buffer(Okio.source(urlConnection.getInputStream())); + return new ResponseBody() { @Override public MediaType contentType() { String contentTypeHeader = urlConnection.getContentType(); return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader); @@ -516,11 +511,7 @@ public final class JavaApiConverter { String s = urlConnection.getHeaderField("Content-Length"); return stringToLong(s); } - @Override public BufferedSource source() throws IOException { - if (body == null) { - InputStream is = urlConnection.getInputStream(); - body = Okio.buffer(Okio.source(is)); - } + @Override public BufferedSource source() { return body; } }; @@ -727,7 +718,11 @@ public final class JavaApiConverter { @Override public InputStream getInputStream() throws IOException { - throw throwResponseBodyAccessException(); + return new InputStream() { + @Override public int read() throws IOException { + throw throwResponseBodyAccessException(); + } + }; } @Override diff --git a/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java b/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java index fae976462..20ee8d66b 100644 --- a/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java +++ b/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java @@ -319,8 +319,9 @@ public class JavaApiConverterTest { JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse); // Check an arbitrary (not complete) set of methods that can be used to access the response // body. + InputStream is = httpUrlConnection.getInputStream(); try { - httpUrlConnection.getInputStream(); + is.read(); fail(); } catch (UnsupportedOperationException expected) { } diff --git a/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java b/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java index 5e0d58770..a9be7d5c2 100644 --- a/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java +++ b/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java @@ -1866,8 +1866,9 @@ public final class ResponseCacheTest { HttpURLConnection httpURLConnection = (HttpURLConnection) connection; assertEquals(server.url("/").url(), uri.toURL()); assertEquals(200, httpURLConnection.getResponseCode()); + InputStream is = httpURLConnection.getInputStream(); try { - httpURLConnection.getInputStream(); + is.read(); fail(); } catch (UnsupportedOperationException expected) { } diff --git a/okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/WebSocketReader.java b/okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/WebSocketReader.java index d81785aa8..70110e9db 100644 --- a/okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/WebSocketReader.java +++ b/okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/WebSocketReader.java @@ -233,7 +233,7 @@ public final class WebSocketReader { return -1; } - @Override public BufferedSource source() throws IOException { + @Override public BufferedSource source() { return source; } }; diff --git a/okhttp/src/main/java/com/squareup/okhttp/ResponseBody.java b/okhttp/src/main/java/com/squareup/okhttp/ResponseBody.java index bdd98b4c6..1f3aae3d4 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/ResponseBody.java +++ b/okhttp/src/main/java/com/squareup/okhttp/ResponseBody.java @@ -39,11 +39,11 @@ public abstract class ResponseBody implements Closeable { */ public abstract long contentLength() throws IOException; - public final InputStream byteStream() throws IOException { + public final InputStream byteStream() { return source().inputStream(); } - public abstract BufferedSource source() throws IOException; + public abstract BufferedSource source(); public final byte[] bytes() throws IOException { long contentLength = contentLength(); @@ -69,7 +69,7 @@ public abstract class ResponseBody implements Closeable { * of the Content-Type header. If that header is either absent or lacks a * charset, this will attempt to decode the response body as UTF-8. */ - public final Reader charStream() throws IOException { + public final Reader charStream() { Reader r = reader; return r != null ? r : (reader = new InputStreamReader(byteStream(), charset())); } diff --git a/samples/guide/src/main/java/com/squareup/okhttp/recipes/Progress.java b/samples/guide/src/main/java/com/squareup/okhttp/recipes/Progress.java index 9d329fa16..06f0a2c40 100644 --- a/samples/guide/src/main/java/com/squareup/okhttp/recipes/Progress.java +++ b/samples/guide/src/main/java/com/squareup/okhttp/recipes/Progress.java @@ -84,7 +84,7 @@ public final class Progress { return responseBody.contentLength(); } - @Override public BufferedSource source() throws IOException { + @Override public BufferedSource source() { if (bufferedSource == null) { bufferedSource = Okio.buffer(source(responseBody.source())); }