X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=socketproxy%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsocketproxy%2FForwarderThread.java;fp=socketproxy%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsocketproxy%2FForwarderThread.java;h=53988b1f3216d93e1a7d55348efa53cecb745e40;hb=62f165891f08ae532b5a794af11d7338a93f9a43;hp=0000000000000000000000000000000000000000;hpb=07cedd3f0730646ea35a7f668b3e1e872a4605d9;p=utils diff --git a/socketproxy/src/main/java/org/wamblee/socketproxy/ForwarderThread.java b/socketproxy/src/main/java/org/wamblee/socketproxy/ForwarderThread.java new file mode 100644 index 00000000..53988b1f --- /dev/null +++ b/socketproxy/src/main/java/org/wamblee/socketproxy/ForwarderThread.java @@ -0,0 +1,86 @@ +package org.wamblee.socketproxy; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Forwarder thread which handles forwarding of an input stream to + * an output stream. + */ + +public class ForwarderThread extends Thread { + + private String _prefix; + + private Barrier _barrier; + + private InputStream _is; + + private OutputStream _os; + + /** + * Constructs the forwarder thread. + * @param aPrefix Prefix to use in the output. + * @param aBarrier Barrier to block on before actually closing the stream. + * This is done to make sure that connections are only closed in th e + * proxy when the forwarders in both directions are ready to close. + * @param aIs Input stream to read from. + * @param aOs Output stream to forward to. + */ + public ForwarderThread( String aPrefix, Barrier aBarrier, + InputStream aIs, OutputStream aOs ) { + _prefix = aPrefix; + _is = aIs; + _os = aOs; + _barrier = aBarrier; + } + + public void run( ) { + boolean firstChar = true; + try { + int c = _is.read( ); + while ( c > 0 ) { + try { + _os.write( c ); + _os.flush( ); + if ( firstChar ) { + System.out.print( _prefix ); + firstChar = false; + } + System.out.print( (char) c ); + if ( c == '\n' ) { + firstChar = true; + } + } catch ( IOException e ) { + } + + c = _is.read( ); + } + } catch ( IOException e ) { + } + closeStreams(); + } + + /** + * @param is + * @param os + */ + private void closeStreams( ) { + _barrier.block( ); // wait until the other forwarder for the other direction + // is also closed. + try { + _is.close( ); + } catch ( IOException e1 ) { + // Empty. + } + try { + _os.flush( ); + _os.close( ); + } catch ( IOException e1 ) { + // Empty + } + System.out.println(_prefix + " closed"); + } + +}