1 11 package org.eclipse.debug.internal.core; 12 13 14 import java.io.IOException ; 15 import java.io.OutputStream ; 16 import java.util.Vector ; 17 18 import org.eclipse.debug.core.DebugPlugin; 19 20 27 public class InputStreamMonitor { 28 29 32 private OutputStream fStream; 33 36 private Vector fQueue; 37 40 private Thread fThread; 41 44 private Object fLock; 45 46 49 private boolean fClosed = false; 50 51 57 public InputStreamMonitor(OutputStream stream) { 58 fStream= stream; 59 fQueue= new Vector (); 60 fLock= new Object (); 61 } 62 63 70 public void write(String text) { 71 synchronized(fLock) { 72 fQueue.add(text); 73 fLock.notifyAll(); 74 } 75 } 76 77 80 public void startMonitoring() { 81 if (fThread == null) { 82 fThread= new Thread (new Runnable () { 83 public void run() { 84 write(); 85 } 86 }, DebugCoreMessages.InputStreamMonitor_label); 87 fThread.setDaemon(true); 88 fThread.start(); 89 } 90 } 91 92 96 public void close() { 97 if (fThread != null) { 98 Thread thread= fThread; 99 fThread= null; 100 thread.interrupt(); 101 } 102 } 103 104 107 protected void write() { 108 while (fThread != null) { 109 writeNext(); 110 } 111 if (!fClosed) { 112 try { 113 fStream.close(); 114 } catch (IOException e) { 115 DebugPlugin.log(e); 116 } 117 } 118 } 119 120 123 protected void writeNext() { 124 while (!fQueue.isEmpty() && !fClosed) { 125 String text = (String )fQueue.firstElement(); 126 fQueue.removeElementAt(0); 127 try { 128 fStream.write(text.getBytes()); 129 fStream.flush(); 130 } catch (IOException e) { 131 DebugPlugin.log(e); 132 } 133 } 134 try { 135 synchronized(fLock) { 136 fLock.wait(); 137 } 138 } catch (InterruptedException e) { 139 } 140 } 141 142 148 public void closeInputStream() throws IOException { 149 if (!fClosed) { 150 fClosed = true; 151 fStream.close(); 152 } else { 153 throw new IOException (); 154 } 155 156 } 157 } 158 159 | Popular Tags |