Organized imports
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.core / src / net / sourceforge / phpdt / monitor / core / internal / DefaultThread.java
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.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 /**
17  * Monitor server I/O thread.
18  */
19 public class DefaultThread extends Thread {
20         private static final int BUFFER = 2048;
21         protected InputStream in;
22         protected OutputStream out;
23         protected boolean isRequest;
24         
25         protected Connection conn;
26         protected Request request;
27
28         /**
29          * MonitorThread constructor comment.
30          */
31         public DefaultThread(Connection conn, Request request, InputStream in, OutputStream out, boolean isRequest) {
32                 super();
33                 this.conn = conn;
34                 this.request = request;
35                 this.in = in;
36                 this.out = out;
37                 this.isRequest = isRequest;
38                 setPriority(Thread.NORM_PRIORITY + 1);
39                 setDaemon(true);
40         }
41
42         /**
43          * Listen for input, save it, and pass to the output stream.
44          */
45         public void run() {
46                 try {
47                         byte[] b = new byte[BUFFER];
48                         int n = in.read(b);
49                         while (n > 0) {
50                                 out.write(b, 0, n);
51                                 if (b != null && n > 0) {
52                                         byte[] x = null;
53                                         if (n == BUFFER)
54                                                 x = b;
55                                         else {
56                                                 x = new byte[n];
57                                                 System.arraycopy(b, 0, x, 0, n);
58                                         }
59                                         if (isRequest)
60                                                 request.addToRequest(x);
61                                         else
62                                                 request.addToResponse(x);
63                                 }
64                                 n = in.read(b);
65                                 Thread.yield();
66                         }
67                         out.flush();
68                 } catch (IOException e) {
69                 } finally {
70                         request.fireChangedEvent();
71                         if (!isRequest)
72                                 conn.close();
73                 }
74         }
75 }