Search in sources :

Example 1 with SFTPv3FileHandle

use of ch.ethz.ssh2.SFTPv3FileHandle in project orion-kit by lijiahangmax.

the class SftpExecutor method touch.

/**
 * 创建文件
 *
 * @param path     文件绝对路径
 * @param truncate 是否截断
 */
public void touch(String path, boolean truncate) {
    try {
        this.mkdirs(Files1.getParentPath(path), DEFAULT_PERMISSIONS);
        SFTPv3FileHandle handle;
        if (truncate) {
            handle = client.createFileTruncate(path);
        } else {
            handle = client.createFile(path);
        }
        handle.getClient().closeFile(handle);
    } catch (Exception e) {
        throw Exceptions.sftp(e);
    }
}
Also used : SFTPv3FileHandle(ch.ethz.ssh2.SFTPv3FileHandle)

Example 2 with SFTPv3FileHandle

use of ch.ethz.ssh2.SFTPv3FileHandle in project orion-kit by lijiahangmax.

the class SftpExecutor method write.

/**
 * 拼接/替换/写入到文件
 *
 * @param path       文件绝对路径
 * @param in         输入流
 * @param fileOffset 文件偏移量
 * @param entry      写入信息
 * @param lines      行
 * @param type       1write  2replace  3append
 * @throws IOException IOException
 */
private void write(String path, long fileOffset, InputStream in, StreamEntry entry, List<String> lines, int type) throws IOException {
    SFTPv3FileHandle handle = null;
    try {
        this.touch(path, type == 1);
        if (type == 3) {
            handle = client.openFileRWAppend(path);
        } else {
            handle = client.openFileRW(path);
        }
        this.write(handle, fileOffset, in, entry, lines);
    } catch (Exception e) {
        throw Exceptions.io("cannot write file " + path, e);
    } finally {
        if (handle != null) {
            handle.getClient().closeFile(handle);
        }
    }
}
Also used : SFTPv3FileHandle(ch.ethz.ssh2.SFTPv3FileHandle)

Example 3 with SFTPv3FileHandle

use of ch.ethz.ssh2.SFTPv3FileHandle in project hudson-2.x by hudson.

the class SFTPClient method writeToFile.

/**
 * Creates a new file and writes to it.
 */
public OutputStream writeToFile(String path) throws IOException {
    final SFTPv3FileHandle h = createFile(path);
    return new OutputStream() {

        private long offset = 0;

        public void write(int b) throws IOException {
            write(new byte[] { (byte) b });
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            SFTPClient.this.write(h, offset, b, off, len);
            offset += len;
        }

        @Override
        public void close() throws IOException {
            closeFile(h);
        }
    };
}
Also used : SFTPv3FileHandle(com.trilead.ssh2.SFTPv3FileHandle) OutputStream(java.io.OutputStream)

Example 4 with SFTPv3FileHandle

use of ch.ethz.ssh2.SFTPv3FileHandle in project hudson-2.x by hudson.

the class SFTPClient method read.

public InputStream read(String file) throws IOException {
    final SFTPv3FileHandle h = openFileRO(file);
    return new InputStream() {

        private long offset = 0;

        public int read() throws IOException {
            byte[] b = new byte[1];
            if (read(b) < 0)
                return -1;
            return b[0];
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            int r = SFTPClient.this.read(h, offset, b, off, len);
            if (r < 0)
                return -1;
            offset += r;
            return r;
        }

        @Override
        public long skip(long n) throws IOException {
            offset += n;
            return n;
        }

        @Override
        public void close() throws IOException {
            closeFile(h);
        }
    };
}
Also used : SFTPv3FileHandle(com.trilead.ssh2.SFTPv3FileHandle) InputStream(java.io.InputStream)

Example 5 with SFTPv3FileHandle

use of ch.ethz.ssh2.SFTPv3FileHandle in project Payara by payara.

the class SFTPClient method writeToFile.

/**
 * Creates a new file and writes to it.
 */
public OutputStream writeToFile(String path) throws IOException {
    path = normalizePath(path);
    final SFTPv3FileHandle h = createFile(path);
    return new OutputStream() {

        private long offset = 0;

        public void write(int b) throws IOException {
            write(new byte[] { (byte) b });
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            SFTPClient.this.write(h, offset, b, off, len);
            offset += len;
        }

        @Override
        public void close() throws IOException {
            closeFile(h);
        }
    };
}
Also used : SFTPv3FileHandle(com.trilead.ssh2.SFTPv3FileHandle) OutputStream(java.io.OutputStream)

Aggregations

SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)8 SFTPv3FileHandle (ch.ethz.ssh2.SFTPv3FileHandle)4 InputStream (java.io.InputStream)3 FileAttrs (com.jn.agileway.ssh.client.sftp.attrs.FileAttrs)2 FileMode (com.jn.agileway.ssh.client.sftp.attrs.FileMode)2 NoSuchFileSftpException (com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)2 OutputStream (java.io.OutputStream)2 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)2 KettleException (org.pentaho.di.core.exception.KettleException)2 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)2 SftpException (com.jn.agileway.ssh.client.sftp.exception.SftpException)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1