Downloaded FTP file should be easy to read as an InputStream instead of OutputStream.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9ffbfc48 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9ffbfc48 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9ffbfc48 Branch: refs/heads/ftpfix Commit: 9ffbfc489b2e4a2cd2d4f1b42c51b766fc570573 Parents: 4d4b5bc Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Sep 7 09:42:12 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Sep 7 09:42:12 2016 +0200 ---------------------------------------------------------------------- .../camel/component/file/remote/FtpOperations.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/9ffbfc48/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java index cfc72f7..db58a9c 100644 --- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java +++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java @@ -325,7 +325,7 @@ public class FtpOperations implements RemoteFileOperations<FTPFile> { if (is != null) { try { - is.close(); + IOHelper.close(is); client.completePendingCommand(); } catch (IOException e) { throw new GenericFileOperationFailedException(e.getMessage(), e); @@ -335,7 +335,6 @@ public class FtpOperations implements RemoteFileOperations<FTPFile> { @SuppressWarnings("unchecked") private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException { - OutputStream os = null; boolean result; try { GenericFile<FTPFile> target = (GenericFile<FTPFile>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE); @@ -365,9 +364,15 @@ public class FtpOperations implements RemoteFileOperations<FTPFile> { exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is); result = true; } else { - os = new ByteArrayOutputStream(); - target.setBody(os); - result = client.retrieveFile(remoteName, os); + // read the entire file into memory in the byte array + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + result = client.retrieveFile(remoteName, bos); + // close the stream after done + IOHelper.close(bos); + + // and then set the body as an input stream so we can read the content easily + InputStream is = new ByteArrayInputStream(bos.toByteArray()); + target.setBody(is); } // store client reply information after the operation @@ -381,8 +386,6 @@ public class FtpOperations implements RemoteFileOperations<FTPFile> { } catch (IOException e) { throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e); - } finally { - IOHelper.close(os, "retrieve: " + name, log); } return result;