view app/src/main/java/ch/ethz/ssh2/SFTPOutputStream.java @ 500:11d714165a2d

updates for android10+
author Carl Byington <carl@five-ten-sg.com>
date Tue, 07 Jun 2022 16:10:58 -0700
parents d29cce60f393
children
line wrap: on
line source

/*
 * Copyright (c) 2011 David Kocher. All rights reserved.
 * Please refer to the LICENSE.txt for licensing details.
 */
package ch.ethz.ssh2;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @version $Id: SFTPOutputStream.java 151 2014-04-28 10:03:39Z dkocher@sudo.ch $
 */
public class SFTPOutputStream extends OutputStream {

    private SFTPFileHandle handle;

    /**
     * Offset (in bytes) in the file to write
     */
    private long writeOffset = 0;

    public SFTPOutputStream(SFTPFileHandle handle) {
        this.handle = handle;
    }

    /**
     * Writes <code>len</code> bytes from the specified byte array
     * starting at offset <code>off</code> to this output stream.
     * The general contract for <code>write(b, off, len)</code> is that
     * some of the bytes in the array <code>b</code> are written to the
     * output stream in order; element <code>b[off]</code> is the first
     * byte written and <code>b[off+len-1]</code> is the last byte written
     * by this operation.
     *
     * @see SFTPClient#write(SFTPFileHandle, long, byte[], int, int)
     */
    @Override
    public void write(byte[] buffer, int offset, int len) throws IOException {
        // We can just blindly write the whole buffer at once.
        // if <code>len</code> &gt; 32768, then the write operation will
        // be split into multiple writes in SFTPv3Client#write.
        handle.getClient().write(handle, writeOffset, buffer, offset, len);
        writeOffset += len;
    }

    @Override
    public void write(int b) throws IOException {
        byte[] buffer = new byte[1];
        buffer[0] = (byte) b;
        handle.getClient().write(handle, writeOffset, buffer, 0, 1);
        writeOffset += 1;
    }

    public long skip(long n) {
        writeOffset += n;
        return n;
    }

    @Override
    public void close() throws IOException {
        handle.getClient().closeFile(handle);
    }
}