comparison app/src/main/java/ch/ethz/ssh2/channel/StreamForwarder.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/channel/StreamForwarder.java@071eccdff8ea
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
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 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;
29
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;
48
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 }
72
73 if (sibling != null) {
74 while (sibling.isAlive()) {
75 try {
76 sibling.join();
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 }
97 }