[camel] branch camel-2.22.x updated: CAMEL-12724: camel-ftp - SftpOperations should set byte[] instead of OutputStream for exchange file body (#2463)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch camel-2.22.x updated: CAMEL-12724: camel-ftp - SftpOperations should set byte[] instead of OutputStream for exchange file body (#2463)

davsclaus-2
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-2.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.22.x by this push:
     new c7ec8ef  CAMEL-12724: camel-ftp - SftpOperations should set byte[] instead of OutputStream for exchange file body (#2463)
c7ec8ef is described below

commit c7ec8ef0c195c9f5febd5902a9424c0ee3c31021
Author: Tadayoshi Sato <[hidden email]>
AuthorDate: Fri Aug 10 09:31:55 2018 +0200

    CAMEL-12724: camel-ftp - SftpOperations should set byte[] instead of OutputStream for exchange file body (#2463)
---
 .../camel/component/file/remote/SftpOperations.java      | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index ff82e72..1f58ea8 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -686,7 +686,6 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
 
     @SuppressWarnings("unchecked")
     private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException {
-        OutputStream os = null;
         String currentDir = null;
         try {
             GenericFile<ChannelSftp.LsEntry> target =
@@ -715,18 +714,19 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
                 target.setBody(is);
                 exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is);
             } else {
-                os = new ByteArrayOutputStream();
-                target.setBody(os);
-                IOHelper.copyAndCloseInput(is, os);
+                // read the entire file into memory in the byte array
+                ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                IOHelper.copyAndCloseInput(is, bos);
+                // close the stream after done
+                IOHelper.close(bos);
+
+                target.setBody(bos.toByteArray());
             }
 
             return true;
-        } catch (IOException e) {
-            throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
-        } catch (SftpException e) {
+        } catch (IOException | SftpException e) {
             throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
         } finally {
-            IOHelper.close(os, "retrieve: " + name, LOG);
             // change back to current directory if we changed directory
             if (currentDir != null) {
                 changeCurrentDirectory(currentDir);