KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ReaderThread


1 /*
2  * ReaderThread.java
3  *
4  * Copyright (C) 2000-2004 Peter Graves
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */

20
21 package org.armedbear.j;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.UnsupportedEncodingException JavaDoc;
28
29 public class ReaderThread extends Thread JavaDoc
30 {
31     private char[] buf = new char[4096];
32     private InputStream JavaDoc inputStream;
33     private BufferedReader JavaDoc reader;
34     private boolean done = false;
35     private int timeOut = 10; // Milliseconds.
36

37     public ReaderThread(InputStream JavaDoc inputStream)
38     {
39         super("reader thread");
40         this.inputStream = inputStream;
41         String JavaDoc encoding =
42             Editor.preferences().getStringProperty(Property.DEFAULT_ENCODING);
43         try {
44             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream,
45                                                               encoding));
46         }
47         catch (UnsupportedEncodingException JavaDoc e) {
48             Log.debug(e);
49             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
50         }
51     }
52
53     public void setTimeOut(int n)
54     {
55         timeOut = n;
56     }
57
58     public void run()
59     {
60         while (!done) {
61             String JavaDoc s = read();
62             if (s == null)
63                 return;
64             update(filter(s));
65         }
66     }
67
68     public void cancel()
69     {
70         interrupt();
71         if (inputStream != null) {
72             try {
73                 inputStream.close();
74             }
75             catch (IOException JavaDoc e) {
76                 Log.error(e);
77             }
78             inputStream = null;
79         }
80     }
81
82     private String JavaDoc read()
83     {
84         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
85         try {
86             do {
87                 int numChars = reader.read(buf, 0, buf.length); // Blocks.
88
if (numChars < 0) {
89                     done = true;
90                     break;
91                 }
92                 if (numChars > 0)
93                     sb.append(buf, 0, numChars);
94                 Thread.sleep(timeOut);
95             }
96             while (reader.ready());
97         }
98         catch (IOException JavaDoc e) {
99             return null;
100         }
101         catch (InterruptedException JavaDoc e) {
102             return null;
103         }
104         catch (Throwable JavaDoc t) {
105             return null;
106         }
107         return sb.toString();
108     }
109
110     public String JavaDoc filter(String JavaDoc s)
111     {
112         return s;
113     }
114
115     public void update(String JavaDoc s)
116     {
117     }
118 }
119
Popular Tags