1 2 package ch.ethz.ssh2.channel; 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.io.OutputStream ; 7 import java.net.Socket ; 8 9 18 public class StreamForwarder extends Thread 19 { 20 OutputStream os; 21 InputStream is; 22 byte[] buffer = new byte[Channel.CHANNEL_BUFFER_SIZE]; 23 Channel c; 24 StreamForwarder sibling; 25 Socket s; 26 String mode; 27 28 StreamForwarder(Channel c, StreamForwarder sibling, Socket s, InputStream is, OutputStream os, String mode) 29 throws IOException 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 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 e) 60 { 61 } 62 } 63 finally 64 { 65 try 66 { 67 os.close(); 68 } 69 catch (IOException e1) 70 { 71 } 72 try 73 { 74 is.close(); 75 } 76 catch (IOException e2) 77 { 78 } 79 80 if (sibling != null) 81 { 82 while (sibling.isAlive()) 83 { 84 try 85 { 86 sibling.join(); 87 } 88 catch (InterruptedException 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 e3) 98 { 99 } 100 101 try 102 { 103 if (s != null) 104 s.close(); 105 } 106 catch (IOException e1) 107 { 108 } 109 } 110 } 111 } 112 } | Popular Tags |