Skip to content

Commit

Permalink
Remove IOException from source() and streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Dec 15, 2015
1 parent ee3114d commit dcb3c14
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,9 @@ static String extractStatusLine(Map<String, List<String>> javaResponseHeaders)
* 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");
Expand All @@ -488,11 +487,7 @@ public MediaType contentType() {
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;
}
};
Expand All @@ -501,13 +496,13 @@ public long contentLength() {
/**
* 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);
Expand All @@ -516,11 +511,7 @@ private static ResponseBody createOkBody(final URLConnection urlConnection) {
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;
}
};
Expand Down Expand Up @@ -727,7 +718,11 @@ public Object getContent(Class[] classes) throws IOException {

@Override
public InputStream getInputStream() throws IOException {
throw throwResponseBodyAccessException();
return new InputStream() {
@Override public int read() throws IOException {
throw throwResponseBodyAccessException();
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1866,8 +1866,9 @@ private static <T> List<T> toListOrNull(T[] arrayOrNull) {
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) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private void readMessageFrame() throws IOException {
return -1;
}

@Override public BufferedSource source() throws IOException {
@Override public BufferedSource source() {
return source;
}
};
Expand Down
6 changes: 3 additions & 3 deletions okhttp/src/main/java/com/squareup/okhttp/ResponseBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -69,7 +69,7 @@ public final byte[] bytes() throws IOException {
* 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ProgressResponseBody(ResponseBody responseBody, ProgressListener progress
return responseBody.contentLength();
}

@Override public BufferedSource source() throws IOException {
@Override public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(responseBody.source()));
}
Expand Down

0 comments on commit dcb3c14

Please sign in to comment.