KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import org.eclipse.debug.core.model.IStreamMonitor;
17 import org.eclipse.debug.core.model.IStreamsProxy;
18 import org.eclipse.debug.core.model.IStreamsProxy2;
19
20 /**
21  * Standard implementation of a streams proxy for s
22  * StreamsProxy
23  */

24
25 public class StreamsProxy implements IStreamsProxy, IStreamsProxy2 {
26     /**
27      * The monitor for the output stream (connected to standard out of the process)
28      */

29     private OutputStreamMonitor fOutputMonitor;
30     /**
31      * The monitor for the error stream (connected to standard error of the process)
32      */

33     private OutputStreamMonitor fErrorMonitor;
34     /**
35      * The monitor for the input stream (connected to standard in of the process)
36      */

37     private InputStreamMonitor fInputMonitor;
38     /**
39      * Records the open/closed state of communications with
40      * the underlying streams.
41      */

42     private boolean fClosed= false;
43     /**
44      * Creates a <code>StreamsProxy</code> on the streams
45      * of the given system process.
46      *
47      * @param process system process to create a streams proxy on
48      * @param encoding the process's encoding or <code>null</code> if default
49      */

50     public StreamsProxy(Process JavaDoc process, String JavaDoc encoding) {
51         if (process == null) {
52             return;
53         }
54         fOutputMonitor= new OutputStreamMonitor(process.getInputStream(), encoding);
55         fErrorMonitor= new OutputStreamMonitor(process.getErrorStream(), encoding);
56         fInputMonitor= new InputStreamMonitor(process.getOutputStream());
57         fOutputMonitor.startMonitoring();
58         fErrorMonitor.startMonitoring();
59         fInputMonitor.startMonitoring();
60     }
61
62     /**
63      * Causes the proxy to close all
64      * communications between it and the
65      * underlying streams after all remaining data
66      * in the streams is read.
67      */

68     public void close() {
69         if (!fClosed) {
70             fClosed= true;
71             fOutputMonitor.close();
72             fErrorMonitor.close();
73             fInputMonitor.close();
74         }
75     }
76
77     /**
78      * Causes the proxy to close all
79      * communications between it and the
80      * underlying streams immediately.
81      * Data remaining in the streams is lost.
82      */

83     public void kill() {
84         fClosed= true;
85         fOutputMonitor.kill();
86         fErrorMonitor.kill();
87         fInputMonitor.close();
88     }
89
90     /**
91      * @see IStreamsProxy#getErrorStreamMonitor()
92      */

93     public IStreamMonitor getErrorStreamMonitor() {
94         return fErrorMonitor;
95     }
96
97     /**
98      * @see IStreamsProxy#getOutputStreamMonitor()
99      */

100     public IStreamMonitor getOutputStreamMonitor() {
101         return fOutputMonitor;
102     }
103
104     /**
105      * @see IStreamsProxy#write(String)
106      */

107     public void write(String JavaDoc input) throws IOException JavaDoc {
108         if (!fClosed) {
109             fInputMonitor.write(input);
110         } else {
111             throw new IOException JavaDoc();
112         }
113     }
114
115     /* (non-Javadoc)
116      * @see org.eclipse.debug.core.model.IStreamsProxy2#closeInputStream()
117      */

118     public void closeInputStream() throws IOException JavaDoc {
119         if (!fClosed) {
120             fInputMonitor.closeInputStream();
121         } else {
122             throw new IOException JavaDoc();
123         }
124         
125     }
126
127 }
128
Popular Tags