use of com.jn.agileway.ssh.client.sftp.exception.SftpException in project agileway by fangjinuo.
the class Ssh2SftpSession method open.
@Override
public SftpFile open(String filepath, int openMode, FileAttrs attrs) throws SftpException {
try {
SFTPv3FileHandle handle = null;
if (!Sftps.exists(this, filepath)) {
if (attrs == null) {
attrs = new FileAttrs();
}
FileMode fileMode = attrs.getFileMode();
if (fileMode == null) {
fileMode = FileMode.createFileMode(FileType.REGULAR, 0644);
attrs.setFileMode(fileMode);
}
if (OpenMode.isCreatable(openMode)) {
handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
} else {
throw new NoSuchFileSftpException(StringTemplates.formatWithPlaceholder("no such file: {}", filepath));
}
} else {
if (OpenMode.isTruncated(openMode)) {
handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
}
}
if (handle != null) {
sftpClient.closeFile(handle);
handle = null;
}
if (OpenMode.isWritable(openMode)) {
handle = sftpClient.openFileRW(filepath);
} else {
handle = sftpClient.openFileRO(filepath);
}
Ssh2SftpFile sftpFile = new Ssh2SftpFile(this, filepath);
sftpFile.setFileHandle(handle);
return sftpFile;
} catch (Throwable ex) {
throw new SftpException(ex);
}
}
use of com.jn.agileway.ssh.client.sftp.exception.SftpException in project agileway by fangjinuo.
the class JschSftpFile method read.
@Override
public int read(long fileOffset, byte[] buffer, int bufferOffset, int length) throws IOException {
ChannelSftp channel = ((JschSftpSession) this.session).getChannel();
InputStream inputStream = null;
try {
inputStream = channel.get(this.path, null, fileOffset);
int readLength = inputStream.read(buffer, bufferOffset, length);
return readLength;
} catch (Throwable ex) {
throw new SftpException(ex);
} finally {
IOs.close(inputStream);
}
}
use of com.jn.agileway.ssh.client.sftp.exception.SftpException in project agileway by fangjinuo.
the class JschSftpFile method write.
@Override
public void write(long fileOffset, byte[] data, int offset, int length) throws IOException {
ChannelSftp channel = ((JschSftpSession) this.session).getChannel();
if (length < 0) {
throw new IllegalArgumentException("length < 0");
}
if (offset >= data.length) {
throw new IllegalArgumentException("bufferOffset >= buffer.length");
}
if (length == 0) {
return;
}
if (offset + length > data.length) {
length = data.length - offset;
}
byte[] buffer = new byte[length];
System.arraycopy(data, offset, buffer, 0, length);
try {
channel.put(new ByteArrayInputStream(buffer), this.path, fileOffset == 0 ? ChannelSftp.OVERWRITE : ChannelSftp.APPEND);
} catch (Throwable ex) {
throw new SftpException(ex);
}
}
use of com.jn.agileway.ssh.client.sftp.exception.SftpException in project agileway by fangjinuo.
the class JschSftps method wrapSftpException.
public static SftpException wrapSftpException(com.jcraft.jsch.SftpException ex) {
ResponseStatusCode statusCode = Enums.ofCode(ResponseStatusCode.class, ex.id);
SftpException exception = null;
if (statusCode == ResponseStatusCode.NO_SUCH_FILE) {
exception = new NoSuchFileSftpException(ex);
} else {
exception = new SftpException(ex);
}
exception.setStatusCode(statusCode);
return exception;
}
use of com.jn.agileway.ssh.client.sftp.exception.SftpException in project agileway by fangjinuo.
the class Ssh2Sftps method wrapSftpException.
public static SftpException wrapSftpException(Throwable ex) {
if (ex instanceof SFTPException) {
ResponseStatusCode statusCode = Enums.ofCode(ResponseStatusCode.class, ((SFTPException) ex).getServerErrorCode());
SftpException exception = null;
if (statusCode == ResponseStatusCode.NO_SUCH_FILE) {
exception = new NoSuchFileSftpException(ex);
} else {
exception = new SftpException(ex);
}
exception.setStatusCode(statusCode);
return exception;
} else {
return new SftpException(ex);
}
}
Aggregations