liukd / ganymed-ssh-2

Automatically exported from code.google.com/p/ganymed-ssh-2
Other
0 stars 0 forks source link

Support for InputStreams in SCPClient.java #14

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

I needed a method to put data from an InputStream on the SSH-Server. I added a 
method to SCPClient.java which allows to put a file from an InputStream to the 
server. Maybe it will be useful for you, too. 

Here is the method I've added:

        /**
     * Copy a data from an InputStream to a remote directory, uses mode 
         * 0600 when creating the file on the remote side.
     * 
         * @param is
         *            InputStream, ready to read data.
         * @param length
         *            Amount of data which will be read from the stream.
     * @param remoteFileName
     *            Remote file name.
     * @param remoteTargetDirectory
     *            Remote target directory. Use an empty string to specify the default directory.
     * 
     * @throws IOException
     */
        public void put(InputStream in, long length, String remoteFileName, String remoteTargetDirectory) throws IOException {
            byte [] buffer = new byte[8192];
            Session sess = conn.openSession();
            String cmd = "scp -t -d " + remoteTargetDirectory;

            OutputStream os = new BufferedOutputStream(sess.getStdin(), 40000);
            InputStream is = new BufferedInputStream(sess.getStdout(), 512);

            remoteTargetDirectory = remoteTargetDirectory.trim();
            remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : ".";

            try 
            {
                String cline = "C0660" + " " + length + " " + remoteFileName + "\n";
                sess.execCommand(cmd);

                os.write(StringEncoder.GetBytes(cline));
                os.flush();
                readResponse(is);

                try {
                    long remain = length;
                    while (remain > 0) 
                    {
                        int trans;
                        if(remain > buffer.length) {
                            trans = buffer.length;
                        } else {
                            trans = (int) remain;
                        }
                        int readed = in.read(buffer, 0, trans);
                        if(readed <= 0) {
                            throw new IOException("Cannot read enough from external stream");
                        }
                        os.write(buffer, 0, readed);
                        os.flush();
                        remain -= readed;
                    }
                }
                finally {
                    if (in != null) {
                        in.close();
                    }
                }

                os.write(0);
        os.flush();

        readResponse(is);

        os.write(StringEncoder.GetBytes("E\n"));
        os.flush();   
            }
            catch (IOException e) {
                throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
            }

            finally {
                if (sess != null) {
                    sess.close();
                }
            }
        }

Original issue reported on code.google.com by weltraum...@googlemail.com on 30 Sep 2011 at 7:18

GoogleCodeExporter commented 8 years ago
You can use SCPInputStream which is already there.

Original comment by dkocher@sudo.ch on 4 Oct 2011 at 11:34