2 * Copyright 2007 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.socketproxy;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
23 * Forwarder thread which handles forwarding of an input stream to
26 * @author Erik Brakkee
29 public class ForwarderThread extends Thread {
31 private String _prefix;
33 private Barrier _barrier;
35 private InputStream _is;
37 private OutputStream _os;
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.
48 public ForwarderThread( String aPrefix, Barrier aBarrier,
49 InputStream aIs, OutputStream aOs ) {
57 boolean firstChar = true;
65 System.out.print( _prefix );
68 System.out.print( (char) c );
72 } catch ( IOException e ) {
77 } catch ( IOException e ) {
86 private void closeStreams( ) {
87 _barrier.block( ); // wait until the other forwarder for the other direction
91 } catch ( IOException e1 ) {
97 } catch ( IOException e1 ) {
100 System.out.println(_prefix + " closed");