KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > util > StreamGobbler


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

19
20
21 package ca.mcgill.sable.soot.util;
22
23 import java.io.*;
24
25
26 import org.eclipse.swt.widgets.Display;
27
28 import ca.mcgill.sable.soot.*;
29 import ca.mcgill.sable.soot.launching.*;
30
31
32 public class StreamGobbler extends Thread JavaDoc {
33
34     public static final int OUTPUT_STREAM_TYPE = 0;
35     public static final int ERROR_STREAM_TYPE = 1;
36     
37     private InputStream is;
38     private int type;
39     private Display display;
40     
41     
42     public StreamGobbler(Display display, InputStream is, int type) {
43         setIs(is);
44         setType(type);
45         setDisplay(display);
46         
47     }
48     
49     public void run() {
50         try {
51             InputStreamReader isr = new InputStreamReader(getIs());
52             BufferedReader br = new BufferedReader(isr);
53             
54             while (true) {
55                 String JavaDoc temp = br.readLine();
56                 
57                 if (temp == null) break;
58         
59                 SootOutputEvent se = new SootOutputEvent(this, ISootOutputEventConstants.SOOT_NEW_TEXT_EVENT);
60                 se.setTextToAppend(temp);
61                 final SootOutputEvent toSend = se;
62
63                 getDisplay().asyncExec(new Runnable JavaDoc(){
64                     public void run() {
65                         SootPlugin.getDefault().fireSootOutputEvent(toSend);
66                         
67                     };
68                 });
69                 se = new SootOutputEvent(this, ISootOutputEventConstants.SOOT_NEW_TEXT_EVENT);
70                 se.setTextToAppend("\n");
71                 final SootOutputEvent newline = se;
72                 getDisplay().asyncExec(new Runnable JavaDoc(){
73                     public void run() {
74                         SootPlugin.getDefault().fireSootOutputEvent(newline);
75                     };
76                 });
77  
78             }
79         }
80         catch(IOException e1) {
81             System.out.println(e1.getMessage());
82         }
83     }
84     
85     /**
86      * Returns the is.
87      * @return InputStream
88      */

89     public InputStream getIs() {
90         return is;
91     }
92
93     /**
94      * Returns the type.
95      * @return int
96      */

97     public int getType() {
98         return type;
99     }
100
101     /**
102      * Sets the is.
103      * @param is The is to set
104      */

105     public void setIs(InputStream is) {
106         this.is = is;
107     }
108
109     /**
110      * Sets the type.
111      * @param type The type to set
112      */

113     public void setType(int type) {
114         this.type = type;
115     }
116
117     /**
118      * Returns the display.
119      * @return Display
120      */

121     public Display getDisplay() {
122         return display;
123     }
124
125     /**
126      * Sets the display.
127      * @param display The display to set
128      */

129     public void setDisplay(Display display) {
130         this.display = display;
131     }
132
133 }
134
Popular Tags