RED-6497: Changed code to use a different method of getting a file size to have better error reporting
This commit is contained in:
parent
65884ce42b
commit
284738b59e
@ -7,6 +7,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -23,6 +24,7 @@ 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;
|
||||
@ -33,7 +35,7 @@ public class FileSystemBackedArchiver implements AutoCloseable {
|
||||
@SneakyThrows
|
||||
public FileSystemBackedArchiver() {
|
||||
|
||||
this(false);
|
||||
this(false, true);
|
||||
|
||||
}
|
||||
|
||||
@ -44,9 +46,10 @@ public class FileSystemBackedArchiver implements AutoCloseable {
|
||||
* @param rethrowExceptions If true exceptions caught when handling streams and files will be re-thrown.
|
||||
*/
|
||||
@SneakyThrows
|
||||
FileSystemBackedArchiver(boolean rethrowExceptions) {
|
||||
FileSystemBackedArchiver(boolean rethrowExceptions, boolean useFileAttributes) {
|
||||
|
||||
this.rethrowExceptions = rethrowExceptions;
|
||||
this.useFileAttributes = useFileAttributes;
|
||||
tempFile = FileUtils.createTempFile("archive", ".zip");
|
||||
zipOutputStream = new ZipOutputStream(new FileOutputStream(tempFile));
|
||||
}
|
||||
@ -127,7 +130,18 @@ public class FileSystemBackedArchiver implements AutoCloseable {
|
||||
}
|
||||
|
||||
if (tempFile.exists()) {
|
||||
tempFileLength = tempFile.length();
|
||||
try {
|
||||
if (useFileAttributes) {
|
||||
var basicFileAttributes = Files.readAttributes(tempFile.toPath(), BasicFileAttributes.class);
|
||||
tempFileLength = basicFileAttributes.size();
|
||||
} else {
|
||||
tempFileLength = tempFile.length();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (rethrowExceptions) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.warn("The temp file {} was deleted before it was completely processed", tempFile);
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ public class FileSystemBackedArchiverTest {
|
||||
@SneakyThrows
|
||||
public void testFileSystemBackedArchiver() {
|
||||
|
||||
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true)) {
|
||||
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false)) {
|
||||
|
||||
SplittableRandom sr = new SplittableRandom();
|
||||
|
||||
@ -62,7 +62,7 @@ public class FileSystemBackedArchiverTest {
|
||||
@Test
|
||||
public void testContentLengthForTwoEntries() {
|
||||
|
||||
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true)) {
|
||||
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false)) {
|
||||
|
||||
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", "original", dummyFileContent));
|
||||
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", "preview", dummyFileContent));
|
||||
@ -72,11 +72,40 @@ 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)) {
|
||||
try (var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false)) {
|
||||
|
||||
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", "original", dummyFileContent));
|
||||
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", "preview", dummyFileContent));
|
||||
@ -95,7 +124,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);
|
||||
var fileSystemBackedArchiver = new FileSystemBackedArchiver(true, false);
|
||||
|
||||
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", "original", dummyFileContent));
|
||||
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", "preview", dummyFileContent));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user