I wonder if we should add back the createBuffer method with a single
argument that would call createBuffer(cmd, 0) for readability...

On Tue, Dec 15, 2009 at 04:56,  <spea...@apache.org> wrote:
> Author: spearce
> Date: Tue Dec 15 03:56:32 2009
> New Revision: 890631
>
> URL: http://svn.apache.org/viewvc?rev=890631&view=rev
> Log:
> Allocate the next ChannelOutputStream buffer sized to last buffer
>
> If an output stream has just received a large burst of data from
> the application, its buffer will have needed to grow beyond the
> default buffer size of 512 bytes.  Its quite likely that there will
> be even more data from the application, filling the next buffer
> up to approximately the same size before we are forced to trigger
> another flush.  So allocate the next buffer sized to hold the last
> packet's complete length, or the data remaining if we had to split
> the buffered packet to fit into the available window space.
>
> Unfortunately most uses of createBuffer don't care about the size
> of the buffer, as they are one-shot requests which are often within
> the default size of 512 bytes.  Pass in 0 for these and allow them
> to use the default 512 byte size.
>
> Modified:
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelAgentForwarding.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
>    mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelDirectTcpip.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
>    
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/TcpipForwardSupport.java
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
>  Tue Dec 15 03:56:32 2009
> @@ -63,7 +63,7 @@
>     protected void sendNextKey(PublicKey key) throws IOException {
>         try {
>             log.info("Send SSH_MSG_USERAUTH_REQUEST for publickey");
> -            Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
> +            Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
>             int pos1 = buffer.wpos() - 1;
>             buffer.putString(username);
>             buffer.putString("ssh-connection");
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
>  Tue Dec 15 03:56:32 2009
> @@ -38,7 +38,7 @@
>
>     public UserAuthPassword(ClientSessionImpl session, String username, 
> String password) throws IOException {
>         log.info("Send SSH_MSG_USERAUTH_REQUEST for password");
> -        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
> +        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
>         buffer.putString(username);
>         buffer.putString("ssh-connection");
>         buffer.putString("password");
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
>  Tue Dec 15 03:56:32 2009
> @@ -47,7 +47,7 @@
>     public UserAuthPublicKey(ClientSessionImpl session, String username, 
> KeyPair key) throws IOException {
>         try {
>             log.info("Send SSH_MSG_USERAUTH_REQUEST for publickey");
> -            Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
> +            Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
>             int pos1 = buffer.wpos() - 1;
>             buffer.putString(username);
>             buffer.putString("ssh-connection");
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
>  Tue Dec 15 03:56:32 2009
> @@ -165,7 +165,7 @@
>         }
>         openFuture = new DefaultOpenFuture(lock);
>         log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
> -        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN);
> +        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN, 0);
>         buffer.putString(type);
>         buffer.putInt(id);
>         buffer.putInt(localWindow.getSize());
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelAgentForwarding.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelAgentForwarding.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelAgentForwarding.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelAgentForwarding.java
>  Tue Dec 15 03:56:32 2009
> @@ -173,7 +173,7 @@
>         log.info("Received SSH_MSG_CHANNEL_REQUEST on channel {}", id);
>         String type = buffer.getString();
>         log.info("Received channel request: {}", type);
> -        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE);
> +        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE, 0);
>         buffer.putInt(recipient);
>         session.writePacket(buffer);
>     }
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
>  Tue Dec 15 03:56:32 2009
> @@ -78,7 +78,7 @@
>         }
>
>         log.info("Send SSH_MSG_CHANNEL_REQUEST exec");
> -        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST);
> +        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
>         buffer.putInt(recipient);
>         buffer.putString("exec");
>         buffer.putBoolean(false);
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
>  Tue Dec 15 03:56:32 2009
> @@ -72,7 +72,7 @@
>     protected void pumpInputStream() {
>         try {
>             while (!closeFuture.isClosed()) {
> -                Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_DATA);
> +                Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_DATA, 0);
>                 buffer.putInt(recipient);
>                 int wpos1 = buffer.wpos(); // keep buffer position to write 
> data length later
>                 buffer.putInt(0);
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
>  Tue Dec 15 03:56:32 2009
> @@ -139,7 +139,7 @@
>
>         if (agentForwarding) {
>             log.info("Send agent forwarding request");
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
>             buffer.putInt(recipient);
>             buffer.putString("auth-agent-...@openssh.com");
>             buffer.putBoolean(false);
> @@ -147,7 +147,7 @@
>         }
>
>         log.info("Send SSH_MSG_CHANNEL_REQUEST pty-req");
> -        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST);
> +        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
>         buffer.putInt(recipient);
>         buffer.putString("pty-req");
>         buffer.putBoolean(false);
> @@ -172,7 +172,7 @@
>  //        session.writePacket(buffer);
>
>         log.info("Send SSH_MSG_CHANNEL_REQUEST shell");
> -        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST);
> +        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
>         buffer.putInt(recipient);
>         buffer.putString("shell");
>         buffer.putBoolean(false);
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
>  Tue Dec 15 03:56:32 2009
> @@ -75,7 +75,7 @@
>         e = dh.getE();
>
>         log.info("Send SSH_MSG_KEXDH_INIT");
> -        Buffer buffer = 
> s.createBuffer(SshConstants.Message.SSH_MSG_KEXDH_INIT);
> +        Buffer buffer = 
> s.createBuffer(SshConstants.Message.SSH_MSG_KEXDH_INIT, 0);
>         buffer.putMPInt(e);
>         session.writePacket(buffer);
>     }
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
>  Tue Dec 15 03:56:32 2009
> @@ -375,7 +375,7 @@
>
>     private void sendAuthRequest() throws Exception {
>         log.info("Send SSH_MSG_SERVICE_REQUEST for ssh-userauth");
> -        Buffer buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_SERVICE_REQUEST);
> +        Buffer buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_SERVICE_REQUEST, 0);
>         buffer.putString("ssh-userauth");
>         writePacket(buffer);
>     }
> @@ -389,7 +389,7 @@
>         log.info("Received SSH_MSG_CHANNEL_OPEN {}", type);
>
>         if (closing) {
> -            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE);
> +            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
>             buf.putInt(id);
>             buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
>             buf.putString("SSH server is shutting down: " + type);
> @@ -400,7 +400,7 @@
>
>         final Channel channel = 
> NamedFactory.Utils.create(getFactoryManager().getChannelFactories(), type);
>         if (channel == null) {
> -            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE);
> +            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
>             buf.putInt(id);
>             buf.putInt(SshConstants.SSH_OPEN_UNKNOWN_CHANNEL_TYPE);
>             buf.putString("Unsupported channel type: " + type);
> @@ -416,14 +416,14 @@
>             public void operationComplete(OpenFuture future) {
>                 try {
>                     if (future.isOpened()) {
> -                        Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
> +                        Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, 0);
>                         buf.putInt(id);
>                         buf.putInt(channelId);
>                         buf.putInt(channel.getLocalWindow().getSize());
>                         buf.putInt(channel.getLocalWindow().getPacketSize());
>                         writePacket(buf);
>                     } else if (future.getException() != null) {
> -                        Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE);
> +                        Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
>                         buf.putInt(id);
>                         if (future.getException() instanceof 
> OpenChannelException) {
>                             
> buf.putInt(((OpenChannelException)future.getException()).getReasonCode());
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java 
> (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java 
> Tue Dec 15 03:56:32 2009
> @@ -68,9 +68,10 @@
>      * (5 bytes) for the packet header.
>      *
>      * @param cmd the SSH command
> +     * @param estimatedSize estimated number of bytes the buffer will hold, 
> 0 if unknown.
>      * @return a new buffer ready for write
>      */
> -    Buffer createBuffer(SshConstants.Message cmd);
> +    Buffer createBuffer(SshConstants.Message cmd, int estimatedSize);
>
>     /**
>      * Encode and send the given buffer.
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
>  Tue Dec 15 03:56:32 2009
> @@ -92,7 +92,7 @@
>                     if (!closing) {
>                         closing = true;
>                         log.info("Send SSH_MSG_CHANNEL_CLOSE on channel {}", 
> id);
> -                        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_CLOSE);
> +                        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_CLOSE, 0);
>                         buffer.putInt(recipient);
>                         session.writePacket(buffer);
>                     }
> @@ -134,7 +134,7 @@
>         // Only accept extended data for stderr
>         if (ex != 1) {
>             log.info("Send SSH_MSG_CHANNEL_FAILURE on channel {}", id);
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>             return;
> @@ -175,7 +175,7 @@
>
>     protected void sendEof() throws IOException {
>         log.info("Send SSH_MSG_CHANNEL_EOF on channel {}", id);
> -        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_EOF);
> +        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_EOF, 0);
>         buffer.putInt(recipient);
>         session.writePacket(buffer);
>     }
> @@ -188,7 +188,7 @@
>
>     protected void sendWindowAdjust(int len) throws IOException {
>         log.info("Send SSH_MSG_CHANNEL_WINDOW_ADJUST on channel {}", id);
> -        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_WINDOW_ADJUST);
> +        Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_WINDOW_ADJUST, 0);
>         buffer.putInt(recipient);
>         buffer.putInt(len);
>         session.writePacket(buffer);
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
>  Tue Dec 15 03:56:32 2009
> @@ -41,6 +41,7 @@
>     private Buffer buffer;
>     private boolean closed;
>     private int bufferLength;
> +    private int lastSize;
>
>     public ChannelOutputStream(AbstractChannel channel, Window remoteWindow, 
> Logger log, SshConstants.Message cmd) {
>         this.channel = channel;
> @@ -77,6 +78,7 @@
>                 buf.wpos(cmd == 
> SshConstants.Message.SSH_MSG_CHANNEL_EXTENDED_DATA ? 14 : 10);
>                 buf.putInt(length);
>                 buf.wpos(buf.wpos() + length);
> +                lastSize = Math.max(length, total - length);
>                 newBuffer();
>                 if (total > length) {
>                     buffer.putRawBytes(buf.array(), pos - (total - length), 
> total - length);
> @@ -104,7 +106,7 @@
>     }
>
>     private void newBuffer() {
> -        buffer = channel.getSession().createBuffer(cmd);
> +        buffer = channel.getSession().createBuffer(cmd, lastSize <= 0 ? 0 : 
> 12 + lastSize);
>         buffer.putInt(channel.getRecipient());
>         if (cmd == SshConstants.Message.SSH_MSG_CHANNEL_EXTENDED_DATA) {
>             buffer.putInt(1);
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
>  Tue Dec 15 03:56:32 2009
> @@ -336,10 +336,31 @@
>      * (5 bytes) for the packet header.
>      *
>      * @param cmd the SSH command
> +     * @param len estimated number of bytes the buffer will hold, 0 if 
> unknown.
>      * @return a new buffer ready for write
>      */
> -    public Buffer createBuffer(SshConstants.Message cmd) {
> -        Buffer buffer = new Buffer();
> +    public Buffer createBuffer(SshConstants.Message cmd, int len) {
> +        Buffer buffer;
> +        if (len <= 0) {
> +            buffer = new Buffer();
> +        } else {
> +            // Since the caller claims to know how many bytes they will need
> +            // increase their request to account for our headers/footers if
> +            // they actually send exactly this amount.
> +            //
> +            int bsize = outCipherSize;
> +            int oldLen = len;
> +            len += 5;
> +            int pad = (-len) & (bsize - 1);
> +            if (pad < bsize) {
> +                pad += bsize;
> +            }
> +            len = len + pad - 4;
> +            if (outMac != null) {
> +                len += outMac.getBlockSize();
> +            }
> +            buffer = new Buffer(new byte[Math.max(len, 
> Buffer.DEFAULT_SIZE)], false);
> +        }
>         buffer.rpos(5);
>         buffer.wpos(5);
>         buffer.putByte(cmd.toByte());
> @@ -615,7 +636,7 @@
>      * @throws IOException if an error occured sending the packet
>      */
>     protected byte[] sendKexInit(String[] proposal) throws IOException {
> -        Buffer buffer = createBuffer(SshConstants.Message.SSH_MSG_KEXINIT);
> +        Buffer buffer = createBuffer(SshConstants.Message.SSH_MSG_KEXINIT, 
> 0);
>         int p = buffer.wpos();
>         buffer.wpos(p + 16);
>         random.fill(buffer.array(), p, 16);
> @@ -663,7 +684,7 @@
>      */
>     protected void sendNewKeys() throws IOException {
>         log.info("Send SSH_MSG_NEWKEYS");
> -        Buffer buffer = createBuffer(SshConstants.Message.SSH_MSG_NEWKEYS);
> +        Buffer buffer = createBuffer(SshConstants.Message.SSH_MSG_NEWKEYS, 
> 0);
>         writePacket(buffer);
>     }
>
> @@ -810,7 +831,7 @@
>      * @throws IOException if an error occured sending the packet
>      */
>     public void disconnect(int reason, String msg) throws IOException {
> -        Buffer buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_DISCONNECT);
> +        Buffer buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_DISCONNECT, 0);
>         buffer.putInt(reason);
>         buffer.putString(msg);
>         buffer.putString("");
> @@ -830,7 +851,7 @@
>      * @throws IOException if an error occured sending the packet
>      */
>     protected void notImplemented() throws IOException {
> -        Buffer buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_UNIMPLEMENTED);
> +        Buffer buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_UNIMPLEMENTED, 0);
>         buffer.putInt(seqi - 1);
>         writePacket(buffer);
>     }
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
>  Tue Dec 15 03:56:32 2009
> @@ -72,7 +72,7 @@
>             return false;
>         }
>         if (!hasSig) {
> -            Buffer buf = 
> session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_PK_OK);
> +            Buffer buf = 
> session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_PK_OK, 0);
>             buf.putString(alg);
>             buf.putRawBytes(buffer.array(), oldPos, 4 + len);
>             session.writePacket(buf);
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
>  Tue Dec 15 03:56:32 2009
> @@ -60,7 +60,7 @@
>         if (!exitStatusSent) {
>             exitStatusSent = true;
>             log.info("Send SSH_MSG_CHANNEL_REQUEST exit-status on channel 
> {}", id);
> -            Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST);
> +            Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
>             buffer.putInt(recipient);
>             buffer.putString("exit-status");
>             buffer.putByte((byte) 0);
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelDirectTcpip.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelDirectTcpip.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelDirectTcpip.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelDirectTcpip.java
>  Tue Dec 15 03:56:32 2009
> @@ -186,7 +186,7 @@
>         log.info("Received SSH_MSG_CHANNEL_REQUEST on channel {}", id);
>         String type = buffer.getString();
>         log.info("Received channel request: {}", type);
> -        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE);
> +        buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE, 0);
>         buffer.putInt(recipient);
>         session.writePacket(buffer);
>     }
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
>  Tue Dec 15 03:56:32 2009
> @@ -195,7 +195,7 @@
>         String type = buffer.getString();
>         log.info("Received channel request: {}", type);
>         if (!handleRequest(type, buffer)) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -276,7 +276,7 @@
>         addEnvVariable(name, value);
>         log.debug("env for channel {}: {} = {}", new Object[] { id, name, 
> value });
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -306,7 +306,7 @@
>         addEnvVariable(Environment.ENV_COLUMNS, Integer.toString(tColumns));
>         addEnvVariable(Environment.ENV_LINES, Integer.toString(tRows));
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -327,7 +327,7 @@
>         e.signal(Signal.WINCH);
>
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -348,7 +348,7 @@
>
>
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -363,7 +363,7 @@
>         command = ((ServerSession) 
> session).getServerFactoryManager().getShellFactory().create();
>         prepareCommand();
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -385,7 +385,7 @@
>         }
>         prepareCommand();
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -407,7 +407,7 @@
>         }
>         prepareCommand();
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -462,7 +462,7 @@
>         addEnvVariable(SshAgent.SSH_AUTHSOCKET_ENV_NAME, 
> Integer.toString(authSocket));
>
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
> @@ -473,7 +473,7 @@
>         boolean wantReply = buffer.getBoolean();
>         // TODO: start x11 forwarding
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
>             buffer.putInt(recipient);
>             session.writePacket(buffer);
>         }
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
>  Tue Dec 15 03:56:32 2009
> @@ -304,7 +304,7 @@
>     private void userAuth(Buffer buffer) throws Exception {
>         if (state == State.WaitingUserAuth) {
>             log.info("Accepting user authentication request");
> -            buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_SERVICE_ACCEPT);
> +            buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_SERVICE_ACCEPT, 0);
>             buffer.putString("ssh-userauth");
>             writePacket(buffer);
>             userAuthFactories = new 
> ArrayList<NamedFactory<UserAuth>>(getServerFactoryManager().getUserAuthFactories());
> @@ -341,14 +341,14 @@
>                 log.info("Unsupported authentication method '{}'", method);
>             }
>             if (authed != null && authed) {
> -                buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS);
> +                buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS, 0);
>                 writePacket(buffer);
>                 state = State.Running;
>                 this.authed = true;
>                 this.username = username;
>                 unscheduleAuthTimer();
>             } else {
> -                buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_FAILURE);
> +                buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_FAILURE, 0);
>                 NamedFactory.Utils.remove(userAuthFactories, "none"); // 
> 'none' MUST NOT be listed
>                 
> buffer.putString(NamedFactory.Utils.getNames(userAuthFactories));
>                 buffer.putByte((byte) 0);
> @@ -370,7 +370,7 @@
>         log.info("Received SSH_MSG_CHANNEL_OPEN {}", type);
>
>         if (closing) {
> -            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE);
> +            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
>             buf.putInt(id);
>             buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
>             buf.putString("SSH server is shutting down: " + type);
> @@ -379,7 +379,7 @@
>             return;
>         }
>         if (!allowMoreSessions) {
> -            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE);
> +            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
>             buf.putInt(id);
>             buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
>             buf.putString("additional sessions disabled");
> @@ -390,7 +390,7 @@
>
>         final Channel channel = 
> NamedFactory.Utils.create(getServerFactoryManager().getChannelFactories(), 
> type);
>         if (channel == null) {
> -            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE);
> +            Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
>             buf.putInt(id);
>             buf.putInt(SshConstants.SSH_OPEN_UNKNOWN_CHANNEL_TYPE);
>             buf.putString("Unsupported channel type: " + type);
> @@ -406,14 +406,14 @@
>             public void operationComplete(OpenFuture future) {
>                 try {
>                     if (future.isOpened()) {
> -                        Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
> +                        Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, 0);
>                         buf.putInt(id);
>                         buf.putInt(channelId);
>                         buf.putInt(channel.getLocalWindow().getSize());
>                         buf.putInt(channel.getLocalWindow().getPacketSize());
>                         writePacket(buf);
>                     } else if (future.getException() != null) {
> -                        Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE);
> +                        Buffer buf = 
> createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
>                         buf.putInt(id);
>                         if (future.getException() instanceof 
> OpenChannelException) {
>                             
> buf.putInt(((OpenChannelException)future.getException()).getReasonCode());
> @@ -450,7 +450,7 @@
>             log.error("Unknown global request: {}", req);
>         }
>         if (wantReply){
> -            buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_REQUEST_FAILURE);
> +            buffer = 
> createBuffer(SshConstants.Message.SSH_MSG_REQUEST_FAILURE, 0);
>             writePacket(buffer);
>         }
>     }
>
> Modified: 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/TcpipForwardSupport.java
> URL: 
> http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/TcpipForwardSupport.java?rev=890631&r1=890630&r2=890631&view=diff
> ==============================================================================
> --- 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/TcpipForwardSupport.java
>  (original)
> +++ 
> mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/TcpipForwardSupport.java
>  Tue Dec 15 03:56:32 2009
> @@ -83,7 +83,7 @@
>         final TcpIpForwardFilter filter = 
> session.getServerFactoryManager().getTcpIpForwardFilter();
>         if (addr == null || filter == null || !filter.canListen(addr, 
> session)) {
>             if (wantReply) {
> -                buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_FAILURE);
> +                buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_FAILURE, 0);
>                 session.writePacket(buffer);
>             }
>             return;
> @@ -98,7 +98,7 @@
>                 close();
>             }
>             if (wantReply) {
> -                buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_FAILURE);
> +                buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_FAILURE, 0);
>                 session.writePacket(buffer);
>             }
>             return;
> @@ -112,7 +112,7 @@
>             }
>         }
>         if (wantReply){
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_SUCCESS, 0);
>             buffer.putInt(port);
>             session.writePacket(buffer);
>         }
> @@ -125,7 +125,7 @@
>             acceptor.unbind(new InetSocketAddress(address, port));
>         }
>         if (wantReply) {
> -            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_SUCCESS);
> +            buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_SUCCESS, 0);
>             session.writePacket(buffer);
>         }
>     }
> @@ -184,7 +184,7 @@
>             }
>             openFuture = new DefaultOpenFuture(lock);
>             log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
> -            Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN);
> +            Buffer buffer = 
> session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN, 0);
>             buffer.putString(type);
>             buffer.putInt(id);
>             buffer.putInt(localWindow.getSize());
>
>
>



-- 
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/
------------------------
Open Source SOA
http://fusesource.com

Reply via email to