comparison src/ch/ethz/ssh2/channel/StreamForwarder.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
8 import java.io.InputStream; 8 import java.io.InputStream;
9 import java.io.OutputStream; 9 import java.io.OutputStream;
10 import java.net.Socket; 10 import java.net.Socket;
11 11
12 /** 12 /**
13 * A StreamForwarder forwards data between two given streams. 13 * A StreamForwarder forwards data between two given streams.
14 * If two StreamForwarder threads are used (one for each direction) 14 * If two StreamForwarder threads are used (one for each direction)
15 * then one can be configured to shutdown the underlying channel/socket 15 * then one can be configured to shutdown the underlying channel/socket
16 * if both threads have finished forwarding (EOF). 16 * if both threads have finished forwarding (EOF).
17 * 17 *
18 * @author Christian Plattner 18 * @author Christian Plattner
19 * @version 2.50, 03/15/10 19 * @version 2.50, 03/15/10
20 */ 20 */
21 public class StreamForwarder extends Thread 21 public class StreamForwarder extends Thread {
22 { 22 OutputStream os;
23 OutputStream os; 23 InputStream is;
24 InputStream is; 24 byte[] buffer = new byte[Channel.CHANNEL_BUFFER_SIZE];
25 byte[] buffer = new byte[Channel.CHANNEL_BUFFER_SIZE]; 25 Channel c;
26 Channel c; 26 StreamForwarder sibling;
27 StreamForwarder sibling; 27 Socket s;
28 Socket s; 28 String mode;
29 String mode;
30 29
31 StreamForwarder(Channel c, StreamForwarder sibling, Socket s, InputStream is, OutputStream os, String mode) 30 StreamForwarder(Channel c, StreamForwarder sibling, Socket s, InputStream is, OutputStream os, String mode)
32 throws IOException 31 throws IOException {
33 { 32 this.is = is;
34 this.is = is; 33 this.os = os;
35 this.os = os; 34 this.mode = mode;
36 this.mode = mode; 35 this.c = c;
37 this.c = c; 36 this.sibling = sibling;
38 this.sibling = sibling; 37 this.s = s;
39 this.s = s; 38 }
40 }
41 39
42 @Override 40 @Override
43 public void run() 41 public void run() {
44 { 42 try {
45 try 43 while (true) {
46 { 44 int len = is.read(buffer);
47 while (true)
48 {
49 int len = is.read(buffer);
50 if (len <= 0)
51 break;
52 os.write(buffer, 0, len);
53 os.flush();
54 }
55 }
56 catch (IOException e)
57 {
58 try
59 {
60 c.cm.closeChannel(c, e, true);
61 }
62 catch (IOException ignored)
63 {
64 }
65 }
66 finally
67 {
68 try
69 {
70 os.close();
71 }
72 catch (IOException ignored)
73 {
74 }
75 try
76 {
77 is.close();
78 }
79 catch (IOException ignored)
80 {
81 }
82 45
83 if (sibling != null) 46 if (len <= 0)
84 { 47 break;
85 while (sibling.isAlive())
86 {
87 try
88 {
89 sibling.join();
90 }
91 catch (InterruptedException ignored)
92 {
93 }
94 }
95 48
96 try 49 os.write(buffer, 0, len);
97 { 50 os.flush();
98 c.cm.closeChannel(c, "StreamForwarder (" + mode + ") is cleaning up the connection", true); 51 }
99 } 52 }
100 catch (IOException ignored) 53 catch (IOException e) {
101 { 54 try {
102 } 55 c.cm.closeChannel(c, e, true);
56 }
57 catch (IOException ignored) {
58 }
59 }
60 finally {
61 try {
62 os.close();
63 }
64 catch (IOException ignored) {
65 }
103 66
104 try 67 try {
105 { 68 is.close();
106 if (s != null) 69 }
107 s.close(); 70 catch (IOException ignored) {
108 } 71 }
109 catch (IOException ignored) 72
110 { 73 if (sibling != null) {
111 } 74 while (sibling.isAlive()) {
112 } 75 try {
113 } 76 sibling.join();
114 } 77 }
78 catch (InterruptedException ignored) {
79 }
80 }
81
82 try {
83 c.cm.closeChannel(c, "StreamForwarder (" + mode + ") is cleaning up the connection", true);
84 }
85 catch (IOException ignored) {
86 }
87
88 try {
89 if (s != null)
90 s.close();
91 }
92 catch (IOException ignored) {
93 }
94 }
95 }
96 }
115 } 97 }