Skip to content

Commit

Permalink
Same fix in camel-zipfile as we recently did in camel-tarfile.
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Mar 24, 2016
1 parent 3680450 commit d7e7688
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.dataformat.zipfile;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -40,8 +41,8 @@ public class ZipIterator implements Iterator<Message>, Closeable {
static final Logger LOGGER = LoggerFactory.getLogger(ZipIterator.class);

private final Message inputMessage;
private ZipInputStream zipInputStream;
private Message parent;
private volatile ZipInputStream zipInputStream;
private volatile Message parent;

public ZipIterator(Message inputMessage) {
this.inputMessage = inputMessage;
Expand All @@ -64,12 +65,12 @@ public boolean hasNext() {
if (!availableDataInCurrentEntry) {
// advance to the next entry.
parent = getNextElement();
// check if there are more data.
availableDataInCurrentEntry = zipInputStream.available() == 1;
// if there are not more data, close the stream.
if (!availableDataInCurrentEntry) {
if (parent == null) {
zipInputStream.close();
}
availableDataInCurrentEntry = false;
} else {
availableDataInCurrentEntry = true;
}
}
return availableDataInCurrentEntry;
} catch (IOException exception) {
Expand All @@ -91,30 +92,34 @@ public Message next() {
}

private Message getNextElement() {
Message answer = null;
if (zipInputStream == null) {
return null;
}

if (zipInputStream != null) {
try {
ZipEntry current = getNextEntry();
try {
ZipEntry current = getNextEntry();

if (current != null) {
LOGGER.debug("read zipEntry {}", current.getName());
answer = new DefaultMessage();
answer.getHeaders().putAll(inputMessage.getHeaders());
answer.setHeader("zipFileName", current.getName());
answer.setHeader(Exchange.FILE_NAME, current.getName());
if (current != null) {
LOGGER.debug("read zipEntry {}", current.getName());
Message answer = new DefaultMessage();
answer.getHeaders().putAll(inputMessage.getHeaders());
answer.setHeader("zipFileName", current.getName());
answer.setHeader(Exchange.FILE_NAME, current.getName());
if (current.getSize() > 0) {
answer.setBody(new ZipInputStreamWrapper(zipInputStream));
return answer;
} else {
LOGGER.trace("close zipInputStream");
// Workaround for the case when the entry is zero bytes big
answer.setBody(new ByteArrayInputStream(new byte[0]));
}
} catch (IOException exception) {
//Just wrap the IOException as CamelRuntimeException
throw new RuntimeCamelException(exception);
return answer;
} else {
LOGGER.trace("close zipInputStream");
return null;
}
}

return answer;
} catch (IOException exception) {
//Just wrap the IOException as CamelRuntimeException
throw new RuntimeCamelException(exception);
}
}

public void checkNullAnswer(Message answer) {
Expand Down Expand Up @@ -143,8 +148,7 @@ public void remove() {

@Override
public void close() throws IOException {
if (zipInputStream != null) {
zipInputStream.close();
}
IOHelper.close(zipInputStream);
zipInputStream = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public void setUp() throws Exception {

@Test
public void testSplitter() throws Exception {
getMockEndpoint("mock:entry").expectedMessageCount(3);
getMockEndpoint("mock:entry").expectedMessageCount(2);

template.sendBody("seda:decompressFiles", new File("src/test/resources/data.zip"));
template.sendBody("direct:decompressFiles", new File("src/test/resources/data.zip"));

assertMockEndpointsSatisfied();
}
Expand All @@ -43,11 +43,11 @@ public void testSplitter() throws Exception {
public void testSplitterWithWrongFile() throws Exception {
getMockEndpoint("mock:entry").expectedMessageCount(0);
getMockEndpoint("mock:errors").expectedMessageCount(1);

//Send a file which is not exit
template.sendBody("seda:decompressFiles", new File("src/test/resources/data"));
template.sendBody("direct:decompressFiles", new File("src/test/resources/data"));

assertMockEndpointsSatisfied();

}

@Override
Expand All @@ -57,11 +57,10 @@ protected RouteBuilder createRouteBuilder() throws Exception {
public void configure() throws Exception {
errorHandler(deadLetterChannel("mock:errors"));

from("seda:decompressFiles")
from("direct:decompressFiles")
.split(new ZipSplitter()).streaming().shareUnitOfWork()
.log("we are splitting")
.to("log:entry")
.to("mock:entry");
//.to("file:target/zip/?fileName=decompressed.txt&fileExist=Append");
}
};
}
Expand Down

0 comments on commit d7e7688

Please sign in to comment.