Java Code Examples for net.schmizz.sshj.sftp.RemoteFile#close()

The following examples show how to use net.schmizz.sshj.sftp.RemoteFile#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SFTPTouchFeature.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Path touch(final Path file, final TransferStatus status) throws BackgroundException {
    if(file.isFile()) {
        try {
            final FileAttributes attrs;
            if(Permission.EMPTY != status.getPermission()) {
                attrs = new FileAttributes.Builder().withPermissions(Integer.parseInt(status.getPermission().getMode(), 8)).build();
            }
            else {
                attrs = FileAttributes.EMPTY;
            }
            final RemoteFile handle = session.sftp().open(file.getAbsolute(),
                EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE), attrs);
            handle.close();
        }
        catch(IOException e) {
            throw new SFTPExceptionMappingService().map("Cannot create file {0}", e, file);
        }
    }
    return new Path(file.getParent(), file.getName(), file.getType(), new SFTPAttributesFinderFeature(session).find(file));
}
 
Example 2
Source File: SFTPReadFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        final RemoteFile handle = session.sftp().open(file.getAbsolute(), EnumSet.of(OpenMode.READ));
        final int maxUnconfirmedReads = this.getMaxUnconfirmedReads(status);
        if(log.isInfoEnabled()) {
            log.info(String.format("Skipping %d bytes", status.getOffset()));
        }
        return handle.new ReadAheadRemoteFileInputStream(maxUnconfirmedReads, status.getOffset()) {
            private final AtomicBoolean close = new AtomicBoolean();

            @Override
            public void close() throws IOException {
                if(close.get()) {
                    log.warn(String.format("Skip double close of stream %s", this));
                    return;
                }
                try {
                    super.close();
                }
                finally {
                    handle.close();
                    close.set(true);
                }
            }
        };
    }
    catch(IOException e) {
        throw new SFTPExceptionMappingService().map("Download {0} failed", e, file);
    }
}
 
Example 3
Source File: SFTPStreamFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static InputStream createInputStream(RemoteFile remoteFile) {
  return remoteFile.new RemoteFileInputStream() {
    private boolean isClosed = false;
    @Override
    public synchronized void close() throws IOException {
      if (!isClosed) {
        remoteFile.close();
        isClosed = true;
      }
    }
  };
}
 
Example 4
Source File: SFTPStreamFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static OutputStream createOutputStream(RemoteFile remoteFile) {
  return remoteFile.new RemoteFileOutputStream(0, MAX_UNCONFIRMED_READ_WRITES) {
    private boolean isClosed = false;
    @Override
    public synchronized void close() throws IOException {
      if (!isClosed) {
        remoteFile.close();
        isClosed = true;
      }
    }
  };
}
 
Example 5
Source File: SFTPWriteFeature.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        final EnumSet<OpenMode> flags;
        if(status.isAppend()) {
            if(status.isExists()) {
                // No append flag. Otherwise the offset field of SSH_FXP_WRITE requests is ignored.
                flags = EnumSet.of(OpenMode.WRITE);
            }
            else {
                // Allocate offset
                flags = EnumSet.of(OpenMode.CREAT, OpenMode.WRITE);
            }
        }
        else {
            // A new file is created; if the file already exists, it is opened and truncated to preserve ownership of file.
            if(status.isExists()) {
                if(file.isSymbolicLink()) {
                    // Workaround for #7327
                    session.sftp().remove(file.getAbsolute());
                    flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE);
                }
                else {
                    flags = EnumSet.of(OpenMode.TRUNC, OpenMode.WRITE);
                }
            }
            else {
                flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE);
            }
        }
        final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags);
        final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status);
        if(log.isInfoEnabled()) {
            log.info(String.format("Using %d unconfirmed writes", maxUnconfirmedWrites));
        }
        if(log.isInfoEnabled()) {
            log.info(String.format("Skipping %d bytes", status.getOffset()));
        }
        // Open stream at offset
        return new VoidStatusOutputStream(handle.new RemoteFileOutputStream(status.getOffset(), maxUnconfirmedWrites) {
            private final AtomicBoolean close = new AtomicBoolean();

            @Override
            public void close() throws IOException {
                if(close.get()) {
                    log.warn(String.format("Skip double close of stream %s", this));
                    return;
                }
                try {
                    super.close();
                }
                finally {
                    handle.close();
                    close.set(true);
                }
            }
        });
    }
    catch(IOException e) {
        throw new SFTPExceptionMappingService().map("Upload {0} failed", e, file);
    }
}