Last change
on this file since 458 was 458, checked in by davidb, 11 years ago |
Classes to support IDE.java
|
-
Property svn:executable
set to
*
|
File size:
1.8 KB
|
Line | |
---|
1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 |
|
---|
4 | import java.util.*;
|
---|
5 | import java.io.*;
|
---|
6 |
|
---|
7 | import javax.swing.SwingUtilities;
|
---|
8 |
|
---|
9 | import org.expeditee.gui.MessageBay;
|
---|
10 |
|
---|
11 | // Base on: "When When Runtime.exec() won't"
|
---|
12 | // http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
|
---|
13 |
|
---|
14 | public class StreamGobbler extends Thread
|
---|
15 | {
|
---|
16 | public enum MessageBayType { display, error };
|
---|
17 |
|
---|
18 | class UpdateMessageBay implements Runnable {
|
---|
19 |
|
---|
20 | protected final MessageBayType type;
|
---|
21 | protected final String line;
|
---|
22 |
|
---|
23 | UpdateMessageBay(MessageBayType type, String line)
|
---|
24 | {
|
---|
25 | this.type = type;
|
---|
26 | this.line = line;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void run()
|
---|
30 | {
|
---|
31 | if (type == MessageBayType.display) {
|
---|
32 | MessageBay.displayMessage(line);
|
---|
33 | }
|
---|
34 | else {
|
---|
35 | MessageBay.errorMessage(line);
|
---|
36 | }
|
---|
37 |
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | InputStream is;
|
---|
44 | MessageBayType type;
|
---|
45 |
|
---|
46 |
|
---|
47 | public StreamGobbler(String threadName, InputStream is, MessageBayType type)
|
---|
48 | {
|
---|
49 | super(threadName);
|
---|
50 | this.is = is;
|
---|
51 | this.type = type;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void run()
|
---|
55 | {
|
---|
56 | try
|
---|
57 | {
|
---|
58 | InputStreamReader isr = new InputStreamReader(is);
|
---|
59 | BufferedReader br = new BufferedReader(isr);
|
---|
60 | String line=null;
|
---|
61 |
|
---|
62 | while ( (line = br.readLine()) != null)
|
---|
63 | {
|
---|
64 | // MessageBay is on the AWT event thread, so need to use 'invokeLater' to avoid thread deadlock
|
---|
65 | Runnable updateMessageBayTask = new UpdateMessageBay(type,line);
|
---|
66 | SwingUtilities.invokeLater(updateMessageBayTask);
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | }
|
---|
72 | catch (IOException ioe) {
|
---|
73 | ioe.printStackTrace();
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.