KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > execution > InputForwarder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.ruby.rubyproject.execution;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.BufferedWriter JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.lang.InterruptedException JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.openide.util.Exceptions;
31
32
33 /**
34  * <p>An InputForwarder takes user input in for example a Program I/O window,
35  * and sends it to a child process. In other words it "redirects" or forwards
36  * the input to the child process. This class doesn't actually care if the I/O
37  * is in a child process - it just forwards data to a given output stream.
38  * <p>
39  * Great care is taken to make this forwarding interruptible, such that it
40  * won't block if process execution is terminated or interrupted. To do this,
41  * it's generally going to sleep for short intervals when there is no available
42  * I/O rather than calling a blocking I/O operation.
43  *
44  *
45  * @author Tor Norbye
46  */

47 class InputForwarder implements Runnable JavaDoc {
48     private OutputStream JavaDoc out;
49     private Reader JavaDoc reader;
50     private FileLocator fileLocator;
51     private List JavaDoc<OutputRecognizer> recognizers;
52     private boolean cancelled;
53
54     public InputForwarder(OutputStream JavaDoc outstream, Reader JavaDoc in) {
55         out = outstream;
56         reader = in;
57         this.fileLocator = fileLocator;
58         this.recognizers = recognizers;
59     }
60
61     public void cancel() {
62         synchronized (this) {
63             cancelled = true;
64         }
65     }
66
67     public void run() {
68         BufferedReader JavaDoc input = null;
69         BufferedWriter JavaDoc output = null;
70
71         try {
72             input = new BufferedReader JavaDoc(reader);
73             output = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(out));
74
75             char[] buffer = new char[512];
76
77             while (true) {
78                 if (Thread.interrupted()) {
79                     return;
80                 }
81
82                 synchronized (this) {
83                     if (cancelled) {
84                         return;
85                     }
86                 }
87
88                 // At first I just simply had a loop on input.read() here, but
89
// that would sometimes block even after the process had exited,
90
// requiring input from the user before the output would be marked
91
// as complete. So instead I poll now on ready(), and when not,
92
// I just sleep.
93
try {
94                     Thread.sleep(300);
95                 } catch (InterruptedException JavaDoc ie) {
96                     return;
97                 }
98
99                 int lenRead;
100
101                 while ((input.ready()) && ((lenRead = input.read(buffer, 0, buffer.length)) > 0)) {
102                     output.write(buffer, 0, lenRead);
103                     output.flush();
104                 }
105             }
106         } catch (Exception JavaDoc ioe) {
107             Exceptions.printStackTrace(ioe);
108         } finally {
109             try {
110                 if (input != null) {
111                     input.close();
112                 }
113
114                 if (output != null) {
115                     output.close();
116                 }
117             } catch (Exception JavaDoc ioe) {
118                 Exceptions.printStackTrace(ioe);
119             }
120         }
121     }
122 }
123
Popular Tags