comparison src/ch/ethz/ssh2/channel/StreamForwarder.java @ 273:91a31873c42a ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:21:46 -0700
parents
children 071eccdff8ea
comparison
equal deleted inserted replaced
272:ce2f4e397703 273:91a31873c42a
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.channel;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.net.Socket;
11
12 /**
13 * A StreamForwarder forwards data between two given streams.
14 * If two StreamForwarder threads are used (one for each direction)
15 * then one can be configured to shutdown the underlying channel/socket
16 * if both threads have finished forwarding (EOF).
17 *
18 * @author Christian Plattner
19 * @version 2.50, 03/15/10
20 */
21 public class StreamForwarder extends Thread
22 {
23 OutputStream os;
24 InputStream is;
25 byte[] buffer = new byte[Channel.CHANNEL_BUFFER_SIZE];
26 Channel c;
27 StreamForwarder sibling;
28 Socket s;
29 String mode;
30
31 StreamForwarder(Channel c, StreamForwarder sibling, Socket s, InputStream is, OutputStream os, String mode)
32 throws IOException
33 {
34 this.is = is;
35 this.os = os;
36 this.mode = mode;
37 this.c = c;
38 this.sibling = sibling;
39 this.s = s;
40 }
41
42 @Override
43 public void run()
44 {
45 try
46 {
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
83 if (sibling != null)
84 {
85 while (sibling.isAlive())
86 {
87 try
88 {
89 sibling.join();
90 }
91 catch (InterruptedException ignored)
92 {
93 }
94 }
95
96 try
97 {
98 c.cm.closeChannel(c, "StreamForwarder (" + mode + ") is cleaning up the connection", true);
99 }
100 catch (IOException ignored)
101 {
102 }
103
104 try
105 {
106 if (s != null)
107 s.close();
108 }
109 catch (IOException ignored)
110 {
111 }
112 }
113 }
114 }
115 }