From 41d14d6a70c36aa2a13940f7144a2fc00b03a1aa Mon Sep 17 00:00:00 2001 From: Ivan Andika Date: Sun, 6 Oct 2024 22:45:38 +0800 Subject: [PATCH] Fix find bugs --- .../s3/awssdk/v1/AbstractS3SDKV1Tests.java | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v1/AbstractS3SDKV1Tests.java b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v1/AbstractS3SDKV1Tests.java index 51c7a7f766c..069ac4bd9dc 100644 --- a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v1/AbstractS3SDKV1Tests.java +++ b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v1/AbstractS3SDKV1Tests.java @@ -779,31 +779,32 @@ private List uploadParts(String bucketName, String key, String uploadI // Upload the file parts. long filePosition = 0; long fileLength = file.length(); - FileInputStream fileInputStream = new FileInputStream(file); - for (int i = 1; filePosition < fileLength; i++) { - // Because the last part could be less than 5 MB, adjust the part size as - // needed. - partSize = Math.min(partSize, (fileLength - filePosition)); - - // Create the request to upload a part. - UploadPartRequest uploadRequest = new UploadPartRequest() - .withBucketName(bucketName) - .withKey(key) - .withUploadId(uploadId) - .withPartNumber(i) - .withFileOffset(filePosition) - .withFile(file) - .withPartSize(partSize); - - // Upload the part and add the response's ETag to our list. - UploadPartResult uploadResult = s3Client.uploadPart(uploadRequest); - PartETag partETag = uploadResult.getPartETag(); - assertEquals(i, partETag.getPartNumber()); - assertEquals(DatatypeConverter.printHexBinary( - calculateDigest(fileInputStream, 0, (int) partSize)).toLowerCase(), partETag.getETag()); - partETags.add(partETag); - - filePosition += partSize; + try (FileInputStream fileInputStream = new FileInputStream(file)) { + for (int i = 1; filePosition < fileLength; i++) { + // Because the last part could be less than 5 MB, adjust the part size as + // needed. + partSize = Math.min(partSize, (fileLength - filePosition)); + + // Create the request to upload a part. + UploadPartRequest uploadRequest = new UploadPartRequest() + .withBucketName(bucketName) + .withKey(key) + .withUploadId(uploadId) + .withPartNumber(i) + .withFileOffset(filePosition) + .withFile(file) + .withPartSize(partSize); + + // Upload the part and add the response's ETag to our list. + UploadPartResult uploadResult = s3Client.uploadPart(uploadRequest); + PartETag partETag = uploadResult.getPartETag(); + assertEquals(i, partETag.getPartNumber()); + assertEquals(DatatypeConverter.printHexBinary( + calculateDigest(fileInputStream, 0, (int) partSize)).toLowerCase(), partETag.getETag()); + partETags.add(partETag); + + filePosition += partSize; + } } return partETags;