RED-6497: Removed alternate file size check because it offers less error reporting capabilities

This commit is contained in:
Viktor Seifert 2023-03-30 17:37:18 +02:00
parent 338b06079b
commit 4c25ac402d
2 changed files with 8 additions and 43 deletions

View File

@ -24,7 +24,6 @@ import lombok.extern.slf4j.Slf4j;
public class FileSystemBackedArchiver implements AutoCloseable {
private final boolean rethrowExceptions;
private final boolean useFileAttributes;
private final Set<String> createdFolders = new HashSet<>();
private final File tempFile;
private final ZipOutputStream zipOutputStream;
@ -35,7 +34,7 @@ public class FileSystemBackedArchiver implements AutoCloseable {
@SneakyThrows
public FileSystemBackedArchiver() {
this(false, true);
this(false);
}
@ -46,10 +45,9 @@ public class FileSystemBackedArchiver implements AutoCloseable {
* @param rethrowExceptions If true exceptions caught when handling streams and files will be re-thrown.
*/
@SneakyThrows
FileSystemBackedArchiver(boolean rethrowExceptions, boolean useFileAttributes) {
FileSystemBackedArchiver(boolean rethrowExceptions) {
this.rethrowExceptions = rethrowExceptions;
this.useFileAttributes = useFileAttributes;
tempFile = FileUtils.createTempFile("archive", ".zip");
zipOutputStream = new ZipOutputStream(new FileOutputStream(tempFile));
}
@ -131,12 +129,8 @@ public class FileSystemBackedArchiver implements AutoCloseable {
if (tempFile.exists()) {
try {
if (useFileAttributes) {
var basicFileAttributes = Files.readAttributes(tempFile.toPath(), BasicFileAttributes.class);
tempFileLength = basicFileAttributes.size();
} else {
tempFileLength = tempFile.length();
}
var basicFileAttributes = Files.readAttributes(tempFile.toPath(), BasicFileAttributes.class);
tempFileLength = basicFileAttributes.size();
} catch (IOException e) {
if (rethrowExceptions) {
throw new RuntimeException(e);

View File

@ -27,7 +27,7 @@ public class FileSystemBackedArchiverTest {
@SneakyThrows
public void testFileSystemBackedArchiver() {
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false)) {
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true)) {
SplittableRandom sr = new SplittableRandom();
@ -64,7 +64,7 @@ public class FileSystemBackedArchiverTest {
@Test
public void testContentLengthForTwoEntries() {
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false)) {
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true)) {
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", "original", dummyFileContent));
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", "preview", dummyFileContent));
@ -74,40 +74,11 @@ public class FileSystemBackedArchiverTest {
}
@Test
public void testContentLengthForTwoEntriesComparingFileSize() {
long defaultFileLength;
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false)) {
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", "original", dummyFileContent));
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", "preview", dummyFileContent));
long contentLength = fileSystemBackedArchiver.getContentLength();
assertThat(contentLength).isGreaterThan(0);
defaultFileLength = contentLength;
}
long fileAttributesContentLength;
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, true)) {
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", "original", dummyFileContent));
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", "preview", dummyFileContent));
long contentLength = fileSystemBackedArchiver.getContentLength();
assertThat(contentLength).isGreaterThan(0);
fileAttributesContentLength = contentLength;
}
assertThat(defaultFileLength).isEqualTo(fileAttributesContentLength);
}
@Test
@SneakyThrows
public void testContentLengthForTwoEntriesAndStream() {
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false)) {
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true)) {
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", "original", dummyFileContent));
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", "preview", dummyFileContent));
@ -126,7 +97,7 @@ public class FileSystemBackedArchiverTest {
@Test
public void testContentLengthForTwoEntriesWithClosing() {
// deliberately do not use try-with-resources to see if the content-length is available after temp file deletion
var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false);
var fileSystemBackedArchiver = new FileSystemBackedArchiver(true);
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", "original", dummyFileContent));
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", "preview", dummyFileContent));