KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > common > StreamReader


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.InputStreamReader JavaDoc;
13
14 import javax.swing.SwingUtilities JavaDoc;
15
16 public class StreamReader extends Thread JavaDoc {
17   TextPaneUpdater m_updater;
18   InputStream JavaDoc m_stream;
19   InputStreamReader JavaDoc m_streamReader;
20   BufferedReader JavaDoc m_bufferedReader;
21   boolean m_stop;
22   OutputStreamListener m_listener;
23   String JavaDoc m_trigger;
24
25   public StreamReader(
26     InputStream JavaDoc stream,
27     OutputStreamListener listener,
28     String JavaDoc trigger)
29   {
30     this(stream, null, listener, trigger);
31   }
32   
33   public StreamReader(
34     InputStream JavaDoc stream,
35     TextPaneUpdater updater,
36     OutputStreamListener listener,
37     String JavaDoc trigger)
38   {
39     m_updater = updater;
40     m_stream = stream;
41     m_streamReader = new InputStreamReader JavaDoc(stream);
42     m_bufferedReader = new BufferedReader JavaDoc(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 JavaDoc trigger) {
53     m_trigger = trigger;
54   }
55   
56   public void run() {
57     String JavaDoc 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 JavaDoc() {
73               public void run() {
74                 m_listener.triggerEncountered();
75               }
76             });
77           }
78           continue;
79         }
80       } catch(IOException JavaDoc ioe) {
81         IOUtils.closeQuietly(m_bufferedReader);
82         return;
83       }
84     }
85     
86     IOUtils.closeQuietly(m_bufferedReader);
87   }
88
89   private void update(String JavaDoc line) {
90     try {
91       m_updater.setLine(line);
92       SwingUtilities.invokeAndWait(m_updater);
93     } catch(Exception JavaDoc e) {/**/}
94   }
95   
96   public synchronized void finish() {
97     m_stop = true;
98     IOUtils.closeQuietly(m_bufferedReader);
99   }
100 }
101
Popular Tags