import java.io.*;
import java.net.Socket;
import java.net.SocketException;

/**
 * Created by Xinan on 14/9/15.
 */
public class RequestHandlerThread extends Thread {

  final private Socket clientSocket;
  final private int BUFFER_SIZE;

  public RequestHandlerThread(Socket clientSocket) {
    this(clientSocket, 65536);
  }

  public RequestHandlerThread(Socket clientSocket, int bufferSize) {
    this.clientSocket = clientSocket;
    BUFFER_SIZE = bufferSize;
  }

  public void run() {
    try {
      try {
        HttpRequest clientRequest = new HttpRequest(clientSocket.getInputStream());
        BufferedOutputStream clientOut = new BufferedOutputStream(clientSocket.getOutputStream(), BUFFER_SIZE);
        System.out.printf("[%s] %s\n", clientSocket.getInetAddress(), clientRequest);

        Socket serverSocket = new Socket(clientRequest.getHost(), 80);

        BufferedInputStream serverIn = new BufferedInputStream(serverSocket.getInputStream(), BUFFER_SIZE);
        BufferedOutputStream serverOut = new BufferedOutputStream(serverSocket.getOutputStream(), BUFFER_SIZE);
        serverOut.write(clientRequest.toByteBuffer());
        serverOut.flush();

        int bytesRead;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = serverIn.read(buffer)) > 0) {
          clientOut.write(buffer, 0, bytesRead);
          clientOut.flush();
        }
        System.out.printf("Done. Active Thread: %d\r", Thread.activeCount());

        serverSocket.close();
        clientSocket.close();
      } catch (SocketException e) {
        System.out.printf("Client closed connection.\n");
      } catch (IOException e) {
        e.printStackTrace();
      } catch (MalformedRequestException e) {
        clientSocket.getOutputStream().write(e.getMessage().getBytes());
        clientSocket.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

@@ -499,22 +499,8 @@
 y %7B%0A
-      try %7B%0A  
     
@@ -573,26 +573,24 @@
 tStream());%0A
-  
       Buffer
@@ -689,26 +689,24 @@
 IZE);%0A      
- 
- 
 System.out.p
@@ -778,18 +778,16 @@
 %0A%0A      
-  
 Socket s
@@ -838,26 +838,24 @@
 st(), 80);%0A%0A
-  
       Buffer
@@ -950,26 +950,24 @@
 IZE);%0A      
-  
 BufferedOutp
@@ -1054,26 +1054,24 @@
 FFER_SIZE);%0A
-  
       server
@@ -1113,26 +1113,24 @@
 r());%0A      
-  
 serverOut.fl
@@ -1133,26 +1133,24 @@
 t.flush();%0A%0A
-  
       int by
@@ -1164,18 +1164,16 @@
 ;%0A      
-  
 byte%5B%5D b
@@ -1203,18 +1203,16 @@
 _SIZE%5D;%0A
-  
       wh
@@ -1259,34 +1259,32 @@
  %3E 0) %7B%0A        
-  
 clientOut.write(
@@ -1310,26 +1310,24 @@
 d);%0A        
-  
 clientOut.fl
@@ -1339,22 +1339,18 @@
 ;%0A      
- 
- 
 %7D%0A
-  
       Sy
@@ -1420,26 +1420,24 @@
 ());%0A%0A      
- 
- 
 serverSocket
@@ -1442,26 +1442,24 @@
 et.close();%0A
-  
       client
@@ -1470,34 +1470,32 @@
 et.close();%0A    
- 
- 
 %7D catch (SocketE
@@ -1504,26 +1504,24 @@
 eption e) %7B%0A
-  
       System
@@ -1564,26 +1564,24 @@
 n.%5Cn%22);%0A    
- 
- 
 %7D catch (IOE
@@ -1596,26 +1596,24 @@
  e) %7B%0A      
-  
 e.printStack
@@ -1617,26 +1617,24 @@
 ackTrace();%0A
-  
     %7D catch 
@@ -1661,24 +1661,36 @@
 eption e) %7B%0A
+      try %7B%0A
         clie
@@ -1787,22 +1787,16 @@
 %0A      %7D
-%0A    %7D
  catch (
@@ -1804,35 +1804,39 @@
 OException e
+x
 ) %7B%0A
+  
       e
+x
 .printStackT
@@ -1839,24 +1839,32 @@
 ackTrace();%0A
+      %7D%0A
     %7D%0A  %7D%0A%7D%0A

Tidy up `RequestHandlerThread`