Moved crawler to
[utils] / socketproxy / src / main / java / org / wamblee / socketproxy / ForwarderThread.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.socketproxy;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21
22 /**
23  * Forwarder thread which handles forwarding of an input stream to
24  * an output stream. 
25  *
26  * @author Erik Brakkee
27  */
28
29 public class ForwarderThread extends Thread {
30
31     private String _prefix;
32
33     private Barrier _barrier;
34
35     private InputStream _is;
36
37     private OutputStream _os;
38
39     /**
40      * Constructs the forwarder thread.
41      * @param aPrefix Prefix to use in the output. 
42      * @param aBarrier Barrier to block on before actually closing the stream. 
43      *        This is done to make sure that connections are only closed in th e
44      *        proxy when the forwarders in both directions are ready to close. 
45      * @param aIs Input stream to read from.
46      * @param aOs Output stream to forward to.
47      */
48     public ForwarderThread( String aPrefix, Barrier aBarrier,
49             InputStream aIs, OutputStream aOs ) {
50         _prefix = aPrefix;
51         _is = aIs;
52         _os = aOs;
53         _barrier = aBarrier;
54     }
55
56     public void run( ) {
57         boolean firstChar = true;
58         try {
59             int c = _is.read( );
60             while ( c > 0 ) {
61                 try {
62                     _os.write( c );
63                     _os.flush( );
64                     if ( firstChar ) {
65                         System.out.print( _prefix );
66                         firstChar = false;
67                     }
68                     System.out.print( (char) c );
69                     if ( c == '\n' ) {
70                         firstChar = true;
71                     }
72                 } catch ( IOException e ) {
73                 }
74
75                 c = _is.read( );
76             }
77         } catch ( IOException e ) {
78         }
79         closeStreams();
80     }
81
82     /**
83      * @param is
84      * @param os
85      */
86     private void closeStreams( ) {
87         _barrier.block( ); // wait until the other forwarder for the other direction
88                            // is also closed.
89         try {
90             _is.close( );
91         } catch ( IOException e1 ) {
92             // Empty.
93         }
94         try {
95             _os.flush( );
96             _os.close( );
97         } catch ( IOException e1 ) {
98             // Empty
99         }
100         System.out.println(_prefix + " closed");
101     }
102
103 }