Skip to content

Commit

Permalink
Always close InputStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
afranken committed Dec 18, 2024
1 parent b696f08 commit 19894bf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
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 (OutputStream os = Files.newOutputStream(tempFile);
InputStream 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 @@ -381,13 +380,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

0 comments on commit 19894bf

Please sign in to comment.