comparison src/ch/ethz/ssh2/SCPInputStream.java @ 308:42b15aaa7ac7 ganymed

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