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:
@@ -474,10 +474,9 @@ public final class JavaApiConverter {
|
|||||||
* Creates an OkHttp Response.Body containing the supplied information.
|
* Creates an OkHttp Response.Body containing the supplied information.
|
||||||
*/
|
*/
|
||||||
private static ResponseBody createOkBody(final Headers okHeaders,
|
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() {
|
return new ResponseBody() {
|
||||||
private BufferedSource body;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MediaType contentType() {
|
public MediaType contentType() {
|
||||||
String contentTypeHeader = okHeaders.get("Content-Type");
|
String contentTypeHeader = okHeaders.get("Content-Type");
|
||||||
@@ -488,11 +487,7 @@ public final class JavaApiConverter {
|
|||||||
public long contentLength() {
|
public long contentLength() {
|
||||||
return OkHeaders.contentLength(okHeaders);
|
return OkHeaders.contentLength(okHeaders);
|
||||||
}
|
}
|
||||||
@Override public BufferedSource source() throws IOException {
|
@Override public BufferedSource source() {
|
||||||
if (body == null) {
|
|
||||||
InputStream is = cacheResponse.getBody();
|
|
||||||
body = Okio.buffer(Okio.source(is));
|
|
||||||
}
|
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -501,13 +496,13 @@ public final class JavaApiConverter {
|
|||||||
/**
|
/**
|
||||||
* Creates an OkHttp Response.Body containing the supplied information.
|
* 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()) {
|
if (!urlConnection.getDoInput()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return new ResponseBody() {
|
|
||||||
private BufferedSource body;
|
|
||||||
|
|
||||||
|
final BufferedSource body = Okio.buffer(Okio.source(urlConnection.getInputStream()));
|
||||||
|
return new ResponseBody() {
|
||||||
@Override public MediaType contentType() {
|
@Override public MediaType contentType() {
|
||||||
String contentTypeHeader = urlConnection.getContentType();
|
String contentTypeHeader = urlConnection.getContentType();
|
||||||
return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
|
return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
|
||||||
@@ -516,11 +511,7 @@ public final class JavaApiConverter {
|
|||||||
String s = urlConnection.getHeaderField("Content-Length");
|
String s = urlConnection.getHeaderField("Content-Length");
|
||||||
return stringToLong(s);
|
return stringToLong(s);
|
||||||
}
|
}
|
||||||
@Override public BufferedSource source() throws IOException {
|
@Override public BufferedSource source() {
|
||||||
if (body == null) {
|
|
||||||
InputStream is = urlConnection.getInputStream();
|
|
||||||
body = Okio.buffer(Okio.source(is));
|
|
||||||
}
|
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -727,7 +718,11 @@ public final class JavaApiConverter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream getInputStream() throws IOException {
|
public InputStream getInputStream() throws IOException {
|
||||||
throw throwResponseBodyAccessException();
|
return new InputStream() {
|
||||||
|
@Override public int read() throws IOException {
|
||||||
|
throw throwResponseBodyAccessException();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -319,8 +319,9 @@ public class JavaApiConverterTest {
|
|||||||
JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse);
|
JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse);
|
||||||
// Check an arbitrary (not complete) set of methods that can be used to access the response
|
// Check an arbitrary (not complete) set of methods that can be used to access the response
|
||||||
// body.
|
// body.
|
||||||
|
InputStream is = httpUrlConnection.getInputStream();
|
||||||
try {
|
try {
|
||||||
httpUrlConnection.getInputStream();
|
is.read();
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException expected) {
|
} catch (UnsupportedOperationException expected) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1866,8 +1866,9 @@ public final class ResponseCacheTest {
|
|||||||
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
|
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
|
||||||
assertEquals(server.url("/").url(), uri.toURL());
|
assertEquals(server.url("/").url(), uri.toURL());
|
||||||
assertEquals(200, httpURLConnection.getResponseCode());
|
assertEquals(200, httpURLConnection.getResponseCode());
|
||||||
|
InputStream is = httpURLConnection.getInputStream();
|
||||||
try {
|
try {
|
||||||
httpURLConnection.getInputStream();
|
is.read();
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException expected) {
|
} catch (UnsupportedOperationException expected) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ public final class WebSocketReader {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public BufferedSource source() throws IOException {
|
@Override public BufferedSource source() {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -39,11 +39,11 @@ public abstract class ResponseBody implements Closeable {
|
|||||||
*/
|
*/
|
||||||
public abstract long contentLength() throws IOException;
|
public abstract long contentLength() throws IOException;
|
||||||
|
|
||||||
public final InputStream byteStream() throws IOException {
|
public final InputStream byteStream() {
|
||||||
return source().inputStream();
|
return source().inputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract BufferedSource source() throws IOException;
|
public abstract BufferedSource source();
|
||||||
|
|
||||||
public final byte[] bytes() throws IOException {
|
public final byte[] bytes() throws IOException {
|
||||||
long contentLength = contentLength();
|
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
|
* 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.
|
* 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;
|
Reader r = reader;
|
||||||
return r != null ? r : (reader = new InputStreamReader(byteStream(), charset()));
|
return r != null ? r : (reader = new InputStreamReader(byteStream(), charset()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ public final class Progress {
|
|||||||
return responseBody.contentLength();
|
return responseBody.contentLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public BufferedSource source() throws IOException {
|
@Override public BufferedSource source() {
|
||||||
if (bufferedSource == null) {
|
if (bufferedSource == null) {
|
||||||
bufferedSource = Okio.buffer(source(responseBody.source()));
|
bufferedSource = Okio.buffer(source(responseBody.source()));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user