diff options
author | Kenny Root <kenny@the-b.org> | 2015-07-18 11:30:18 -0700 |
---|---|---|
committer | Kenny Root <kenny@the-b.org> | 2015-07-18 11:30:18 -0700 |
commit | 2f5f3754dce85212a71138fd80c2300b73461908 (patch) | |
tree | ac51a1c9f143bc77a0cfec2e7da1b7c184394a98 /sshlib/src/main/java/com/trilead/ssh2/channel/StreamForwarder.java | |
parent | 0cf7ac30faecc82e04b080f418b08758624b07f5 (diff) | |
download | sshlib-2f5f3754dce85212a71138fd80c2300b73461908.tar.gz sshlib-2f5f3754dce85212a71138fd80c2300b73461908.tar.bz2 sshlib-2f5f3754dce85212a71138fd80c2300b73461908.zip |
Rename project to sshlib
Diffstat (limited to 'sshlib/src/main/java/com/trilead/ssh2/channel/StreamForwarder.java')
-rw-r--r-- | sshlib/src/main/java/com/trilead/ssh2/channel/StreamForwarder.java | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/sshlib/src/main/java/com/trilead/ssh2/channel/StreamForwarder.java b/sshlib/src/main/java/com/trilead/ssh2/channel/StreamForwarder.java new file mode 100644 index 0000000..e1afee8 --- /dev/null +++ b/sshlib/src/main/java/com/trilead/ssh2/channel/StreamForwarder.java @@ -0,0 +1,113 @@ + +package com.trilead.ssh2.channel; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +/** + * A StreamForwarder forwards data between two given streams. + * If two StreamForwarder threads are used (one for each direction) + * then one can be configured to shutdown the underlying channel/socket + * if both threads have finished forwarding (EOF). + * + * @author Christian Plattner, plattner@trilead.com + * @version $Id: StreamForwarder.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $ + */ +public class StreamForwarder extends Thread +{ + final OutputStream os; + final InputStream is; + final byte[] buffer = new byte[Channel.CHANNEL_BUFFER_SIZE]; + final Channel c; + final StreamForwarder sibling; + final Socket s; + final String mode; + + StreamForwarder(Channel c, StreamForwarder sibling, Socket s, InputStream is, OutputStream os, String mode) + throws IOException + { + this.is = is; + this.os = os; + this.mode = mode; + this.c = c; + this.sibling = sibling; + this.s = s; + } + + public void run() + { + try + { + while (true) + { + int len = is.read(buffer); + if (len <= 0) + break; + os.write(buffer, 0, len); + os.flush(); + } + } + catch (IOException ignore) + { + try + { + c.cm.closeChannel(c, "Closed due to exception in StreamForwarder (" + mode + "): " + + ignore.getMessage(), true); + } + catch (IOException e) + { + } + } + finally + { + try + { + os.close(); + } + catch (IOException e1) + { + } + try + { + is.close(); + } + catch (IOException e2) + { + } + + if (sibling != null) + { + while (sibling.isAlive()) + { + try + { + sibling.join(); + } + catch (InterruptedException e) + { + } + } + + try + { + c.cm.closeChannel(c, "StreamForwarder (" + mode + ") is cleaning up the connection", true); + } + catch (IOException e3) + { + } + } + + if (s != null) { + try + { + s.close(); + } + catch (IOException e1) + { + } + } + } + } +} |