Repository: mina-sshd
Updated Branches:
  refs/heads/master f80467b7b -> 6068e0b0b


[SSHD-477] Allow user control over size of copy buffer(s) used in 
DefaultSftpClient


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

Branch: refs/heads/master
Commit: 6068e0b0bb48a3eb3fa478965b6bcac05aa215fa
Parents: f80467b
Author: Lyor Goldstein <[email protected]>
Authored: Sun May 31 12:05:53 2015 +0300
Committer: Lyor Goldstein <[email protected]>
Committed: Sun May 31 12:05:53 2015 +0300

----------------------------------------------------------------------
 .../java/org/apache/sshd/ClientSession.java     |  1 +
 .../sshd/client/session/ClientSessionImpl.java  | 10 ++-
 .../sshd/client/sftp/AbstractSftpClient.java    | 43 +++++++++--
 .../sshd/client/sftp/DefaultSftpClient.java     | 16 +++-
 .../org/apache/sshd/client/sftp/SftpClient.java | 10 +++
 .../sshd/client/sftp/SftpFileChannel.java       | 10 +--
 .../apache/sshd/client/sftp/SftpFileSystem.java | 61 +++++++++++++--
 .../client/sftp/SftpFileSystemProvider.java     |  5 ++
 .../apache/sshd/common/FactoryManagerUtils.java | 79 +++++++++++---------
 .../sshd/client/sftp/SftpFileSystemTest.java    | 26 ++++++-
 .../org/apache/sshd/client/sftp/SftpTest.java   |  9 ++-
 11 files changed, 208 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/ClientSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/ClientSession.java 
b/sshd-core/src/main/java/org/apache/sshd/ClientSession.java
index 0f43cf1..e340210 100644
--- a/sshd-core/src/main/java/org/apache/sshd/ClientSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/ClientSession.java
@@ -154,6 +154,7 @@ public interface ClientSession extends Session {
     SftpClient createSftpClient() throws IOException;
 
     FileSystem createSftpFileSystem() throws IOException;
+    FileSystem createSftpFileSystem(int readBufferSize, int writeBufferSize) 
throws IOException;
 
     /**
      * Start forwarding the given local address on the client to the given 
address on the server.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java 
b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
index 09286d8..806a160 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
@@ -286,7 +286,15 @@ public class ClientSessionImpl extends AbstractSession 
implements ClientSession
 
     @Override
     public FileSystem createSftpFileSystem() throws IOException {
-        return new SftpFileSystem(new 
SftpFileSystemProvider((org.apache.sshd.SshClient) factoryManager), this);
+        return createSftpFileSystem(SftpClient.DEFAULT_READ_BUFFER_SIZE, 
SftpClient.DEFAULT_WRITE_BUFFER_SIZE);
+    }
+
+    @Override
+    public FileSystem createSftpFileSystem(int readBufferSize, int 
writeBufferSize) throws IOException {
+        SftpFileSystem  fs=new SftpFileSystem(new 
SftpFileSystemProvider((org.apache.sshd.SshClient) factoryManager), this);
+        fs.setReadBufferSize(readBufferSize);
+        fs.setWriteBufferSize(writeBufferSize);
+        return fs;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/client/sftp/AbstractSftpClient.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/sftp/AbstractSftpClient.java 
b/sshd-core/src/main/java/org/apache/sshd/client/sftp/AbstractSftpClient.java
index b1533c1..b532b9d 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/sftp/AbstractSftpClient.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/sftp/AbstractSftpClient.java
@@ -22,6 +22,7 @@ package org.apache.sshd.client.sftp;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumSet;
 
@@ -57,13 +58,28 @@ public abstract class AbstractSftpClient extends 
AbstractLoggingBean implements
     }
 
     @Override
-    public InputStream read(final String path) throws IOException {
-        return read(path, EnumSet.of(OpenMode.Read));
+    public InputStream read(String path) throws IOException {
+        return read(path, DEFAULT_READ_BUFFER_SIZE);
+    }
+
+    @Override
+    public InputStream read(String path, int bufferSize) throws IOException {
+        return read(path, bufferSize, EnumSet.of(OpenMode.Read));
     }
 
     @Override
     public InputStream read(String path, OpenMode ... mode) throws IOException 
{
-        return read(path, GenericUtils.of(mode));
+        return read(path, DEFAULT_READ_BUFFER_SIZE, mode);
+    }
+
+    @Override
+    public InputStream read(String path, int bufferSize, OpenMode ... mode) 
throws IOException {
+        return read(path, bufferSize, GenericUtils.of(mode));
+    }
+
+    @Override
+    public InputStream read(String path, Collection<OpenMode>  mode) throws 
IOException {
+        return read(path, DEFAULT_READ_BUFFER_SIZE, mode);
     }
 
     @Override
@@ -72,13 +88,28 @@ public abstract class AbstractSftpClient extends 
AbstractLoggingBean implements
     }
 
     @Override
-    public OutputStream write(final String path) throws IOException {
-        return write(path, EnumSet.of(OpenMode.Write, OpenMode.Create, 
OpenMode.Truncate));
+    public OutputStream write(String path) throws IOException {
+        return write(path, DEFAULT_WRITE_BUFFER_SIZE);
+    }
+
+    @Override
+    public OutputStream write(String path, int bufferSize) throws IOException {
+        return write(path, bufferSize, EnumSet.of(OpenMode.Write, 
OpenMode.Create, OpenMode.Truncate));
     }
 
     @Override
     public OutputStream write(String path, OpenMode ... mode) throws 
IOException {
-        return write(path, GenericUtils.of(mode));
+        return write(path, DEFAULT_WRITE_BUFFER_SIZE, mode);
+    }
+
+    @Override
+    public OutputStream write(String path, Collection<OpenMode> mode) throws 
IOException {
+        return write(path, DEFAULT_WRITE_BUFFER_SIZE, mode);
+    }
+
+    @Override
+    public OutputStream write(String path, int bufferSize, OpenMode ... mode) 
throws IOException {
+        return write(path, bufferSize, GenericUtils.of(mode));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java 
b/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java
index ebf80ed..c934cd8 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java
@@ -1025,10 +1025,14 @@ public class DefaultSftpClient extends 
AbstractSftpClient {
     }
 
     @Override
-    public InputStream read(final String path, final Collection<OpenMode> 
mode) throws IOException {
+    public InputStream read(final String path, final int bufferSize, final 
Collection<OpenMode> mode) throws IOException {
+        if (bufferSize < MIN_READ_BUFFER_SIZE) {
+            throw new IllegalArgumentException("Insufficient read buffer size: 
" + bufferSize + ", min.=" + MIN_READ_BUFFER_SIZE);
+        }
+
         return new InputStreamWithChannel() {
             private byte[] bb = new byte[1];
-            private byte[] buffer = new byte[32 * 1024];
+            private byte[] buffer = new byte[bufferSize];
             private int index;
             private int available;
             private CloseableHandle handle = DefaultSftpClient.this.open(path, 
mode);
@@ -1095,10 +1099,14 @@ public class DefaultSftpClient extends 
AbstractSftpClient {
     }
 
     @Override
-    public OutputStream write(final String path, final Collection<OpenMode> 
mode) throws IOException {
+    public OutputStream write(final String path, final int bufferSize, final 
Collection<OpenMode> mode) throws IOException {
+        if (bufferSize < MIN_WRITE_BUFFER_SIZE) {
+            throw new IllegalArgumentException("Insufficient write buffer 
size: " + bufferSize + ", min.=" + MIN_WRITE_BUFFER_SIZE);
+        }
+
         return new OutputStreamWithChannel() {
             private byte[] bb = new byte[1];
-            private byte[] buffer = new byte[32 * 1024];
+            private byte[] buffer = new byte[bufferSize];
             private int index;
             private CloseableHandle handle = DefaultSftpClient.this.open(path, 
mode);
             private long offset;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpClient.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpClient.java 
b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpClient.java
index 75483b7..ada3f48 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpClient.java
@@ -278,12 +278,22 @@ public interface SftpClient extends Closeable {
 
     Iterable<DirEntry> readDir(String path) throws IOException;
 
+    // default values used if none specified
+    int MIN_BUFFER_SIZE=Byte.MAX_VALUE, MIN_READ_BUFFER_SIZE=MIN_BUFFER_SIZE, 
MIN_WRITE_BUFFER_SIZE=MIN_BUFFER_SIZE;
+    int IO_BUFFER_SIZE=32 * 1024, DEFAULT_READ_BUFFER_SIZE=IO_BUFFER_SIZE, 
DEFAULT_WRITE_BUFFER_SIZE=IO_BUFFER_SIZE;
+
     InputStream read(String path) throws IOException;
+    InputStream read(String path, int bufferSize) throws IOException;
     InputStream read(String path, OpenMode ... mode) throws IOException;
+    InputStream read(String path, int bufferSize, OpenMode ... mode) throws 
IOException;
     InputStream read(String path, Collection<OpenMode> mode) throws 
IOException;
+    InputStream read(String path, int bufferSize, Collection<OpenMode> mode) 
throws IOException;
 
     OutputStream write(String path) throws IOException;
+    OutputStream write(String path, int bufferSize) throws IOException;
     OutputStream write(String path, OpenMode ... mode) throws IOException;
+    OutputStream write(String path, int bufferSize, OpenMode ... mode) throws 
IOException;
     OutputStream write(String path, Collection<OpenMode> mode) throws 
IOException;
+    OutputStream write(String path, int bufferSize, Collection<OpenMode> mode) 
throws IOException;
 
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileChannel.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileChannel.java 
b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileChannel.java
index ddb8663..0d4f826 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileChannel.java
@@ -49,17 +49,17 @@ public class SftpFileChannel extends FileChannel {
     private final Collection<SftpClient.OpenMode> modes;
     private final SftpClient sftp;
     private final SftpClient.CloseableHandle handle;
-    private final Object lock;
+    private final Object lock = new Object();
     private volatile long pos;
     private volatile Thread blockingThread;
 
     public SftpFileChannel(SftpPath p, Collection<SftpClient.OpenMode> modes) 
throws IOException {
-        this.p = p;
+        this.p = ValidateUtils.checkNotNull(p, "No target path", 
GenericUtils.EMPTY_OBJECT_ARRAY);
         this.modes = ValidateUtils.checkNotNull(modes, "No channel modes 
specified", GenericUtils.EMPTY_OBJECT_ARRAY);
-        sftp = p.getFileSystem().getClient();
+        
+        SftpFileSystem  fs=p.getFileSystem();
+        sftp = fs.getClient();
         handle = sftp.open(p.toString(), modes);
-        lock = new Object();
-        pos = 0;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java 
b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java
index 820099c..de861af 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java
@@ -43,6 +43,8 @@ public class SftpFileSystem extends BaseFileSystem<SftpPath> {
     private final Queue<SftpClient> pool;
     private final ThreadLocal<Wrapper> wrappers = new ThreadLocal<>();
     private SftpPath defaultDir;
+    private int readBufferSize = SftpClient.DEFAULT_READ_BUFFER_SIZE;
+    private int writeBufferSize = SftpClient.DEFAULT_WRITE_BUFFER_SIZE;
 
     public SftpFileSystem(SftpFileSystemProvider provider, ClientSession 
session) throws IOException {
         super(provider);
@@ -53,6 +55,30 @@ public class SftpFileSystem extends BaseFileSystem<SftpPath> 
{
         }
     }
 
+    public int getReadBufferSize() {
+        return readBufferSize;
+    }
+
+    public void setReadBufferSize(int size) {
+        if (size < SftpClient.MIN_READ_BUFFER_SIZE) {
+            throw new IllegalArgumentException("Insufficient read buffer size: 
" + size + ", min.=" + SftpClient.MIN_READ_BUFFER_SIZE);
+        }
+
+        readBufferSize = size;
+    }
+
+    public int getWriteBufferSize() {
+        return writeBufferSize;
+    }
+
+    public void setWriteBufferSize(int size) {
+        if (size < SftpClient.MIN_WRITE_BUFFER_SIZE) {
+            throw new IllegalArgumentException("Insufficient write buffer 
size: " + size + ", min.=" + SftpClient.MIN_WRITE_BUFFER_SIZE);
+        }
+
+        writeBufferSize = size;
+    }
+
     @Override
     protected SftpPath create(String root, ImmutableList<String> names) {
         return new SftpPath(this, root, names);
@@ -72,7 +98,7 @@ public class SftpFileSystem extends BaseFileSystem<SftpPath> {
                     client = session.createSftpClient();
                 }
                 if (!client.isClosing()) {
-                    wrapper = new Wrapper(client);
+                    wrapper = new Wrapper(client, getReadBufferSize(), 
getWriteBufferSize());
                 }
             }
             wrappers.set(wrapper);
@@ -115,9 +141,12 @@ public class SftpFileSystem extends 
BaseFileSystem<SftpPath> {
 
         private final SftpClient delegate;
         private final AtomicInteger count = new AtomicInteger(1);
+        private final int readSize, writeSize;
 
-        private Wrapper(SftpClient delegate) {
+        private Wrapper(SftpClient delegate, int readSize, int writeSize) {
             this.delegate = delegate;
+            this.readSize = readSize;
+            this.writeSize = writeSize;
         }
 
         @Override
@@ -242,22 +271,42 @@ public class SftpFileSystem extends 
BaseFileSystem<SftpPath> {
 
         @Override
         public InputStream read(String path) throws IOException {
-            return delegate.read(path);
+            return read(path, readSize);
+        }
+
+        @Override
+        public InputStream read(String path, OpenMode... mode) throws 
IOException {
+            return read(path, readSize, mode);
         }
 
         @Override
         public InputStream read(String path, Collection<OpenMode> mode) throws 
IOException {
-            return delegate.read(path, mode);
+            return read(path, readSize, mode);
+        }
+
+        @Override
+        public InputStream read(String path, int bufferSize, 
Collection<OpenMode> mode) throws IOException {
+            return delegate.read(path, bufferSize, mode);
         }
 
         @Override
         public OutputStream write(String path) throws IOException {
-            return delegate.write(path);
+            return write(path, writeSize);
+        }
+
+        @Override
+        public OutputStream write(String path, OpenMode... mode) throws 
IOException {
+            return write(path, writeSize, mode);
         }
 
         @Override
         public OutputStream write(String path, Collection<OpenMode> mode) 
throws IOException {
-            return delegate.write(path, mode);
+            return write(path, writeSize, mode);
+        }
+
+        @Override
+        public OutputStream write(String path, int bufferSize, 
Collection<OpenMode> mode) throws IOException {
+            return delegate.write(path, bufferSize, mode);
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystemProvider.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystemProvider.java
 
b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystemProvider.java
index b1f567b..99d7b5a 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystemProvider.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystemProvider.java
@@ -77,6 +77,7 @@ import org.apache.sshd.SshBuilder;
 import org.apache.sshd.SshClient;
 import org.apache.sshd.client.SftpException;
 import org.apache.sshd.client.sftp.SftpClient.Attributes;
+import org.apache.sshd.common.FactoryManagerUtils;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.config.SshConfigFileReader;
 import org.apache.sshd.common.sftp.SftpConstants;
@@ -87,6 +88,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class SftpFileSystemProvider extends FileSystemProvider {
+    public static final String  READ_BUFFER_PROP_NAME = "read-buffer-size", 
WRITE_BUFFER_PROP_NAME="write-buffer-size";
+
     private final SshClient client;
     private final Map<String, SftpFileSystem> fileSystems = new 
HashMap<String, SftpFileSystem>();
     protected final Logger log;
@@ -132,6 +135,8 @@ public class SftpFileSystemProvider extends 
FileSystemProvider {
                 session.addPasswordIdentity(ui[1]);
                 session.auth().verify();
                 fileSystem = new SftpFileSystem(this, session);
+                
fileSystem.setReadBufferSize(FactoryManagerUtils.getIntProperty(env, 
READ_BUFFER_PROP_NAME, SftpClient.DEFAULT_READ_BUFFER_SIZE));
+                
fileSystem.setWriteBufferSize(FactoryManagerUtils.getIntProperty(env, 
WRITE_BUFFER_PROP_NAME, SftpClient.DEFAULT_WRITE_BUFFER_SIZE));
                 fileSystems.put(authority, fileSystem);
                 return fileSystem;
             } catch(Exception e) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java 
b/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java
index ece21bc..bab4bc0 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java
@@ -60,12 +60,14 @@ public class FactoryManagerUtils {
      * @return The resolved property
      * @throws NumberFormatException if malformed value
      */
-    public static final long getLongProperty(Map<String, String> props, String 
name, long defaultValue) {
-        String value = getStringProperty(props, name, null);
-        if (GenericUtils.isEmpty(value)) {
+    public static final long getLongProperty(Map<String,?> props, String name, 
long defaultValue) {
+        Object value = GenericUtils.isEmpty(props) ? null : props.get(name);
+        if (value == null) {
             return defaultValue;
-        } else {
-            return Long.parseLong(value);
+        } else if (value instanceof Long) {
+            return ((Long) value).longValue();
+        } else {    // we parse the string in case it is not a valid long value
+            return Long.parseLong(value.toString());
         }
     }
 
@@ -96,12 +98,14 @@ public class FactoryManagerUtils {
      * empty string
      * @throws NumberFormatException if malformed value
      */
-    public static final Long getLong(Map<String, String> props, String name) {
-        String value = getStringProperty(props, name, null);
-        if (GenericUtils.isEmpty(value)) {
+    public static final Long getLong(Map<String,?> props, String name) {
+        Object value = GenericUtils.isEmpty(props) ? null : props.get(name);
+        if (value == null) {
             return null;
-        } else {
-            return Long.valueOf(value);
+        } else if (value instanceof Long) {
+            return (Long) value;
+        } else {    // we parse the string in case it is not a valid long value
+            return Long.valueOf(value.toString());
         }
     }
 
@@ -113,7 +117,7 @@ public class FactoryManagerUtils {
         return updateProperty(manager, name, Long.toString(value));
     }
 
-    public static final String updateProperty(Map<String, String> props, 
String name, long value) {
+    public static final String updateProperty(Map<String,String> props, String 
name, long value) {
         return updateProperty(props, name, Long.toString(value));
     }
 
@@ -125,12 +129,14 @@ public class FactoryManagerUtils {
         return getIntProperty(manager.getProperties(), name, defaultValue);
     }
 
-    public static final int getIntProperty(Map<String, String> props, String 
name, int defaultValue) {
-        String value = getStringProperty(props, name, null);
-        if ((value == null) || (value.length() <= 0)) {
+    public static final int getIntProperty(Map<String,?> props, String name, 
int defaultValue) {
+        Object value = GenericUtils.isEmpty(props) ? null : props.get(name);
+        if (value == null) {
             return defaultValue;
-        } else {
-            return Integer.parseInt(value);
+        } else if (value instanceof Integer) {
+            return ((Integer) value).intValue();
+        } else {    // we parse the string in case this is NOT an integer
+            return Integer.parseInt(value.toString());
         }
     }
 
@@ -142,12 +148,14 @@ public class FactoryManagerUtils {
         return getInteger(manager.getProperties(), name);
     }
 
-    public static final Integer getInteger(Map<String, String> props, String 
name) {
-        String value = getStringProperty(props, name, null);
-        if ((value == null) || (value.length() <= 0)) {
+    public static final Integer getInteger(Map<String,?> props, String name) {
+        Object value = GenericUtils.isEmpty(props) ? null : props.get(name);
+        if (value == null) {
             return null;
-        } else {
-            return Integer.valueOf(value);
+        } else if (value instanceof Integer) {
+            return (Integer) value;
+        } else {    // we parse the string in case this is NOT an integer
+            return Integer.valueOf(value.toString());
         }
     }
 
@@ -171,12 +179,12 @@ public class FactoryManagerUtils {
         return getBooleanProperty(manager.getProperties(), name, defaultValue);
     }
 
-    public static final boolean getBooleanProperty(Map<String, String> props, 
String name, boolean defaultValue) {
-        String value = getStringProperty(props, name, null);
-        if ((value == null) || (value.length() <= 0)) {
+    public static final boolean getBooleanProperty(Map<String,?> props, String 
name, boolean defaultValue) {
+        Boolean value = getBoolean(props, name);
+        if (value == null) {
             return defaultValue;
         } else {
-            return Boolean.parseBoolean(value);
+            return value.booleanValue();
         }
     }
 
@@ -188,12 +196,14 @@ public class FactoryManagerUtils {
         return getBoolean(manager.getProperties(), name);
     }
 
-    public static final Boolean getBoolean(Map<String, String> props, String 
name) {
-        String value = getStringProperty(props, name, null);
-        if (GenericUtils.isEmpty(value)) {
+    public static final Boolean getBoolean(Map<String,?> props, String name) {
+        Object  value = GenericUtils.isEmpty(props) ? null : props.get(name);
+        if (value == null) {
             return null;
+        } else if (value instanceof Boolean) {
+            return (Boolean) value;
         } else {
-            return Boolean.valueOf(value);
+            return Boolean.valueOf(value.toString());
         }
     }
 
@@ -225,16 +235,17 @@ public class FactoryManagerUtils {
         return getStringProperty(manager.getProperties(), name, defaultValue);
     }
 
-    public static final String getString(Map<String, String> props, String 
name) {
+    public static final String getString(Map<String,?> props, String name) {
         return getStringProperty(props, name, null);
     }
 
-    public static final String getStringProperty(Map<String, String> props, 
String name, String defaultValue) {
-        String value = GenericUtils.isEmpty(props) ? null : props.get(name);
-        if (GenericUtils.isEmpty(value)) {
+    public static final String getStringProperty(Map<String,?> props, String 
name, String defaultValue) {
+        Object value = GenericUtils.isEmpty(props) ? null : props.get(name);
+        String s = (value == null) ? null : value.toString();
+        if (GenericUtils.isEmpty(s)) {
             return defaultValue;
         } else {
-            return value;
+            return s;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpFileSystemTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpFileSystemTest.java 
b/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpFileSystemTest.java
index 1acb676..b7dc9a1 100644
--- 
a/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpFileSystemTest.java
+++ 
b/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpFileSystemTest.java
@@ -40,6 +40,7 @@ import java.nio.file.attribute.UserPrincipalNotFoundException;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Map;
+import java.util.TreeMap;
 
 import org.apache.sshd.SshServer;
 import org.apache.sshd.common.NamedFactory;
@@ -47,6 +48,7 @@ import org.apache.sshd.common.Session;
 import org.apache.sshd.common.file.FileSystemFactory;
 import org.apache.sshd.common.file.root.RootedFileSystemProvider;
 import org.apache.sshd.common.sftp.SftpConstants;
+import org.apache.sshd.common.util.IoUtils;
 import org.apache.sshd.common.util.OsUtils;
 import org.apache.sshd.server.Command;
 import org.apache.sshd.server.command.ScpCommandFactory;
@@ -103,7 +105,17 @@ public class SftpFileSystemTest extends BaseTestSupport {
         Path lclSftp = Utils.resolve(targetPath, 
SftpConstants.SFTP_SUBSYSTEM_NAME, getClass().getSimpleName());
         Utils.deleteRecursive(lclSftp);
 
-        try(FileSystem fs = 
FileSystems.newFileSystem(URI.create("sftp://x:x@localhost:"; + port + "/"), 
Collections.<String,Object>emptyMap())) {
+        try(FileSystem fs = FileSystems.newFileSystem(
+                URI.create("sftp://"; + getCurrentTestName() + ":" + 
getCurrentTestName() + "@localhost:" + port + "/"),
+                new TreeMap<String,Object>() {
+                    private static final long serialVersionUID = 1L;    // 
we're not serializing it
+                
+                    {
+                        put(SftpFileSystemProvider.READ_BUFFER_PROP_NAME, 
Integer.valueOf(IoUtils.DEFAULT_COPY_SIZE));
+                        put(SftpFileSystemProvider.WRITE_BUFFER_PROP_NAME, 
Integer.valueOf(IoUtils.DEFAULT_COPY_SIZE));
+                    }
+            })) {
+
             Iterable<Path> rootDirs = fs.getRootDirectories();
             for (Path root : rootDirs) {
                 String  rootName = root.toString();
@@ -212,7 +224,17 @@ public class SftpFileSystemTest extends BaseTestSupport {
         Path lclSftp = Utils.resolve(targetPath, 
SftpConstants.SFTP_SUBSYSTEM_NAME, getClass().getSimpleName());
         Utils.deleteRecursive(lclSftp);
 
-        try (FileSystem fs = 
FileSystems.newFileSystem(URI.create("sftp://x:x@localhost:"; + port + "/"), 
null)) {
+        try(FileSystem fs = FileSystems.newFileSystem(
+                URI.create("sftp://"; + getCurrentTestName() + ":" + 
getCurrentTestName() + "@localhost:" + port + "/"),
+                new TreeMap<String,Object>() {
+                    private static final long serialVersionUID = 1L;    // 
we're not serializing it
+                
+                    {
+                        put(SftpFileSystemProvider.READ_BUFFER_PROP_NAME, 
Integer.valueOf(SftpClient.MIN_READ_BUFFER_SIZE));
+                        put(SftpFileSystemProvider.WRITE_BUFFER_PROP_NAME, 
Integer.valueOf(SftpClient.MIN_WRITE_BUFFER_SIZE));
+                    }
+            })) {
+
             Path parentPath = targetPath.getParent();
             Path clientFolder = lclSftp.resolve("client");
             String remFilePath = Utils.resolveRelativeRemotePath(parentPath, 
clientFolder.resolve(getCurrentTestName() + ".txt"));

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6068e0b0/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpTest.java 
b/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpTest.java
index fe9e917..6bdffea 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/sftp/SftpTest.java
@@ -48,6 +48,7 @@ import org.apache.sshd.common.Session;
 import org.apache.sshd.common.file.FileSystemFactory;
 import org.apache.sshd.common.file.root.RootedFileSystemProvider;
 import org.apache.sshd.common.sftp.SftpConstants;
+import org.apache.sshd.common.util.IoUtils;
 import org.apache.sshd.common.util.OsUtils;
 import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
 import org.apache.sshd.server.Command;
@@ -262,13 +263,13 @@ public class SftpTest extends BaseTestSupport {
             
                     sftp.remove(file);
     
-                    byte[] workBuf = new byte[1024 * 128];
+                    byte[] workBuf = new byte[IoUtils.DEFAULT_COPY_SIZE * 
Short.SIZE];
                     new Random(System.currentTimeMillis()).nextBytes(workBuf);
                     try (OutputStream os = sftp.write(file)) {
                         os.write(workBuf);
                     }
             
-                    try (InputStream is = sftp.read(file)) {
+                    try (InputStream is = sftp.read(file, 
IoUtils.DEFAULT_COPY_SIZE)) {
                         int readLen = is.read(workBuf);
                         assertEquals("Mismatched read data length", 
workBuf.length, readLen);
         
@@ -536,7 +537,7 @@ public class SftpTest extends BaseTestSupport {
                 try(SftpClient sftp = session.createSftpClient()) {
                     Path file1 = clientFolder.resolve(getCurrentTestName() + 
"-1.txt");
                     String file1Path = 
Utils.resolveRelativeRemotePath(parentPath, file1);
-                    try (OutputStream os = sftp.write(file1Path)) {
+                    try (OutputStream os = sftp.write(file1Path, 
SftpClient.MIN_WRITE_BUFFER_SIZE)) {
                         os.write((getCurrentTestName() + "\n").getBytes());
                     }
 
@@ -551,7 +552,7 @@ public class SftpTest extends BaseTestSupport {
                         assertEquals("Mismatched status for failed rename of " 
+ file2Path + " => " + file3Path, SSH_FX_NO_SUCH_FILE, e.getStatus());
                     }
             
-                    try (OutputStream os = sftp.write(file2Path)) {
+                    try (OutputStream os = sftp.write(file2Path, 
SftpClient.MIN_WRITE_BUFFER_SIZE)) {
                         os.write("H".getBytes());
                     }
             

Reply via email to