KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > LocalStreamForwarder


1
2 package ch.ethz.ssh2;
3
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7
8 import ch.ethz.ssh2.channel.Channel;
9 import ch.ethz.ssh2.channel.ChannelManager;
10 import ch.ethz.ssh2.channel.LocalAcceptThread;
11
12 /**
13  * A <code>LocalStreamForwarder</code> forwards an Input- and Outputstream
14  * pair via the secure tunnel to another host (which may or may not be identical
15  * to the remote SSH-2 server).
16  *
17  * @author Christian Plattner, plattner@inf.ethz.ch
18  * @version $Id: LocalStreamForwarder.java,v 1.6 2006/02/14 19:43:16 cplattne Exp $
19  */

20 public class LocalStreamForwarder
21 {
22     ChannelManager cm;
23
24     String JavaDoc host_to_connect;
25     int port_to_connect;
26     LocalAcceptThread lat;
27
28     Channel cn;
29
30     LocalStreamForwarder(ChannelManager cm, String JavaDoc host_to_connect, int port_to_connect) throws IOException JavaDoc
31     {
32         this.cm = cm;
33         this.host_to_connect = host_to_connect;
34         this.port_to_connect = port_to_connect;
35
36         cn = cm.openDirectTCPIPChannel(host_to_connect, port_to_connect, "127.0.0.1", 0);
37     }
38
39     /**
40      * @return An <code>InputStream</code> object.
41      * @throws IOException
42      */

43     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
44     {
45         return cn.getStdoutStream();
46     }
47
48     /**
49      * Get the OutputStream. Please be aware that the implementation MAY use an
50      * internal buffer. To make sure that the buffered data is sent over the
51      * tunnel, you have to call the <code>flush</code> method of the
52      * <code>OutputStream</code>. To signal EOF, please use the
53      * <code>close</code> method of the <code>OutputStream</code>.
54      *
55      * @return An <code>OutputStream</code> object.
56      * @throws IOException
57      */

58     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
59     {
60         return cn.getStdinStream();
61     }
62
63     /**
64      * Close the underlying SSH forwarding channel and free up resources.
65      * You can also use this method to force the shutdown of the underlying
66      * forwarding channel. Pending output (OutputStream not flushed) will NOT
67      * be sent. Pending input (InputStream) can still be read. If the shutdown
68      * operation is already in progress (initiated from either side), then this
69      * call is a no-op.
70      *
71      * @throws IOException
72      */

73     public void close() throws IOException JavaDoc
74     {
75         cm.closeChannel(cn, "Closed due to user request.", true);
76     }
77 }
78
Popular Tags