1 11 package org.eclipse.debug.internal.core; 12 13 14 import java.io.BufferedInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 18 import org.eclipse.core.runtime.ISafeRunnable; 19 import org.eclipse.core.runtime.ListenerList; 20 import org.eclipse.core.runtime.SafeRunner; 21 import org.eclipse.debug.core.DebugPlugin; 22 import org.eclipse.debug.core.IStreamListener; 23 import org.eclipse.debug.core.model.IFlushableStreamMonitor; 24 25 32 public class OutputStreamMonitor implements IFlushableStreamMonitor { 33 36 private InputStream fStream; 37 38 41 private ListenerList fListeners= new ListenerList(); 42 43 46 private boolean fBuffered = true; 47 48 51 private StringBuffer fContents; 52 53 56 private Thread fThread; 57 58 61 private static final int BUFFER_SIZE= 8192; 62 63 68 private boolean fKilled= false; 69 70 private long lastSleep; 71 72 private String fEncoding; 73 74 81 public OutputStreamMonitor(InputStream stream, String encoding) { 82 fStream = new BufferedInputStream (stream, 8192); 83 fEncoding = encoding; 84 fContents= new StringBuffer (); 85 } 86 87 90 public synchronized void addListener(IStreamListener listener) { 91 fListeners.add(listener); 92 } 93 94 99 protected void close() { 100 if (fThread != null) { 101 Thread thread= fThread; 102 fThread= null; 103 try { 104 thread.join(); 105 } catch (InterruptedException ie) { 106 } 107 fListeners = new ListenerList(); 108 } 109 } 110 111 115 private void fireStreamAppended(String text) { 116 getNotifier().notifyAppend(text); 117 } 118 119 122 public synchronized String getContents() { 123 return fContents.toString(); 124 } 125 126 134 private void read() { 135 lastSleep = System.currentTimeMillis(); 136 long currentTime = lastSleep; 137 byte[] bytes= new byte[BUFFER_SIZE]; 138 int read = 0; 139 while (read >= 0) { 140 try { 141 if (fKilled) { 142 break; 143 } 144 read= fStream.read(bytes); 145 if (read > 0) { 146 String text; 147 if (fEncoding != null) { 148 text = new String (bytes, 0, read, fEncoding); 149 } else { 150 text = new String (bytes, 0, read); 151 } 152 synchronized (this) { 153 if (isBuffered()) { 154 fContents.append(text); 155 } 156 fireStreamAppended(text); 157 } 158 } 159 } catch (IOException ioe) { 160 if (!fKilled) { 161 DebugPlugin.log(ioe); 162 } 163 return; 164 } catch (NullPointerException e) { 165 if (!fKilled && fThread != null) { 168 DebugPlugin.log(e); 169 } 170 return; 171 } 172 173 currentTime = System.currentTimeMillis(); 174 if (currentTime - lastSleep > 1000) { 175 lastSleep = currentTime; 176 try { 177 Thread.sleep(1); } catch (InterruptedException e) { 179 } 180 } 181 } 182 try { 183 fStream.close(); 184 } catch (IOException e) { 185 DebugPlugin.log(e); 186 } 187 } 188 189 protected void kill() { 190 fKilled= true; 191 } 192 193 196 public synchronized void removeListener(IStreamListener listener) { 197 fListeners.remove(listener); 198 } 199 200 203 protected void startMonitoring() { 204 if (fThread == null) { 205 fThread= new Thread (new Runnable () { 206 public void run() { 207 read(); 208 } 209 }, DebugCoreMessages.OutputStreamMonitor_label); 210 fThread.setDaemon(true); 211 fThread.setPriority(Thread.MIN_PRIORITY); 212 fThread.start(); 213 } 214 } 215 216 219 public synchronized void setBuffered(boolean buffer) { 220 fBuffered = buffer; 221 } 222 223 226 public synchronized void flushContents() { 227 fContents.setLength(0); 228 } 229 230 233 public synchronized boolean isBuffered() { 234 return fBuffered; 235 } 236 237 private ContentNotifier getNotifier() { 238 return new ContentNotifier(); 239 } 240 241 class ContentNotifier implements ISafeRunnable { 242 243 private IStreamListener fListener; 244 private String fText; 245 246 249 public void handleException(Throwable exception) { 250 DebugPlugin.log(exception); 251 } 252 253 256 public void run() throws Exception { 257 fListener.streamAppended(fText, OutputStreamMonitor.this); 258 } 259 260 public void notifyAppend(String text) { 261 if (text == null) 262 return; 263 fText = text; 264 Object [] copiedListeners= fListeners.getListeners(); 265 for (int i= 0; i < copiedListeners.length; i++) { 266 fListener = (IStreamListener) copiedListeners[i]; 267 SafeRunner.run(this); 268 } 269 fListener = null; 270 fText = null; 271 } 272 } 273 } 274 | Popular Tags |