aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ch/ethz/ssh2/SCPOutputStream.java
blob: 8c511ea00c4139deb214430166def8d6badc4371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Copyright (c) 2011 David Kocher. All rights reserved.
 * Please refer to the LICENSE.txt for licensing details.
 */
package ch.ethz.ssh2;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;

import ch.ethz.ssh2.util.StringEncoder;

/**
 * @version $Id:$
 */
public class SCPOutputStream extends BufferedOutputStream
{

	private Session session;

	private SCPClient scp;

	public SCPOutputStream(SCPClient client, Session session, final String remoteFile, long length, String mode) throws IOException
	{
		super(session.getStdin(), 40000);
		this.session = session;
		this.scp = client;

		InputStream is = new BufferedInputStream(session.getStdout(), 512);

		scp.readResponse(is);

		String cline = "C" + mode + " " + length + " " + remoteFile + "\n";

		super.write(StringEncoder.GetBytes(cline));
		this.flush();

		scp.readResponse(is);
	}

	@Override
	public void close() throws IOException
	{
		try
		{
			this.write(0);
			this.flush();

			scp.readResponse(session.getStdout());

			this.write(StringEncoder.GetBytes("E\n"));
			this.flush();
		}
		finally
		{
			if (session != null)
				session.close();
		}
	}
}