Mercurial > 510Connectbot
annotate src/ch/ethz/ssh2/channel/StreamForwarder.java @ 416:07c1da90deab
avoid null pointer
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 27 Oct 2014 13:58:42 -0700 |
parents | 071eccdff8ea |
children |
rev | line source |
---|---|
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
1 /* |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved. |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
3 * Please refer to the LICENSE.txt for licensing details. |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
4 */ |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
5 package ch.ethz.ssh2.channel; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
6 |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
7 import java.io.IOException; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
8 import java.io.InputStream; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
9 import java.io.OutputStream; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
10 import java.net.Socket; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
11 |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
12 /** |
307 | 13 * A StreamForwarder forwards data between two given streams. |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
14 * If two StreamForwarder threads are used (one for each direction) |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
15 * then one can be configured to shutdown the underlying channel/socket |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
16 * if both threads have finished forwarding (EOF). |
307 | 17 * |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
18 * @author Christian Plattner |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
19 * @version 2.50, 03/15/10 |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
20 */ |
307 | 21 public class StreamForwarder extends Thread { |
22 OutputStream os; | |
23 InputStream is; | |
24 byte[] buffer = new byte[Channel.CHANNEL_BUFFER_SIZE]; | |
25 Channel c; | |
26 StreamForwarder sibling; | |
27 Socket s; | |
28 String mode; | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
29 |
307 | 30 StreamForwarder(Channel c, StreamForwarder sibling, Socket s, InputStream is, OutputStream os, String mode) |
31 throws IOException { | |
32 this.is = is; | |
33 this.os = os; | |
34 this.mode = mode; | |
35 this.c = c; | |
36 this.sibling = sibling; | |
37 this.s = s; | |
38 } | |
39 | |
40 @Override | |
41 public void run() { | |
42 try { | |
43 while (true) { | |
44 int len = is.read(buffer); | |
45 | |
46 if (len <= 0) | |
47 break; | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
48 |
307 | 49 os.write(buffer, 0, len); |
50 os.flush(); | |
51 } | |
52 } | |
53 catch (IOException e) { | |
54 try { | |
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 } | |
66 | |
67 try { | |
68 is.close(); | |
69 } | |
70 catch (IOException ignored) { | |
71 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
72 |
307 | 73 if (sibling != null) { |
74 while (sibling.isAlive()) { | |
75 try { | |
76 sibling.join(); | |
77 } | |
78 catch (InterruptedException ignored) { | |
79 } | |
80 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
81 |
307 | 82 try { |
83 c.cm.closeChannel(c, "StreamForwarder (" + mode + ") is cleaning up the connection", true); | |
84 } | |
85 catch (IOException ignored) { | |
86 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
87 |
307 | 88 try { |
89 if (s != null) | |
90 s.close(); | |
91 } | |
92 catch (IOException ignored) { | |
93 } | |
94 } | |
95 } | |
96 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
97 } |