use of com.jcraft.jsch.ChannelSftp in project keepass2android by PhilippC.
the class SftpStorage method delete.
@Override
public void delete(String path) throws Exception {
ChannelSftp c = init(path);
delete(path, c);
}
use of com.jcraft.jsch.ChannelSftp in project keepass2android by PhilippC.
the class SftpStorage method openFileForRead.
@Override
public InputStream openFileForRead(String path) throws Exception {
ChannelSftp c = init(path);
try {
byte[] buff = new byte[8000];
int bytesRead = 0;
InputStream in = c.get(extractSessionPath(path));
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while ((bytesRead = in.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}
byte[] data = bao.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(data);
c.getSession().disconnect();
return bin;
} catch (Exception e) {
tryDisconnect(c);
throw convertException(e);
}
}
use of com.jcraft.jsch.ChannelSftp in project keepass2android by PhilippC.
the class SftpStorage method uploadFile.
@Override
public void uploadFile(String path, byte[] data, boolean writeTransactional) throws Exception {
ChannelSftp c = init(path);
try {
InputStream in = new ByteArrayInputStream(data);
String targetPath = extractSessionPath(path);
if (writeTransactional) {
// upload to temporary location:
String tmpPath = targetPath + ".tmp";
c.put(in, tmpPath);
// remove previous file:
try {
c.rm(targetPath);
} catch (SftpException e) {
// ignore. Can happen if file didn't exist before
}
// rename tmp to target path:
c.rename(tmpPath, targetPath);
} else {
c.put(in, targetPath);
}
tryDisconnect(c);
} catch (Exception e) {
tryDisconnect(c);
throw e;
}
}
use of com.jcraft.jsch.ChannelSftp in project ant-ivy by apache.
the class SFTPRepository method getSftpChannel.
/**
* Establish the connection to the server if not yet connected, and listen to ivy events for
* closing connection when resolve is finished. Not meant to be used in multi threaded
* environment.
*
* @return the ChannelSftp with which a connection is established
* @throws IOException
* if any connection problem occurs
*/
private ChannelSftp getSftpChannel(String pathOrUri) throws IOException {
Session session = getSession(pathOrUri);
String host = session.getHost();
ChannelSftp channel = SshCache.getInstance().getChannelSftp(session);
if (channel == null) {
try {
channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
Message.verbose(":: SFTP :: connected to " + host + "!");
SshCache.getInstance().attachChannelSftp(session, channel);
} catch (JSchException e) {
throw new IOException(e.getMessage(), e);
}
}
return channel;
}
use of com.jcraft.jsch.ChannelSftp in project ant-ivy by apache.
the class SshCache method getChannelSftp.
/**
* retrieves an sftp channel from the cache
*
* @param session
* to connect to
* @return channelSftp or null if not successful (channel not existent or dead)
* @throws IOException should never happen
*/
public ChannelSftp getChannelSftp(Session session) throws IOException {
ChannelSftp channel = null;
Entry entry = getCacheEntry(session);
if (entry != null) {
channel = entry.getChannelSftp();
if (channel != null && !channel.isConnected()) {
entry.releaseChannelSftp();
channel = null;
}
}
return channel;
}
Aggregations