1 4 package com.tc.admin.common; 5 6 import org.apache.commons.io.IOUtils; 7 import org.apache.commons.lang.StringUtils; 8 9 import java.io.BufferedReader ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.InputStreamReader ; 13 14 import javax.swing.SwingUtilities ; 15 16 public class StreamReader extends Thread { 17 TextPaneUpdater m_updater; 18 InputStream m_stream; 19 InputStreamReader m_streamReader; 20 BufferedReader m_bufferedReader; 21 boolean m_stop; 22 OutputStreamListener m_listener; 23 String m_trigger; 24 25 public StreamReader( 26 InputStream stream, 27 OutputStreamListener listener, 28 String trigger) 29 { 30 this(stream, null, listener, trigger); 31 } 32 33 public StreamReader( 34 InputStream stream, 35 TextPaneUpdater updater, 36 OutputStreamListener listener, 37 String trigger) 38 { 39 m_updater = updater; 40 m_stream = stream; 41 m_streamReader = new InputStreamReader (stream); 42 m_bufferedReader = new BufferedReader (m_streamReader); 43 m_stop = false; 44 m_listener = listener; 45 m_trigger = trigger; 46 } 47 48 public void setTriggerListener(OutputStreamListener listener) { 49 m_listener = listener; 50 } 51 52 public void setTrigger(String trigger) { 53 m_trigger = trigger; 54 } 55 56 public void run() { 57 String line; 58 59 while(!m_stop) { 60 try { 61 if((line = m_bufferedReader.readLine()) == null) { 62 IOUtils.closeQuietly(m_bufferedReader); 63 return; 64 } 65 else { 66 if(m_updater != null) { 67 update(line); 68 } 69 if(m_listener != null && m_trigger != null && 70 StringUtils.contains(line, m_trigger)) 71 { 72 SwingUtilities.invokeLater(new Runnable () { 73 public void run() { 74 m_listener.triggerEncountered(); 75 } 76 }); 77 } 78 continue; 79 } 80 } catch(IOException ioe) { 81 IOUtils.closeQuietly(m_bufferedReader); 82 return; 83 } 84 } 85 86 IOUtils.closeQuietly(m_bufferedReader); 87 } 88 89 private void update(String line) { 90 try { 91 m_updater.setLine(line); 92 SwingUtilities.invokeAndWait(m_updater); 93 } catch(Exception e) {} 94 } 95 96 public synchronized void finish() { 97 m_stop = true; 98 IOUtils.closeQuietly(m_bufferedReader); 99 } 100 } 101 | Popular Tags |