1 package com.sslexplorer.requesthandler.test; 2 3 import java.io.InputStream ; 4 import java.io.InterruptedIOException ; 5 import java.io.OutputStream ; 6 import java.util.Random ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import com.sslexplorer.boot.RequestHandlerTunnel; 12 13 public class TestTunnel implements RequestHandlerTunnel { 14 15 public TestTunnel() { 16 super(); 17 } 19 20 21 private static Log log= LogFactory.getLog(TestTunnel.class); 22 23 private Thread _thread; 24 private int _timeoutMs; 25 private InputStream _in; 26 private OutputStream _out; 27 28 29 30 38 public void tunnel(InputStream in, OutputStream out) 39 { 40 41 Copy copy= new Copy(); 42 _in= in; 43 _out= out; 44 try 45 { 46 _thread= Thread.currentThread(); 47 copy.start(); 48 49 copydata(null, _out); 50 } 51 catch (Exception e) 52 { 53 } 54 finally 55 { 56 try 57 { 58 _in.close(); 59 60 } 61 catch (Exception e) 62 { 63 if (log.isDebugEnabled()) 64 log.debug("Failed to close tunnel.", e); 65 } 66 copy.interrupt(); 67 68 } 69 } 70 71 72 private void copydata(InputStream in, OutputStream out) throws java.io.IOException 73 { 74 long timestamp= 0; 75 while (true) 76 { 77 try 78 { 79 byte[] buf = new byte[32768]; 80 int read; 81 82 if(in!=null) { 83 while((read = in.read(buf)) > -1) { 84 } 86 } 87 else if(out!=null) { 88 Random rnd = new Random (); 89 while(true) { 90 rnd.nextBytes(buf); 91 out.write(buf); 92 } 93 } 94 timestamp= 0; 95 return; 96 } 97 catch (InterruptedIOException e) 98 { 99 if (timestamp == 0) 100 timestamp= System.currentTimeMillis(); 101 else if (_timeoutMs > 0 && (System.currentTimeMillis() - timestamp) > _timeoutMs) 102 throw e; 103 } 104 } 105 } 106 107 108 109 112 private class Copy extends Thread 113 { 114 public void run() 115 { 116 try 117 { 118 copydata(_in, null); 119 } 120 catch (Exception e) 121 { 122 if (log.isDebugEnabled()) 123 log.debug("Failed to copy data." , e); 124 } 125 finally 126 { 127 try 128 { 129 _out.close(); 130 } 131 catch (Exception e) 132 { 133 } 134 _thread.interrupt(); 135 } 136 } 137 } 138 139 public void close() { 140 141 } 142 } 143 | Popular Tags |