comparison app/src/main/java/ch/ethz/ssh2/SCPInputStream.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/ch/ethz/ssh2/SCPInputStream.java@071eccdff8ea
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
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.OutputStream;
11
12 /**
13 * @version $Id: SCPInputStream.java 151 2014-04-28 10:03:39Z dkocher@sudo.ch $
14 */
15 public class SCPInputStream extends BufferedInputStream {
16 private Session session;
17
18 /**
19 * Bytes remaining to be read from the stream
20 */
21 private long remaining;
22
23 public SCPInputStream(SCPClient client, Session session) throws IOException {
24 super(session.getStdout());
25 this.session = session;
26 OutputStream os = new BufferedOutputStream(session.getStdin(), 512);
27 os.write(0x0);
28 os.flush();
29 final SCPClient.LenNamePair lnp;
30
31 while (true) {
32 int c = session.getStdout().read();
33
34 if (c < 0) {
35 throw new IOException("Remote scp terminated unexpectedly.");
36 }
37
38 String line = client.receiveLine(session.getStdout());
39
40 if (c == 'T') {
41 /* Ignore modification times */
42 continue;
43 }
44
45 if ((c == 1) || (c == 2)) {
46 throw new IOException("Remote SCP error: " + line);
47 }
48
49 if (c == 'C') {
50 lnp = client.parseCLine(line);
51 break;
52 }
53
54 throw new IOException("Remote SCP error: " + ((char) c) + line);
55 }
56
57 os.write(0x0);
58 os.flush();
59 this.remaining = lnp.length;
60 }
61
62 @Override
63 public int read() throws IOException {
64 if (!(remaining > 0)) {
65 return -1;
66 }
67
68 int b = super.read();
69
70 if (b < 0) {
71 throw new IOException("Remote scp terminated connection unexpectedly");
72 }
73
74 remaining -= 1;
75 return b;
76 }
77
78 @Override
79 public int read(byte b[], int off, int len) throws IOException {
80 if (!(remaining > 0)) {
81 return -1;
82 }
83
84 int trans = (int) remaining;
85
86 if (remaining > len) {
87 trans = len;
88 }
89
90 int read = super.read(b, off, trans);
91
92 if (read < 0) {
93 throw new IOException("Remote scp terminated connection unexpectedly");
94 }
95
96 remaining -= read;
97 return read;
98 }
99
100 @Override
101 public void close() throws IOException {
102 try {
103 session.getStdin().write(0x0);
104 session.getStdin().flush();
105 }
106 finally {
107 if (session != null) {
108 session.close();
109 }
110 }
111 }
112 }