All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pastdev.jsch.scp.ScpOutputStream Maven / Gradle / Ivy

Go to download

A set of extensions on top of JSch providing a full SCP protocol implementation, tunneling including multi-hop, a Java 7 FileSystem like implementation for Java 6 and remote command execution

There is a newer version: 0.1.11
Show newest version
package com.pastdev.jsch.scp;


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


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.jcraft.jsch.JSchException;
import com.pastdev.jsch.SessionFactory;


/**
 * Based upon information found here.
 * 
 * @author ltheisen
 * 
 */
public class ScpOutputStream extends OutputStream {
    private static Logger logger = LoggerFactory.getLogger( ScpOutputStream.class );

    private ScpConnection connection;
    private OutputStream outputStream;

    public ScpOutputStream( SessionFactory sessionFactory, String path, CopyMode copyMode ) throws JSchException, IOException {
        logger.debug( "Opening ScpOutputStream to {} {}", sessionFactory, path );
        this.connection = new ScpConnection( sessionFactory, path, ScpMode.TO, copyMode );
    }

    @Override
    public void close() throws IOException {
        logger.debug( "Closing ScpOutputStream" );
        connection.close();
        outputStream = null;
    }

    public void closeEntry() throws IOException {
        connection.closeEntry();
        outputStream = null;
    }

    public void putNextEntry( String name ) throws IOException {
        connection.putNextEntry( ScpEntry.newDirectory( name ) );
        outputStream = connection.getCurrentOuputStream();
    }

    public void putNextEntry( String name, long size ) throws IOException {
        connection.putNextEntry( ScpEntry.newFile( name, size ) );
        outputStream = connection.getCurrentOuputStream();
    }

    public void putNextEntry( ScpEntry entry ) throws IOException {
        connection.putNextEntry( entry );
        outputStream = connection.getCurrentOuputStream();
    }

    @Override
    public void write( int b ) throws IOException {
        if ( outputStream == null ) {
            throw new IllegalStateException( "no current entry, cannot write" );
        }
        outputStream.write( b );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy