CAMEL-10007: camel-tarfile - TarAggregationStrategy should ignore zero byte files. Also close resources so works better on windows.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3628697f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3628697f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3628697f Branch: refs/heads/camel-2.17.x Commit: 3628697f3ee50957a946a8172b1da9fdf12657ea Parents: 71c681b Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Jun 1 15:30:43 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Jun 1 15:31:38 2016 +0200 ---------------------------------------------------------------------- .../tarfile/TarAggregationStrategy.java | 36 ++++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3628697f/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java ---------------------------------------------------------------------- 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 5394076..d79ce1a 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 @@ -21,8 +21,10 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import org.apache.camel.Exchange; +import org.apache.camel.WrappedFile; import org.apache.camel.component.file.FileConsumer; import org.apache.camel.component.file.GenericFile; import org.apache.camel.component.file.GenericFileMessage; @@ -151,18 +153,21 @@ public class TarAggregationStrategy implements AggregationStrategy { tarFile = oldExchange.getIn().getBody(File.class); } - // Handle GenericFileMessages - if (GenericFileMessage.class.isAssignableFrom(newExchange.getIn().getClass())) { + Object body = newExchange.getIn().getBody(); + if (body instanceof WrappedFile) { + body = ((WrappedFile) body).getFile(); + } + + if (body instanceof File) { try { - File appendFile = newExchange.getIn().getMandatoryBody(File.class); - if (appendFile != null) { - addFileToTar(tarFile, appendFile, this.preserveFolderStructure ? newExchange.getIn().toString() : null); + File appendFile = (File) body; + // do not try to append empty files + if (appendFile.length() > 0) { + String entryName = preserveFolderStructure ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId(); + addFileToTar(tarFile, appendFile, this.preserveFolderStructure ? entryName : null); GenericFile<File> genericFile = FileConsumer.asGenericFile( - tarFile.getParent(), - tarFile, - null, // Do not set charset here, that will cause the tar file to be handled as ASCII later which breaks it.. - false); + tarFile.getParent(), tarFile, Charset.defaultCharset().toString(), false); genericFile.bindToExchange(answer); } } catch (Exception e) { @@ -172,15 +177,18 @@ public class TarAggregationStrategy implements AggregationStrategy { // Handle all other messages try { byte[] buffer = newExchange.getIn().getMandatoryBody(byte[].class); - String entryName = useFilenameHeader ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId(); - addEntryToTar(tarFile, entryName, buffer, buffer.length); - GenericFile<File> genericFile = FileConsumer.asGenericFile(tarFile.getParent(), tarFile, null, false); - genericFile.bindToExchange(answer); + // do not try to append empty data + if (buffer.length > 0) { + String entryName = useFilenameHeader ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId(); + addEntryToTar(tarFile, entryName, buffer, buffer.length); + GenericFile<File> genericFile = FileConsumer.asGenericFile( + tarFile.getParent(), tarFile, Charset.defaultCharset().toString(), false); + genericFile.bindToExchange(answer); + } } catch (Exception e) { throw new GenericFileOperationFailedException(e.getMessage(), e); } } - return answer; }