1 package com.sslexplorer.requesthandler.test; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.InterruptedIOException ; 6 import java.io.OutputStream ; 7 import java.util.Random ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import com.sslexplorer.boot.RequestHandlerTunnel; 13 import com.sslexplorer.boot.Util; 14 15 public class TestFullDuplexTunnel implements RequestHandlerTunnel { 16 17 18 private InputStream _in; 19 private OutputStream _out; 20 private Thread thread; 21 22 static Log log = LogFactory.getLog(TestFullDuplexTunnel.class); 23 24 public TestFullDuplexTunnel() { 25 26 } 27 28 29 public void close() { 30 31 32 } 33 34 35 43 public void tunnel(InputStream in, OutputStream out) 44 { 45 46 log.info("Starting new full duplex test tunnel"); 47 48 Copy copy= new Copy(); 49 50 try 51 { 52 thread= Thread.currentThread(); 53 _in = in; 54 _out = out; 55 copy.start(); 56 copydata(new RandomInputStream(), out); 57 } 58 catch (Exception e) 59 { 60 log.error("Error from full duplex test thread", e); 61 } 62 finally 63 { 64 Util.closeStream(_out); 65 Util.closeStream(_in); 66 copy.interrupt(); 67 68 log.info("Full duplex test therad is exiting"); 69 } 70 } 71 72 73 private void copydata(InputStream in, OutputStream out) throws java.io.IOException 74 { 75 long timestamp= 0; 76 while (true) 77 { 78 try 79 { 80 Util.copy(in, out); 81 timestamp= 0; 82 return; 83 } 84 catch (InterruptedIOException e) 85 { 86 87 } 88 } 89 } 90 91 92 93 96 private class Copy extends Thread 97 { 98 public void run() 99 { 100 try 101 { 102 copydata(_in, new RandomOutputStream()); 103 } 104 catch (Exception e) 105 { 106 } 108 finally 109 { 110 try 111 { 112 Util.closeStream(_out); 113 Util.closeStream(_in); 114 thread.interrupt(); 115 } 116 catch (Exception e) 117 { 118 } 119 } 120 121 log.info("Exiting run method of Copy thread"); 122 } 123 } 124 125 class RandomInputStream extends InputStream { 126 127 Random rnd = new Random (); 128 129 public int read() { 130 return rnd.nextInt(255); 131 } 132 133 public int read(byte[] buf, int off, int len) throws IOException { 134 135 rnd.nextBytes(buf); 136 137 return len; 138 } 139 } 140 141 142 class RandomOutputStream extends OutputStream { 143 144 Random rnd = new Random (); 145 146 public void write(int b) { 147 rnd.nextInt(); 148 } 149 150 public void write(byte[] buf, int off, int len) { 151 152 rnd.nextBytes(buf); 153 } 154 } 155 } 156 | Popular Tags |