This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 1fa4fd9 (chores) camel-tarfile cleanup (#5681)
1fa4fd9 is described below
commit 1fa4fd929707e7ee2e36c2f9da93b25a51bbf813
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Wed Jun 16 17:45:09 2021 +0200
(chores) camel-tarfile cleanup (#5681)
- Rework the code to use try-with-resources to avoid leaking them even if
exceptions are thrown
---
.../aggregate/tarfile/TarAggregationStrategy.java | 78 ++++++++++++----------
1 file changed, 43 insertions(+), 35 deletions(-)
diff --git
a/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java
b/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java
index 837db66..96e2e4e 100644
---
a/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java
+++
b/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java
@@ -34,7 +34,6 @@ import org.apache.camel.component.file.GenericFileMessage;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.spi.Synchronization;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
@@ -208,33 +207,42 @@ public class TarAggregationStrategy implements
AggregationStrategy {
throw new IOException("Could not make temp file (" +
source.getName() + ")");
}
- FileInputStream fis = new FileInputStream(tmpTar);
- TarArchiveInputStream tin
- = (TarArchiveInputStream) new
ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
- TarArchiveOutputStream tos = new TarArchiveOutputStream(new
FileOutputStream(source));
- tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
- tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
+ try (FileInputStream fis = new FileInputStream(tmpTar)) {
+ try (TarArchiveInputStream tin = (TarArchiveInputStream) new
ArchiveStreamFactory()
+ .createArchiveInputStream(ArchiveStreamFactory.TAR, fis)) {
+ try (TarArchiveOutputStream tos = new
TarArchiveOutputStream(new FileOutputStream(source))) {
+ tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
+
tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
- InputStream in = new FileInputStream(file);
+ try (InputStream in = new FileInputStream(file)) {
+ copyExistingEntries(tin, tos);
- // copy the existing entries
- ArchiveEntry nextEntry;
- while ((nextEntry = tin.getNextEntry()) != null) {
- tos.putArchiveEntry(nextEntry);
- IOUtils.copy(tin, tos);
- tos.closeArchiveEntry();
+ // Add the new entry
+ addNewEntry(file, fileName, tos, in);
+ }
+ }
+ }
}
+ LOG.trace("Deleting temporary file: {}", tmpTar);
+ FileUtil.deleteFile(tmpTar);
+ }
- // Add the new entry
+ private void addNewEntry(File file, String fileName,
TarArchiveOutputStream tos, InputStream in) throws IOException {
TarArchiveEntry entry = new TarArchiveEntry(fileName == null ?
file.getName() : fileName);
entry.setSize(file.length());
tos.putArchiveEntry(entry);
IOUtils.copy(in, tos);
tos.closeArchiveEntry();
+ }
- IOHelper.close(fis, in, tin, tos);
- LOG.trace("Deleting temporary file: {}", tmpTar);
- FileUtil.deleteFile(tmpTar);
+ private void copyExistingEntries(TarArchiveInputStream tin,
TarArchiveOutputStream tos) throws IOException {
+ // copy the existing entries
+ ArchiveEntry nextEntry;
+ while ((nextEntry = tin.getNextEntry()) != null) {
+ tos.putArchiveEntry(nextEntry);
+ IOUtils.copy(tin, tos);
+ tos.closeArchiveEntry();
+ }
}
private void addEntryToTar(File source, String entryName, byte[] buffer,
int length) throws IOException, ArchiveException {
@@ -244,31 +252,31 @@ public class TarAggregationStrategy implements
AggregationStrategy {
throw new IOException("Cannot create temp file: " +
source.getName());
}
- FileInputStream fis = new FileInputStream(tmpTar);
- TarArchiveInputStream tin
- = (TarArchiveInputStream) new
ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
- TarArchiveOutputStream tos = new TarArchiveOutputStream(new
FileOutputStream(source));
- tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
- tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
+ try (FileInputStream fis = new FileInputStream(tmpTar)) {
+ try (TarArchiveInputStream tin = (TarArchiveInputStream) new
ArchiveStreamFactory()
+ .createArchiveInputStream(ArchiveStreamFactory.TAR, fis)) {
+ try (TarArchiveOutputStream tos = new
TarArchiveOutputStream(new FileOutputStream(source))) {
+ tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
+
tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
- // copy the existing entries
- ArchiveEntry nextEntry;
- while ((nextEntry = tin.getNextEntry()) != null) {
- tos.putArchiveEntry(nextEntry);
- IOUtils.copy(tin, tos);
- tos.closeArchiveEntry();
+ // copy the existing entries
+ copyExistingEntries(tin, tos);
+
+ // Create new entry
+ createNewEntry(entryName, buffer, length, tos);
+ }
+ }
}
+ LOG.trace("Deleting temporary file: {}", tmpTar);
+ FileUtil.deleteFile(tmpTar);
+ }
- // Create new entry
+ private void createNewEntry(String entryName, byte[] buffer, int length,
TarArchiveOutputStream tos) throws IOException {
TarArchiveEntry entry = new TarArchiveEntry(entryName);
entry.setSize(length);
tos.putArchiveEntry(entry);
tos.write(buffer, 0, length);
tos.closeArchiveEntry();
-
- IOHelper.close(fis, tin, tos);
- LOG.trace("Deleting temporary file: {}", tmpTar);
- FileUtil.deleteFile(tmpTar);
}
/**