KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > InputStreamMonitor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.core;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import org.eclipse.debug.core.DebugPlugin;
19
20 /**
21  * Writes to the input stream of a system process,
22  * queueing output if the stream is blocked.
23  *
24  * The input stream monitor writes to system in via
25  * an output stream.
26  */

27 public class InputStreamMonitor {
28     
29     /**
30      * The stream which is being written to (connected to system in).
31      */

32     private OutputStream JavaDoc fStream;
33     /**
34      * The queue of output.
35      */

36     private Vector JavaDoc fQueue;
37     /**
38      * The thread which writes to the stream.
39      */

40     private Thread JavaDoc fThread;
41     /**
42      * A lock for ensuring that writes to the queue are contiguous
43      */

44     private Object JavaDoc fLock;
45     
46     /**
47      * Whether the underlying output stream has been closed
48      */

49     private boolean fClosed = false;
50     
51     /**
52      * Creates an input stream monitor which writes
53      * to system in via the given output stream.
54      *
55      * @param stream output stream
56      */

57     public InputStreamMonitor(OutputStream JavaDoc stream) {
58         fStream= stream;
59         fQueue= new Vector JavaDoc();
60         fLock= new Object JavaDoc();
61     }
62     
63     /**
64      * Appends the given text to the stream, or
65      * queues the text to be written at a later time
66      * if the stream is blocked.
67      *
68      * @param text text to append
69      */

70     public void write(String JavaDoc text) {
71         synchronized(fLock) {
72             fQueue.add(text);
73             fLock.notifyAll();
74         }
75     }
76
77     /**
78      * Starts a thread which writes the stream.
79      */

80     public void startMonitoring() {
81         if (fThread == null) {
82             fThread= new Thread JavaDoc(new Runnable JavaDoc() {
83                 public void run() {
84                     write();
85                 }
86             }, DebugCoreMessages.InputStreamMonitor_label);
87             fThread.setDaemon(true);
88             fThread.start();
89         }
90     }
91     
92     /**
93      * Close all communications between this
94      * monitor and the underlying stream.
95      */

96     public void close() {
97         if (fThread != null) {
98             Thread JavaDoc thread= fThread;
99             fThread= null;
100             thread.interrupt();
101         }
102     }
103     
104     /**
105      * Continuously writes to the stream.
106      */

107     protected void write() {
108         while (fThread != null) {
109             writeNext();
110         }
111         if (!fClosed) {
112             try {
113                 fStream.close();
114             } catch (IOException JavaDoc e) {
115                 DebugPlugin.log(e);
116             }
117         }
118     }
119     
120     /**
121      * Write the text in the queue to the stream.
122      */

123     protected void writeNext() {
124         while (!fQueue.isEmpty() && !fClosed) {
125             String JavaDoc text = (String JavaDoc)fQueue.firstElement();
126             fQueue.removeElementAt(0);
127             try {
128                 fStream.write(text.getBytes());
129                 fStream.flush();
130             } catch (IOException JavaDoc e) {
131                 DebugPlugin.log(e);
132             }
133         }
134         try {
135             synchronized(fLock) {
136                 fLock.wait();
137             }
138         } catch (InterruptedException JavaDoc e) {
139         }
140     }
141
142     /**
143      * Closes the output stream attached to the standard input stream of this
144      * monitor's process.
145      *
146      * @exception IOException if an exception occurs closing the input stream
147      */

148     public void closeInputStream() throws IOException JavaDoc {
149         if (!fClosed) {
150             fClosed = true;
151             fStream.close();
152         } else {
153             throw new IOException JavaDoc();
154         }
155         
156     }
157 }
158
159
Popular Tags