Revert "CAMEL-9375: TarSplitter includes one extra empty entry at the end."
This reverts commit 929d437cc2dd73b284d38485adf1931c4700b9c1. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0cf67e5b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0cf67e5b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0cf67e5b Branch: refs/heads/camel-2.17.x Commit: 0cf67e5b0789cd01ddc0b74d865cd7050b3bcac7 Parents: eabcbc6 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Mar 22 13:12:57 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Mar 22 14:18:23 2016 +0100 ---------------------------------------------------------------------- .../dataformat/tarfile/TarFileDataFormat.java | 2 +- .../camel/dataformat/tarfile/TarIterator.java | 84 ++++++++++---------- .../camel/dataformat/tarfile/TarSplitter.java | 2 +- 3 files changed, 46 insertions(+), 42 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0cf67e5b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java index 49b1a82..5465edc 100644 --- a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java +++ b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java @@ -86,7 +86,7 @@ public class TarFileDataFormat extends ServiceSupport implements DataFormat, Dat @Override public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { if (usingIterator) { - return new TarIterator(exchange, stream); + return new TarIterator(exchange.getIn(), stream); } else { BufferedInputStream bis = new BufferedInputStream(stream); TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, bis); http://git-wip-us.apache.org/repos/asf/camel/blob/0cf67e5b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java index c5c85ce..91933c0 100644 --- a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java +++ b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java @@ -26,7 +26,6 @@ import java.util.Iterator; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.RuntimeCamelException; -import org.apache.camel.StreamCache; import org.apache.camel.impl.DefaultMessage; import org.apache.camel.util.IOHelper; import org.apache.commons.compress.archivers.ArchiveException; @@ -52,13 +51,11 @@ public class TarIterator implements Iterator<Message>, Closeable { private final Message inputMessage; private TarArchiveInputStream tarInputStream; - private Message nextMessage; + private Message parent; - private Exchange exchange; - - public TarIterator(Exchange exchange, InputStream inputStream) { - this.exchange = exchange; - this.inputMessage = exchange.getIn(); + public TarIterator(Message inputMessage, InputStream inputStream) { + this.inputMessage = inputMessage; + //InputStream inputStream = inputMessage.getBody(InputStream.class); if (inputStream instanceof TarArchiveInputStream) { tarInputStream = (TarArchiveInputStream) inputStream; @@ -70,38 +67,47 @@ public class TarIterator implements Iterator<Message>, Closeable { throw new RuntimeException(e.getMessage(), e); } } - nextMessage = null; + parent = null; } @Override public boolean hasNext() { - tryAdvanceToNext(); - - return this.nextMessage != null; + try { + if (tarInputStream == null) { + return false; + } + boolean availableDataInCurrentEntry = tarInputStream.available() > 0; + if (!availableDataInCurrentEntry) { + // advance to the next entry. + parent = getNextElement(); + if (parent == null) { + tarInputStream.close(); + availableDataInCurrentEntry = false; + } else { + availableDataInCurrentEntry = true; + } + } + return availableDataInCurrentEntry; + } catch (IOException exception) { + //Just wrap the IOException as CamelRuntimeException + throw new RuntimeCamelException(exception); + } } @Override public Message next() { - tryAdvanceToNext(); - - //consume element - Message next = this.nextMessage; - this.nextMessage = null; - return next; - } - - - private void tryAdvanceToNext() { - //return current next - if (this.nextMessage != null) { - return; + if (parent == null) { + parent = getNextElement(); } - this.nextMessage = createNextMessage(); - checkNullAnswer(this.nextMessage); + Message answer = parent; + parent = null; + checkNullAnswer(answer); + + return answer; } - private Message createNextMessage() { + private Message getNextElement() { if (tarInputStream == null) { return null; } @@ -116,10 +122,7 @@ public class TarIterator implements Iterator<Message>, Closeable { answer.setHeader(TARFILE_ENTRY_NAME_HEADER, current.getName()); answer.setHeader(Exchange.FILE_NAME, current.getName()); if (current.getSize() > 0) { - //Have to cache current entry's portion of tarInputStream here, because getNextTarEntry - //advances tarInputStream beyond current entry - answer.setBody(exchange.getContext().getTypeConverter().mandatoryConvertTo(StreamCache.class, exchange, - new TarElementInputStreamWrapper(tarInputStream))); + answer.setBody(new TarElementInputStreamWrapper(tarInputStream)); } else { // Workaround for the case when the entry is zero bytes big answer.setBody(new ByteArrayInputStream(new byte[0])); @@ -129,16 +132,16 @@ public class TarIterator implements Iterator<Message>, Closeable { LOGGER.trace("Closed tarInputStream"); return null; } - } catch (Exception exception) { - this.close(); - //Just wrap the Exception as CamelRuntimeException + } catch (IOException exception) { + //Just wrap the IOException as CamelRuntimeException throw new RuntimeCamelException(exception); } } public void checkNullAnswer(Message answer) { - if (answer == null) { - this.close(); + if (answer == null && tarInputStream != null) { + IOHelper.close(tarInputStream); + tarInputStream = null; } } @@ -160,9 +163,10 @@ public class TarIterator implements Iterator<Message>, Closeable { } @Override - public void close() { - //suppress any exceptions from closing - IOHelper.close(tarInputStream); - tarInputStream = null; + public void close() throws IOException { + if (tarInputStream != null) { + tarInputStream.close(); + tarInputStream = null; + } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0cf67e5b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java index 3ee5371..132dd55 100644 --- a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java +++ b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java @@ -33,7 +33,7 @@ public class TarSplitter implements Expression { public Object evaluate(Exchange exchange) { Message inputMessage = exchange.getIn(); - return new TarIterator(exchange, inputMessage.getBody(InputStream.class)); + return new TarIterator(inputMessage, inputMessage.getBody(InputStream.class)); } @Override