1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-27 18:21:14 +03:00

Merge pull request #2105 from square/jw/no-throws

Remove IOException from source() and streams.
This commit is contained in:
Jesse Wilson
2015-12-15 20:54:08 -05:00
6 changed files with 21 additions and 24 deletions

View File

@@ -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

View File

@@ -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) {
}

View File

@@ -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) {
}

View File

@@ -233,7 +233,7 @@ public final class WebSocketReader {
return -1;
}
@Override public BufferedSource source() throws IOException {
@Override public BufferedSource source() {
return source;
}
};

View File

@@ -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()));
}

View File

@@ -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()));
}