ScpToMessagepublic class ScpToMessage extends AbstractSshMessage Utility class to carry out an upload scp transfer. |
Fields Summary |
---|
private static final int | BUFFER_SIZE | private File | localFile | private String | remotePath | private List | directoryList |
Constructors Summary |
---|
public ScpToMessage(com.jcraft.jsch.Session session)Constructor for ScpToMessage
super(session);
| public ScpToMessage(boolean verbose, com.jcraft.jsch.Session session)Constructor for ScpToMessage
super(verbose, session);
| public ScpToMessage(boolean verbose, com.jcraft.jsch.Session session, File aLocalFile, String aRemotePath)Constructor for a local file to remote.
this(verbose, session, aRemotePath);
this.localFile = aLocalFile;
| public ScpToMessage(boolean verbose, com.jcraft.jsch.Session session, List aDirectoryList, String aRemotePath)Constructor for a local directories to remote.
this(verbose, session, aRemotePath);
this.directoryList = aDirectoryList;
| private ScpToMessage(boolean verbose, com.jcraft.jsch.Session session, String aRemotePath)Constructor for ScpToMessage.
super(verbose, session);
this.remotePath = aRemotePath;
| public ScpToMessage(com.jcraft.jsch.Session session, File aLocalFile, String aRemotePath)Constructor for ScpToMessage.
this(false, session, aLocalFile, aRemotePath);
| public ScpToMessage(com.jcraft.jsch.Session session, List aDirectoryList, String aRemotePath)Constructor for ScpToMessage.
this(false, session, aDirectoryList, aRemotePath);
|
Methods Summary |
---|
private void | doMultipleTransfer()
Channel channel = openExecChannel("scp -r -d -t " + remotePath);
try {
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
waitForAck(in);
for (Iterator i = directoryList.iterator(); i.hasNext();) {
Directory current = (Directory) i.next();
sendDirectory(current, in, out);
}
} finally {
if (channel != null) {
channel.disconnect();
}
}
| private void | doSingleTransfer()
String cmd = "scp -t " + remotePath;
Channel channel = openExecChannel(cmd);
try {
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
waitForAck(in);
sendFileToRemote(localFile, in, out);
} finally {
if (channel != null) {
channel.disconnect();
}
}
| public void | execute()Carry out the transfer.
if (directoryList != null) {
doMultipleTransfer();
}
if (localFile != null) {
doSingleTransfer();
}
log("done.\n");
| public java.io.File | getLocalFile()Get the local file
return localFile;
| public java.lang.String | getRemotePath()Get the remote path
return remotePath;
| private void | sendDirectory(Directory current, java.io.InputStream in, java.io.OutputStream out)
for (Iterator fileIt = current.filesIterator(); fileIt.hasNext();) {
sendFileToRemote((File) fileIt.next(), in, out);
}
for (Iterator dirIt = current.directoryIterator(); dirIt.hasNext();) {
Directory dir = (Directory) dirIt.next();
sendDirectoryToRemote(dir, in, out);
}
| private void | sendDirectoryToRemote(Directory directory, java.io.InputStream in, java.io.OutputStream out)
String command = "D0755 0 ";
command += directory.getDirectory().getName();
command += "\n";
out.write(command.getBytes());
out.flush();
waitForAck(in);
sendDirectory(directory, in, out);
out.write("E\n".getBytes());
waitForAck(in);
| private void | sendFileToRemote(java.io.File localFile, java.io.InputStream in, java.io.OutputStream out)
// send "C0644 filesize filename", where filename should not include '/'
long filesize = localFile.length();
String command = "C0644 " + filesize + " ";
command += localFile.getName();
command += "\n";
out.write(command.getBytes());
out.flush();
waitForAck(in);
// send a content of lfile
FileInputStream fis = new FileInputStream(localFile);
byte[] buf = new byte[BUFFER_SIZE];
long startTime = System.currentTimeMillis();
long totalLength = 0;
// only track progress for files larger than 100kb in verbose mode
boolean trackProgress = getVerbose() && filesize > 102400;
// since filesize keeps on decreasing we have to store the
// initial filesize
long initFilesize = filesize;
int percentTransmitted = 0;
try {
if (this.getVerbose()) {
log("Sending: " + localFile.getName() + " : " + localFile.length());
}
while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0) {
break;
}
out.write(buf, 0, len);
totalLength += len;
if (trackProgress) {
percentTransmitted = trackProgress(initFilesize,
totalLength,
percentTransmitted);
}
}
out.flush();
sendAck(out);
waitForAck(in);
} finally {
if (this.getVerbose()) {
long endTime = System.currentTimeMillis();
logStats(startTime, endTime, totalLength);
}
fis.close();
}
|
|