class SFTPWriteFeature extends AppendWriteFeature<Void> {

    @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);
        }
    }

    public  SFTPWriteFeature(final SFTPSession session);

}