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

use try with resource style to auto-close streams #871

Merged
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
33 changes: 12 additions & 21 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1086,10 +1086,8 @@ private HttpResponse execute(
}

String errorXml = null;
try {
errorXml = new String(response.body().bytes(), StandardCharsets.UTF_8);
} finally {
response.close();
try (ResponseBody responseBody = response.body()) {
errorXml = new String(responseBody.bytes(), StandardCharsets.UTF_8);
}

if (this.traceStream != null && !("".equals(errorXml) && method.equals(Method.HEAD))) {
Expand Down Expand Up @@ -2542,10 +2540,8 @@ private List<DeleteError> removeObject(String bucketName, List<DeleteObject> obj
HttpResponse response = executePost(bucketName, null, null, queryParamMap, request);

String bodyContent = "";
try {
bodyContent = new String(response.body().bytes(), StandardCharsets.UTF_8);
} finally {
response.body().close();
try (ResponseBody body = response.body()) {
bodyContent = new String(body.bytes(), StandardCharsets.UTF_8);
}

List<DeleteError> errorList = null;
Expand Down Expand Up @@ -3883,12 +3879,8 @@ public void putObject(
+ " does not match");
}

RandomAccessFile file = new RandomAccessFile(filePath.toFile(), "r");

try {
try (RandomAccessFile file = new RandomAccessFile(filePath.toFile(), "r")) {
putObject(bucketName, objectName, options, file);
} finally {
file.close();
}
}

Expand Down Expand Up @@ -3979,8 +3971,7 @@ public String getBucketPolicy(String bucketName)
response = executeGet(bucketName, null, null, queryParamMap);
bytesRead = response.body().byteStream().read(buf, 0, MAX_BUCKET_POLICY_SIZE);
if (bytesRead < 0) {
// reached EOF
throw new IOException("reached EOF when reading bucket policy");
throw new IOException("unexpected EOF when reading bucket policy");
}

// Read one byte extra to ensure only MAX_BUCKET_POLICY_SIZE data is sent by the server.
Expand All @@ -3991,7 +3982,9 @@ public String getBucketPolicy(String bucketName)
if (byteRead < 0) {
// reached EOF which is fine.
break;
} else if (byteRead > 0) {
}

if (byteRead > 0) {
throw new BucketPolicyTooLargeException(bucketName);
}
}
Expand All @@ -4001,7 +3994,7 @@ public String getBucketPolicy(String bucketName)
throw e;
}
} finally {
if (response != null && response.body() != null) {
if (response != null) {
response.body().close();
}
}
Expand Down Expand Up @@ -4564,11 +4557,9 @@ private void completeMultipart(
HttpResponse response =
executePost(bucketName, objectName, null, queryParamMap, completeManifest);
String bodyContent = "";
try {
bodyContent = new String(response.body().bytes(), StandardCharsets.UTF_8);
try (ResponseBody body = response.body()) {
bodyContent = new String(body.bytes(), StandardCharsets.UTF_8);
bodyContent = bodyContent.trim();
} finally {
response.body().close();
}

// Handle if body contains error.
Expand Down