Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly dispose streams #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public void sendData() throws IOException {
settings.getAuthentication().addAuth(urlConnection, body);
}

Writer writer = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
writer.write(body);
writer.flush();
writer.close();
try( Writer writer = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8") ) {
writer.write(body);
writer.flush();
}

int rc = urlConnection.getResponseCode();
if (rc != 200) {
Expand All @@ -98,18 +98,18 @@ public boolean hasPendingData() {
}

private static String slurpErrors(HttpURLConnection urlConnection) {
try {
InputStream stream = urlConnection.getErrorStream();
try( InputStream stream = urlConnection.getErrorStream() ) {
if (stream == null) {
return "<no data>";
}

StringBuilder builder = new StringBuilder();
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
char[] buf = new char[2048];
int numRead;
while ((numRead = reader.read(buf)) > 0) {
builder.append(buf, 0, numRead);
try( InputStreamReader reader = new InputStreamReader(stream, "UTF-8") ) {
char[] buf = new char[2048];
int numRead;
while ((numRead = reader.read(buf)) > 0) {
builder.append(buf, 0, numRead);
}
}
return builder.toString();
} catch (Exception e) {
Expand Down