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/02/23 16:30:15 UTC

[14/15] mina-sshd git commit: [SSHD-416] Avoid unnecessary memory copies when reading / writing data to files

[SSHD-416] Avoid unnecessary memory copies when reading / writing data to files


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

Branch: refs/heads/master
Commit: 656f0d17e5cf2b7384156feb77c193d5c85f0e77
Parents: 55d45e9
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Feb 23 01:08:48 2015 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Feb 23 16:21:30 2015 +0100

----------------------------------------------------------------------
 .../apache/sshd/server/sftp/SftpSubsystem.java  | 29 ++++++++++++++------
 1 file changed, 20 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/656f0d17/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
index 28fba31..b78315c 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
@@ -1196,15 +1196,23 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
     protected void doWrite(Buffer buffer, int id) throws IOException {
         String handle = buffer.getString();
         long offset = buffer.getLong();
-        byte[] data = buffer.getBytes();
-        log.debug("Received SSH_FXP_WRITE (handle={}, offset={}, data=byte[{}])", new Object[] { handle, offset, data.length });
+        int length = buffer.getInt();
+        if (length < 0) {
+            throw new IllegalStateException();
+        }
+        if (buffer.available() < length) {
+            throw new BufferUnderflowException();
+        }
+        byte[] data = buffer.array();
+        int doff = buffer.rpos();
+        log.debug("Received SSH_FXP_WRITE (handle={}, offset={}, data=byte[{}])", new Object[] { handle, offset, length });
         try {
             Handle p = handles.get(handle);
             if (!(p instanceof FileHandle)) {
                 sendStatus(id, SSH_FX_INVALID_HANDLE, handle);
             } else {
                 FileHandle fh = (FileHandle) p;
-                fh.write(data, offset);
+                fh.write(data, doff, length, offset);
                 sendStatus(id, SSH_FX_OK, "");
             }
         } catch (IOException e) {
@@ -1223,13 +1231,16 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
                 sendStatus(id, SSH_FX_INVALID_HANDLE, handle);
             } else {
                 FileHandle fh = (FileHandle) p;
-                byte[] b = new byte[Math.min(len, Buffer.MAX_LEN)];
-                len = fh.read(b, offset);
+                Buffer buf = new Buffer(len + 9);
+                buf.putByte((byte) SSH_FXP_DATA);
+                buf.putInt(id);
+                int pos = buf.wpos();
+                buf.putInt(0);
+                len = fh.read(buf.array(), buf.wpos(), len, offset);
                 if (len >= 0) {
-                    Buffer buf = new Buffer(len + 5);
-                    buf.putByte((byte) SSH_FXP_DATA);
-                    buf.putInt(id);
-                    buf.putBytes(b, 0, len);
+                    buf.wpos(pos);
+                    buf.putInt(len);
+                    buf.wpos(pos + 4 + len);
                     send(buf);
                 } else {
                     sendStatus(id, SSH_FX_EOF, "");