KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > utils > StreamWatcher


1 package org.antlr.works.utils;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.InputStreamReader JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 /*
10
11 [The "BSD licence"]
12 Copyright (c) 2005-2006 Jean Bovet
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
18
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 3. The name of the author may not be used to endorse or promote products
25 derived from this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 */

39
40 public class StreamWatcher extends Thread JavaDoc {
41
42     protected InputStream JavaDoc is;
43     protected String JavaDoc type;
44     protected StreamWatcherDelegate delegate;
45     protected List JavaDoc<String JavaDoc> lines;
46
47     public StreamWatcher(InputStream JavaDoc is, String JavaDoc type, StreamWatcherDelegate delegate) {
48         this.is = is;
49         this.type = type;
50         this.delegate = delegate;
51         lines = new ArrayList JavaDoc<String JavaDoc>();
52     }
53
54     public List JavaDoc<String JavaDoc> getLines() {
55         return lines;
56     }
57
58     public void run() {
59         try {
60             if(delegate != null)
61                 delegate.streamWatcherDidStarted();
62
63             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
64             String JavaDoc line;
65             while ( (line = br.readLine()) != null) {
66                 lines.add(line);
67                 if(delegate != null)
68                     delegate.streamWatcherDidReceiveString(line+"\n");
69             }
70         } catch (IOException JavaDoc e) {
71             if(delegate != null)
72                 delegate.streamWatcherException(e);
73             else
74                 e.printStackTrace();
75         }
76     }
77 }
78
Popular Tags