Skip to content

Commit

Permalink
added buffers to all InputStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
pwinckles committed Sep 8, 2021
1 parent 6681ecc commit 94a52ed
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -174,7 +175,7 @@ public void updateObjectDetails(Inventory inventory, String inventoryDigest, Pat
Enforce.notNull(inventoryFile, "inventoryFile cannot be null");
Enforce.notNull(runnable, "runnable cannot be null");

try (var inventoryStream = Files.newInputStream(inventoryFile)) {
try (var inventoryStream = new BufferedInputStream(Files.newInputStream(inventoryFile))) {
updateObjectDetailsInternal(inventory, inventoryDigest, inventoryStream, runnable);
} catch (IOException e) {
throw new OcflIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import edu.wisc.library.ocfl.api.model.DigestAlgorithm;
import edu.wisc.library.ocfl.api.util.Enforce;

import java.io.BufferedInputStream;

/**
* OcflFileRetriever implementation for lazy-loading files from cloud storage.
*/
Expand Down Expand Up @@ -71,7 +73,7 @@ public CloudOcflFileRetriever(CloudClient cloudClient, String key, DigestAlgorit
@Override
public FixityCheckInputStream retrieveFile() {
// TODO caching?
return new FixityCheckInputStream(cloudClient.downloadStream(key), digestAlgorithm, digestValue);
return new FixityCheckInputStream(new BufferedInputStream(cloudClient.downloadStream(key)), digestAlgorithm, digestValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -272,7 +273,8 @@ public void reconstructObjectVersion(Inventory inventory, VersionNum versionNum,

UncheckedFiles.createDirectories(destination.getParent());

try (var stream = new FixityCheckInputStream(cloudClient.downloadStream(srcPath), digestAlgorithm, id)) {
try (var stream = new FixityCheckInputStream(new BufferedInputStream(cloudClient.downloadStream(srcPath)),
digestAlgorithm, id)) {
Files.copy(stream, destination);
stream.checkFixity();
} catch (FixityCheckException e) {
Expand Down Expand Up @@ -736,7 +738,7 @@ private Inventory downloadAndVerifyInventory(String objectId, String objectRootP
var expectedDigest = findAndGetDigestFromSidecar(objectRootPath);
var remotePath = ObjectPaths.inventoryPath(objectRootPath);

try (var stream = new FixityCheckInputStream(cloudClient.downloadStream(remotePath),
try (var stream = new FixityCheckInputStream(new BufferedInputStream(cloudClient.downloadStream(remotePath)),
expectedDigest.getKey(), expectedDigest.getValue())) {
var inventory = inventoryMapper.read(objectRootPath, expectedDigest.getValue(), stream);

Expand All @@ -758,7 +760,7 @@ private Inventory downloadAndVerifyMutableInventory(String objectId, String obje
var expectedDigest = findAndGetDigestFromSidecar(ObjectPaths.mutableHeadVersionPath(objectRootPath));
var remotePath = ObjectPaths.mutableHeadInventoryPath(objectRootPath);

try (var stream = new FixityCheckInputStream(cloudClient.downloadStream(remotePath),
try (var stream = new FixityCheckInputStream(new BufferedInputStream(cloudClient.downloadStream(remotePath)),
expectedDigest.getKey(), expectedDigest.getValue())) {
var revisionNum = identifyLatestRevision(objectRootPath);
var inventory = inventoryMapper.readMutableHead(objectRootPath, expectedDigest.getValue(), revisionNum, stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import edu.wisc.library.ocfl.api.model.DigestAlgorithm;
import edu.wisc.library.ocfl.api.util.Enforce;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -55,7 +56,7 @@ public FileSystemOcflFileRetriever(Path filePath, DigestAlgorithm digestAlgorith
@Override
public FixityCheckInputStream retrieveFile() {
try {
return new FixityCheckInputStream(Files.newInputStream(filePath), digestAlgorithm, digestValue);
return new FixityCheckInputStream(new BufferedInputStream(Files.newInputStream(filePath)), digestAlgorithm, digestValue);
} catch (IOException e) {
throw new OcflIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileAlreadyExistsException;
Expand Down Expand Up @@ -292,7 +293,7 @@ public void reconstructObjectVersion(Inventory inventory, VersionNum versionNum,

UncheckedFiles.createDirectories(destination.getParent());

try (var stream = new FixityCheckInputStream(Files.newInputStream(srcPath), digestAlgorithm, id)) {
try (var stream = new FixityCheckInputStream(new BufferedInputStream(Files.newInputStream(srcPath)), digestAlgorithm, id)) {
Files.copy(stream, destination);
stream.checkFixity();
} catch (IOException e) {
Expand Down Expand Up @@ -642,7 +643,7 @@ private FixityCheckInputStream inventoryVerifyingInputStream(Path inventoryPath)
var expectedDigest = SidecarMapper.readDigest(sidecarPath);
var algorithm = SidecarMapper.getDigestAlgorithmFromSidecar(FileUtil.pathToStringStandardSeparator(sidecarPath));

return new FixityCheckInputStream(Files.newInputStream(inventoryPath), algorithm, expectedDigest);
return new FixityCheckInputStream(new BufferedInputStream(Files.newInputStream(inventoryPath)), algorithm, expectedDigest);
}

private RevisionNum identifyLatestRevision(Path objectRootPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand Down Expand Up @@ -516,7 +515,7 @@ private void fixityCheck(String objectRootPath, SimpleInventory inventory, Manif
expectations.putAll(fixityDigests);
}

try (var contentStream = new BufferedInputStream(storage.readFile(storagePath))) {
try (var contentStream = storage.readFile(storagePath)) {
var wrapped = MultiDigestInputStream.create(contentStream, expectations.keySet());

while (wrapped.read() != -1) {
Expand Down Expand Up @@ -734,7 +733,7 @@ private ParseResult parseInventory(String inventoryPath,
}

private String computeInventoryDigest(String inventoryPath, DigestAlgorithm algorithm) {
try (var stream = new BufferedInputStream(storage.readFile(inventoryPath))) {
try (var stream = storage.readFile(inventoryPath)) {
var wrapped = MultiDigestInputStream.create(stream, List.of(algorithm));
while (wrapped.read() > 0) {
// consume stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import edu.wisc.library.ocfl.core.storage.cloud.KeyNotFoundException;
import edu.wisc.library.ocfl.core.storage.cloud.ListResult;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -81,7 +82,7 @@ public boolean fileExists(String filePath) {
@Override
public InputStream readFile(String filePath) {
try {
return client.downloadStream(filePath);
return new BufferedInputStream(client.downloadStream(filePath));
} catch (KeyNotFoundException e) {
throw new NotFoundException(String.format("%s was not found", filePath), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import edu.wisc.library.ocfl.api.util.Enforce;
import edu.wisc.library.ocfl.core.util.FileUtil;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
Expand Down Expand Up @@ -75,7 +76,7 @@ public boolean fileExists(String filePath) {
@Override
public InputStream readFile(String filePath) {
try {
return Files.newInputStream(storageRoot.resolve(filePath));
return new BufferedInputStream(Files.newInputStream(storageRoot.resolve(filePath)));
} catch (NoSuchFileException e) {
throw new NotFoundException(String.format("%s was not found", filePath), e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -2299,7 +2300,7 @@ private void assertStream(String expected, OcflObjectVersionFile actual) throws
}

private void verifyStream(Path expectedFile, OcflObjectVersionFile actual) throws IOException {
try (var fileStream = Files.newInputStream(expectedFile)) {
try (var fileStream = new BufferedInputStream(Files.newInputStream(expectedFile))) {
assertStream(TestHelper.inputToString(fileStream), actual);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package edu.wisc.library.ocfl.test;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -40,7 +41,7 @@ private TestHelper() {
}

public static String fileToString(Path file) {
try (var input = Files.newInputStream(file)) {
try (var input = new BufferedInputStream(Files.newInputStream(file))) {
return inputToString(input);
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -63,7 +64,7 @@ public static InputStream inputStream(String content) {

public static InputStream inputStream(Path path) {
try {
return Files.newInputStream(path);
return new BufferedInputStream(Files.newInputStream(path));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 94a52ed

Please sign in to comment.