Don't have to check for stuff if you throw instead

This commit is contained in:
Sollace 2018-07-30 10:34:54 +02:00
parent bd010d6d97
commit 993eb7ac37

View file

@ -119,22 +119,18 @@ public class NetClient implements Closeable {
* Gets or obtains a response status code. * Gets or obtains a response status code.
*/ */
public int getResponseCode() throws IOException { public int getResponseCode() throws IOException {
if (getResponse() == null) { return getResponse().getStatusLine().getStatusCode();
return HttpStatus.SC_NOT_FOUND;
}
return response.getStatusLine().getStatusCode();
} }
/** /**
* Consumes and returns the entire response body. * Consumes and returns the entire response body.
*/ */
public String getResponseText() throws IOException { public String getResponseText() throws IOException {
if (getResponse() == null || response.getEntity() == null) { if (getResponse().getEntity() == null) {
return ""; return "";
} }
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(getResponse().getEntity().getContent()))) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
int ch; int ch;
@ -148,8 +144,12 @@ public class NetClient implements Closeable {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (response != null) { try {
EntityUtils.consumeQuietly(response.getEntity()); if (response != null) {
response.close();
}
} finally {
response = null;
} }
} }
} }