bd8c2a31e584343b06ce741486c7b1a3bcae0170
[phpeclipse.git] /
1 /**********************************************************************
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.core.internal;
12
13 import java.io.*;
14 /**
15  * Monitor server I/O thread.
16  */
17 public class DefaultThread extends Thread {
18         private static final int BUFFER = 2048;
19         protected InputStream in;
20         protected OutputStream out;
21         protected boolean isRequest;
22         
23         protected Connection conn;
24         protected Request request;
25
26         /**
27          * MonitorThread constructor comment.
28          */
29         public DefaultThread(Connection conn, Request request, InputStream in, OutputStream out, boolean isRequest) {
30                 super();
31                 this.conn = conn;
32                 this.request = request;
33                 this.in = in;
34                 this.out = out;
35                 this.isRequest = isRequest;
36                 setPriority(Thread.NORM_PRIORITY + 1);
37                 setDaemon(true);
38         }
39
40         /**
41          * Listen for input, save it, and pass to the output stream.
42          */
43         public void run() {
44                 try {
45                         byte[] b = new byte[BUFFER];
46                         int n = in.read(b);
47                         while (n > 0) {
48                                 out.write(b, 0, n);
49                                 if (b != null && n > 0) {
50                                         byte[] x = null;
51                                         if (n == BUFFER)
52                                                 x = b;
53                                         else {
54                                                 x = new byte[n];
55                                                 System.arraycopy(b, 0, x, 0, n);
56                                         }
57                                         if (isRequest)
58                                                 request.addToRequest(x);
59                                         else
60                                                 request.addToResponse(x);
61                                 }
62                                 n = in.read(b);
63                                 Thread.yield();
64                         }
65                         out.flush();
66                 } catch (IOException e) {
67                 } finally {
68                         request.fireChangedEvent();
69                         if (!isRequest)
70                                 conn.close();
71                 }
72         }
73 }