Home | History | Annotate | Download | only in ssh2
      1 /*
      2  * Copyright (c) 2011 David Kocher. All rights reserved.
      3  * Please refer to the LICENSE.txt for licensing details.
      4  */
      5 package ch.ethz.ssh2;
      6 
      7 import java.io.BufferedInputStream;
      8 import java.io.BufferedOutputStream;
      9 import java.io.IOException;
     10 import java.io.InputStream;
     11 
     12 import ch.ethz.ssh2.util.StringEncoder;
     13 
     14 /**
     15  * @version $Id:$
     16  */
     17 public class SCPOutputStream extends BufferedOutputStream
     18 {
     19 
     20 	private Session session;
     21 
     22 	private SCPClient scp;
     23 
     24 	public SCPOutputStream(SCPClient client, Session session, final String remoteFile, long length, String mode) throws IOException
     25 	{
     26 		super(session.getStdin(), 40000);
     27 		this.session = session;
     28 		this.scp = client;
     29 
     30 		InputStream is = new BufferedInputStream(session.getStdout(), 512);
     31 
     32 		scp.readResponse(is);
     33 
     34 		String cline = "C" + mode + " " + length + " " + remoteFile + "\n";
     35 
     36 		super.write(StringEncoder.GetBytes(cline));
     37 		this.flush();
     38 
     39 		scp.readResponse(is);
     40 	}
     41 
     42 	@Override
     43 	public void close() throws IOException
     44 	{
     45 		try
     46 		{
     47 			this.write(0);
     48 			this.flush();
     49 
     50 			scp.readResponse(session.getStdout());
     51 
     52 			this.write(StringEncoder.GetBytes("E\n"));
     53 			this.flush();
     54 		}
     55 		finally
     56 		{
     57 			if (session != null)
     58 				session.close();
     59 		}
     60 	}
     61 }
     62