-
Notifications
You must be signed in to change notification settings - Fork 15
/
ForwardThread.java
54 lines (47 loc) · 1.77 KB
/
ForwardThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* ForwardThread handles the TCP forwarding between a socket input stream (source)
* and a socket output stream (destination). It reads the input stream and forwards
* everything to the output stream. If some of the streams fails, the forwarding
* is stopped and the parent thread is notified to close all its connections.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ForwardThread extends Thread
{
private static final int READ_BUFFER_SIZE = 8192;
InputStream mInputStream = null;
OutputStream mOutputStream = null;
ForwardServerClientThread mParent = null;
/**
* Creates a new traffic forward thread specifying its input stream,
* output stream and parent thread
*/
public ForwardThread(ForwardServerClientThread aParent, InputStream aInputStream, OutputStream aOutputStream)
{
mInputStream = aInputStream;
mOutputStream = aOutputStream;
mParent = aParent;
}
/**
* Runs the thread. Until it is possible, reads the input stream and puts read
* data in the output stream. If reading can not be done (due to exception or
* when the stream is at his end) or writing is failed, exits the thread.
*/
public void run()
{
byte[] buffer = new byte[READ_BUFFER_SIZE];
try {
while (true) {
int bytesRead = mInputStream.read(buffer);
if (bytesRead == -1)
break; // End of stream is reached --> exit the thread
mOutputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
// Read/write failed --> connection is broken --> exit the thread
}
// Notify parent thread that the connection is broken and forwarding should stop
mParent.connectionBroken();
}
}