Skip to content

Commit

Permalink
prevent null pointer exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
zegnus committed Aug 20, 2019
1 parent 01cff2d commit 646e40b
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;

import okhttp3.Response;
import okhttp3.ResponseBody;

class WrappedOkHttpResponse implements NetworkResponse {

Expand All @@ -30,16 +31,31 @@ public String header(String name, String defaultValue) {

@Override
public InputStream openByteStream() throws IOException {
return response.body().byteStream();
ResponseBody body = response.body();
if (body == null) {
throw new IOException("Response body is null");
} else {
return body.byteStream();
}
}

@Override
public void closeByteStream() throws IOException {
response.body().close();
ResponseBody body = response.body();
if (body == null) {
throw new IOException("Response body is null");
} else {
body.close();
}
}

@Override
public long bodyContentLength() {
return response.body().contentLength();
ResponseBody body = response.body();
if (body == null) {
return -1;
} else {
return body.contentLength();
}
}
}

0 comments on commit 646e40b

Please sign in to comment.