1 | /**
|
---|
2 | * StreamGobbler.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.io.*;
|
---|
22 |
|
---|
23 | import org.expeditee.gui.MessageBay;
|
---|
24 |
|
---|
25 | // Base on: "When When Runtime.exec() won't"
|
---|
26 | // http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
|
---|
27 |
|
---|
28 | public class StreamGobbler extends Thread
|
---|
29 | {
|
---|
30 | public enum MessageBayType { display, error };
|
---|
31 |
|
---|
32 | class UpdateMessageBay implements Runnable {
|
---|
33 |
|
---|
34 | protected final MessageBayType type;
|
---|
35 | protected final String line;
|
---|
36 |
|
---|
37 | UpdateMessageBay(MessageBayType type, String line)
|
---|
38 | {
|
---|
39 | this.type = type;
|
---|
40 | this.line = line;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void run()
|
---|
44 | {
|
---|
45 | if (type == MessageBayType.display) {
|
---|
46 | MessageBay.displayMessage(line);
|
---|
47 | }
|
---|
48 | else {
|
---|
49 | MessageBay.errorMessage(line);
|
---|
50 | }
|
---|
51 |
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | InputStream is;
|
---|
58 | MessageBayType type;
|
---|
59 |
|
---|
60 |
|
---|
61 | public StreamGobbler(String threadName, InputStream is, MessageBayType type)
|
---|
62 | {
|
---|
63 | super(threadName);
|
---|
64 | this.is = is;
|
---|
65 | this.type = type;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void run()
|
---|
69 | {
|
---|
70 | try
|
---|
71 | {
|
---|
72 | InputStreamReader isr = new InputStreamReader(is);
|
---|
73 | BufferedReader br = new BufferedReader(isr);
|
---|
74 | String line=null;
|
---|
75 |
|
---|
76 | while ( (line = br.readLine()) != null)
|
---|
77 | {
|
---|
78 | // MessageBay is on the AWT event thread, so need to use 'invokeLater' to avoid thread deadlock
|
---|
79 | Runnable updateMessageBayTask = new UpdateMessageBay(type,line);
|
---|
80 | updateMessageBayTask.run();
|
---|
81 |
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | }
|
---|
86 | catch (IOException ioe) {
|
---|
87 | ioe.printStackTrace();
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|