KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > channel > StreamForwarder


1
2 package ch.ethz.ssh2.channel;
3
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.net.Socket JavaDoc;
8
9 /**
10  * A StreamForwarder forwards data between two given streams.
11  * If two StreamForwarder threads are used (one for each direction)
12  * then one can be configured to shutdown the underlying channel/socket
13  * if both threads have finished forwarding (EOF).
14  *
15  * @author Christian Plattner, plattner@inf.ethz.ch
16  * @version $Id: StreamForwarder.java,v 1.2 2006/02/13 21:19:25 cplattne Exp $
17  */

18 public class StreamForwarder extends Thread JavaDoc
19 {
20     OutputStream JavaDoc os;
21     InputStream JavaDoc is;
22     byte[] buffer = new byte[Channel.CHANNEL_BUFFER_SIZE];
23     Channel c;
24     StreamForwarder sibling;
25     Socket JavaDoc s;
26     String JavaDoc mode;
27
28     StreamForwarder(Channel c, StreamForwarder sibling, Socket JavaDoc s, InputStream JavaDoc is, OutputStream JavaDoc os, String JavaDoc mode)
29             throws IOException JavaDoc
30     {
31         this.is = is;
32         this.os = os;
33         this.mode = mode;
34         this.c = c;
35         this.sibling = sibling;
36         this.s = s;
37     }
38
39     public void run()
40     {
41         try
42         {
43             while (true)
44             {
45                 int len = is.read(buffer);
46                 if (len <= 0)
47                     break;
48                 os.write(buffer, 0, len);
49                 os.flush();
50             }
51         }
52         catch (IOException JavaDoc ignore)
53         {
54             try
55             {
56                 c.cm.closeChannel(c, "Closed due to exception in StreamForwarder (" + mode + "): "
57                         + ignore.getMessage(), true);
58             }
59             catch (IOException JavaDoc e)
60             {
61             }
62         }
63         finally
64         {
65             try
66             {
67                 os.close();
68             }
69             catch (IOException JavaDoc e1)
70             {
71             }
72             try
73             {
74                 is.close();
75             }
76             catch (IOException JavaDoc e2)
77             {
78             }
79
80             if (sibling != null)
81             {
82                 while (sibling.isAlive())
83                 {
84                     try
85                     {
86                         sibling.join();
87                     }
88                     catch (InterruptedException JavaDoc e)
89                     {
90                     }
91                 }
92
93                 try
94                 {
95                     c.cm.closeChannel(c, "StreamForwarder (" + mode + ") is cleaning up the connection", true);
96                 }
97                 catch (IOException JavaDoc e3)
98                 {
99                 }
100
101                 try
102                 {
103                     if (s != null)
104                         s.close();
105                 }
106                 catch (IOException JavaDoc e1)
107                 {
108                 }
109             }
110         }
111     }
112 }
Popular Tags