You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2015/03/02 13:08:53 UTC

[2/4] mina-sshd git commit: [SSHD-424] Avoid inadvertent closure of the input stream used by ScpHelper#receiveFile

[SSHD-424] Avoid inadvertent closure of the input stream used by ScpHelper#receiveFile

Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/687e7712
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/687e7712
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/687e7712

Branch: refs/heads/master
Commit: 687e77122cdaf57d4c19a248e2d97a87f0ce6294
Parents: 74f6b6c
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Mar 2 10:43:19 2015 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Mar 2 10:43:19 2015 +0100

----------------------------------------------------------------------
 .../org/apache/sshd/common/scp/ScpHelper.java   | 104 +++++++++++--------
 1 file changed, 59 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/687e7712/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
index 709de73..d73be2a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
@@ -255,51 +255,9 @@ public class ScpHelper {
         } else if (Files.exists(file) && !Files.isWritable(file)) {
             throw new IOException("Can not write to file: " + file);
         }
-        InputStream is = new FilterInputStream(in) {
-            long remaining = length;
-            @Override
-            public int read() throws IOException {
-                if (remaining > 0) {
-                    remaining--;
-                    return super.read();
-                } else{
-                    return -1;
-                }
-            }
-
-            @Override
-            public int read(byte[] b, int off, int len) throws IOException {
-                int nb = len;
-                if (nb > remaining) {
-                    nb = (int) remaining;
-                }
-                if (nb > 0) {
-                    int read = super.read(b, off, nb);
-                    remaining -= read;
-                    return read;
-                } else {
-                    return -1;
-                }
-            }
-
-            @Override
-            public long skip(long n) throws IOException {
-                long skipped = super.skip(n);
-                remaining -= skipped;
-                return skipped;
-            }
-
-            @Override
-            public int available() throws IOException {
-                int av = super.available();
-                if (av > remaining) {
-                    return (int) remaining;
-                } else {
-                    return av;
-                }
-            }
-        };
-        try (OutputStream os = Files.newOutputStream(file)) {
+        
+        try (InputStream is = new LimitInputStream(this.in, length);
+             OutputStream os = Files.newOutputStream(file)) {
             ack();
             IoUtils.copy(is, os, bufSize);
         }
@@ -625,4 +583,60 @@ public class ScpHelper {
         return c;
     }
 
+    private static class LimitInputStream extends FilterInputStream {
+
+        private long remaining;
+
+        public LimitInputStream(InputStream in, long length) {
+            super(in);
+            remaining = length;
+        }
+
+        @Override
+        public int read() throws IOException {
+            if (remaining > 0) {
+                remaining--;
+                return super.read();
+            } else{
+                return -1;
+            }
+        }
+
+        @Override
+        public int read(byte[] b, int off, int len) throws IOException {
+            int nb = len;
+            if (nb > remaining) {
+                nb = (int) remaining;
+            }
+            if (nb > 0) {
+                int read = super.read(b, off, nb);
+                remaining -= read;
+                return read;
+            } else {
+                return -1;
+            }
+        }
+
+        @Override
+        public long skip(long n) throws IOException {
+            long skipped = super.skip(n);
+            remaining -= skipped;
+            return skipped;
+        }
+
+        @Override
+        public int available() throws IOException {
+            int av = super.available();
+            if (av > remaining) {
+                return (int) remaining;
+            } else {
+                return av;
+            }
+        }
+
+        @Override
+        public void close() throws IOException {
+            // do not close the original input stream since it serves for ACK(s)
+        }
+    }
 }