Skip to content

Commit

Permalink
Always close Input/OutputStreams
Browse files Browse the repository at this point in the history
Also: formatting
  • Loading branch information
afranken committed Dec 19, 2024
1 parent 286692b commit 15735b9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,17 @@ Version 4.x is JDK17 LTS bytecode compatible, with Docker and JUnit / direct Jav
* Features and fixes
* Allow overriding headers in head object
* Implement If-(Un)modified-Since handling (fixes #829)
* Close all InputStreams and OutputStreams
* Refactorings
* Use Tomcat instead of Jetty as the application container (fixes #2136)
* "FROM" in Dockerfile did not match "as"
* Delete files on shutdown using a `DisposableBean` instead of `File#deleteOnExit()`
* Version updates (deliverable dependencies)
* none
* Bump spring-boot.version from 3.3.5 to 3.4.1
* Version updates (build dependencies)
* Bump github/codeql-action from 3.27.6 to 3.27.9
* Bump actions/upload-artifact from 4.4.3 to 4.5.0
* Bump actions/setup-java from 4.5.0 to 4.6.0
* Bump com.puppycrawl.tools:checkstyle from 10.20.2 to 10.21.0
* Jackson 2.18.2 to 2.17.2 (remove override, use Spring-Boot supplied version)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BoundedInputStream;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange;
Expand Down Expand Up @@ -785,7 +784,13 @@ private static void extractBytesToOutputStream(HttpRange range, S3ObjectMetadata
try (var fis = Files.newInputStream(s3ObjectMetadata.dataPath())) {
var skip = fis.skip(range.getRangeStart(fileSize));
if (skip == range.getRangeStart(fileSize)) {
IOUtils.copy(new BoundedInputStream(fis, bytesToRead), outputStream);
try (var bis = BoundedInputStream
.builder()
.setInputStream(fis)
.setMaxCount(bytesToRead)
.get()) {
bis.transferTo(outputStream);
}
} else {
throw new IllegalStateException("Could not skip exact byte range");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void verifyChecksum(Path path, String checksum, ChecksumAlgorithm checksu
public Pair<Path, String> toTempFile(InputStream inputStream, HttpHeaders httpHeaders) {
try {
var tempFile = Files.createTempFile("ObjectService", "toTempFile");
try (OutputStream os = Files.newOutputStream(tempFile)) {
InputStream wrappedStream = wrapStream(inputStream, httpHeaders);
try (var os = Files.newOutputStream(tempFile);
var wrappedStream = wrapStream(inputStream, httpHeaders)) {
wrappedStream.transferTo(os);
ChecksumAlgorithm algorithmFromSdk = checksumAlgorithmFromSdk(httpHeaders);
if (algorithmFromSdk != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.StreamSupport;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BoundedInputStream;
import org.apache.commons.lang3.stream.Streams;
import org.slf4j.Logger;
Expand Down Expand Up @@ -249,29 +248,33 @@ public CompleteMultipartUploadResult completeMultipartUpload(BucketMetadata buck
)
.toList();
Path tempFile = null;
try (var inputStream = toInputStream(partsPaths)) {
try {
tempFile = Files.createTempFile("completeMultipartUpload", "");
inputStream.transferTo(Files.newOutputStream(tempFile));
var checksumFor = checksumFor(partsPaths, uploadInfo);
var etag = hexDigestMultipart(partsPaths);
objectStore.storeS3ObjectMetadata(bucket,
id,
key,
uploadInfo.contentType(),
uploadInfo.storeHeaders(),
tempFile,
uploadInfo.userMetadata(),
encryptionHeaders,
etag,
Collections.emptyList(), //TODO: no tags for multi part uploads?
uploadInfo.checksumAlgorithm(),
checksumFor,
uploadInfo.upload().owner(),
uploadInfo.storageClass()
);
FileUtils.deleteDirectory(partFolder.toFile());
return new CompleteMultipartUploadResult(location, uploadInfo.bucket(),
key, etag, uploadInfo, checksumFor);

try (var is = toInputStream(partsPaths);
var os = newOutputStream(tempFile)) {
is.transferTo(os);
var checksumFor = checksumFor(partsPaths, uploadInfo);
var etag = hexDigestMultipart(partsPaths);
objectStore.storeS3ObjectMetadata(bucket,
id,
key,
uploadInfo.contentType(),
uploadInfo.storeHeaders(),
tempFile,
uploadInfo.userMetadata(),
encryptionHeaders,
etag,
Collections.emptyList(), //TODO: no tags for multi part uploads?
uploadInfo.checksumAlgorithm(),
checksumFor,
uploadInfo.upload().owner(),
uploadInfo.storageClass()
);
FileUtils.deleteDirectory(partFolder.toFile());
return new CompleteMultipartUploadResult(location, uploadInfo.bucket(),
key, etag, uploadInfo, checksumFor);
}
} catch (IOException e) {
throw new IllegalStateException(String.format(
"Error finishing multipart upload bucket=%s, key=%s, id=%s, uploadId=%s",
Expand Down Expand Up @@ -381,13 +384,13 @@ private String copyPartToFile(BucketMetadata bucket,
var targetStream = newOutputStream(partFile.toPath())) {
var skip = sourceStream.skip(from);
if (skip == from) {
IOUtils.copy(BoundedInputStream
try (var bis = BoundedInputStream
.builder()
.setInputStream(sourceStream)
.setMaxCount(len)
.get(),
targetStream
);
.get()) {
bis.transferTo(targetStream);
}
} else {
throw new IllegalStateException("Could not skip exact byte range");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static String checksumFor(Path path, Algorithm algorithm) {
* @param algorithm algorithm to use
* @return the checksum
*/
public static String checksumFor(InputStream is, Algorithm algorithm) {
private static String checksumFor(InputStream is, Algorithm algorithm) {
return BinaryUtils.toBase64(checksum(is, algorithm));
}

Expand All @@ -83,7 +83,7 @@ public static String checksumFor(InputStream is, Algorithm algorithm) {
* @param algorithm algorithm to use
* @return the checksum
*/
public static byte[] checksum(InputStream is, Algorithm algorithm) {
private static byte[] checksum(InputStream is, Algorithm algorithm) {
SdkChecksum sdkChecksum = SdkChecksum.forAlgorithm(algorithm);
try {
byte[] buffer = new byte[4096];
Expand Down Expand Up @@ -230,7 +230,7 @@ public static String base64Digest(InputStream inputStream) {
*
* @return String Base64 MD5 digest.
*/
public static String base64Digest(String salt, InputStream inputStream) {
private static String base64Digest(String salt, InputStream inputStream) {
return Base64.encodeBase64String(md5(salt, inputStream));
}

Expand Down

0 comments on commit 15735b9

Please sign in to comment.