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 646c010 commit 01cff2d
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.InputStream;

import okhttp3.Response;
import okhttp3.ResponseBody;

class CustomHttpResponse implements NetworkResponse {

Expand All @@ -32,16 +33,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 01cff2d

Please sign in to comment.