start of new file
[IRC.git] / Robust / src / Benchmarks / Jhttpp2 / Java / Jhttpp2Read.java
1 /* Written and copyright 2001-2003 Benjamin Kohl.\r
2  * Distributed under the GNU General Public License; see the README file.\r
3  * This code comes with NO WARRANTY.\r
4  */\r
5 \r
6 import java.io.BufferedInputStream;\r
7 import java.io.BufferedOutputStream;\r
8 import java.io.IOException;\r
9 /**\r
10         File: Jhttpp2Read.java\r
11         reads from a Jhttpp2ClientInputStream and writes to the BufferedOutputStream\r
12 \r
13         @author Benjamin Kohl\r
14 */\r
15 public class Jhttpp2Read extends Thread\r
16 {\r
17         private final int BUFFER_SIZE;\r
18         private BufferedInputStream in;\r
19         private BufferedOutputStream out;\r
20         private Jhttpp2HTTPSession connection;\r
21         private Jhttpp2Server server;\r
22 \r
23     public Jhttpp2Read(Jhttpp2Server server,Jhttpp2HTTPSession connection,BufferedInputStream l_in, BufferedOutputStream l_out) {\r
24         BUFFER_SIZE=96000;\r
25         in=l_in;\r
26         out=l_out;\r
27         this.connection=connection;\r
28         this.server=server;\r
29         start();\r
30     }\r
31     public void run() {\r
32         read();\r
33     }\r
34     private void read() {\r
35         int bytes_read=0;\r
36         byte[] buf=new byte[BUFFER_SIZE];\r
37         boolean cnt=true;\r
38         while(cnt) {\r
39             bytes_read=in.read(buf);\r
40             if (bytes_read!=-1) {\r
41                 out.write(buf,0,bytes_read);\r
42                 out.flush();\r
43                 server.addBytesRead(bytes_read);\r
44             } else cnt=false;\r
45         }\r
46         if (connection.getStatus()!=connection.SC_CONNECTING_TO_HOST) // *uaaahhh*: fixes a very strange bug\r
47             connection.getLocalSocket().close();\r
48         // why? If we are connecting to a new host (and this thread is already running!) , the upstream\r
49         // socket will be closed. So we get here and close our own downstream socket..... and the browser\r
50         // displays an empty page because jhttpp2\r
51         // closes the connection..... so close the downstream socket only when NOT connecting to a new host....\r
52     }\r
53     public void close() {\r
54         in.close();\r
55     }\r
56 }\r
57 \r
58 \r