source: trunk/src/org/expeditee/io/StreamGobbler.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

  • Property svn:executable set to *
File size: 2.5 KB
Line 
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
19package org.expeditee.io;
20
21
22import java.util.*;
23import java.io.*;
24
25import javax.swing.SwingUtilities;
26
27import org.expeditee.gui.MessageBay;
28
29// Base on: "When When Runtime.exec() won't"
30// http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
31
32public class StreamGobbler extends Thread
33{
34 public enum MessageBayType { display, error };
35
36 class UpdateMessageBay implements Runnable {
37
38 protected final MessageBayType type;
39 protected final String line;
40
41 UpdateMessageBay(MessageBayType type, String line)
42 {
43 this.type = type;
44 this.line = line;
45 }
46
47 public void run()
48 {
49 if (type == MessageBayType.display) {
50 MessageBay.displayMessage(line);
51 }
52 else {
53 MessageBay.errorMessage(line);
54 }
55
56 }
57 }
58
59
60
61 InputStream is;
62 MessageBayType type;
63
64
65 public StreamGobbler(String threadName, InputStream is, MessageBayType type)
66 {
67 super(threadName);
68 this.is = is;
69 this.type = type;
70 }
71
72 public void run()
73 {
74 try
75 {
76 InputStreamReader isr = new InputStreamReader(is);
77 BufferedReader br = new BufferedReader(isr);
78 String line=null;
79
80 while ( (line = br.readLine()) != null)
81 {
82 // MessageBay is on the AWT event thread, so need to use 'invokeLater' to avoid thread deadlock
83 Runnable updateMessageBayTask = new UpdateMessageBay(type,line);
84 SwingUtilities.invokeLater(updateMessageBayTask);
85
86 }
87
88
89 }
90 catch (IOException ioe) {
91 ioe.printStackTrace();
92 }
93 }
94}
95
Note: See TracBrowser for help on using the repository browser.