3 * Copyright 2001-2004 The Ant-Contrib project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package net
.sf
.antcontrib
.cpptasks
.compiler
;
18 import java
.io
.BufferedReader
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
21 import java
.io
.InputStreamReader
;
22 import java
.io
.OutputStream
;
23 import java
.util
.Vector
;
25 import org
.apache
.tools
.ant
.taskdefs
.Execute
;
26 import org
.apache
.tools
.ant
.taskdefs
.ExecuteStreamHandler
;
28 * Implements ExecuteStreamHandler to capture the output of a Execute to an
33 public class CaptureStreamHandler
implements ExecuteStreamHandler
{
35 * Runs an executable and captures the output in a String array
38 * command line arguments
39 * @return output of process
41 public static String
[] run(String
[] cmdline
) {
42 CaptureStreamHandler handler
= new CaptureStreamHandler();
43 Execute exec
= new Execute(handler
);
44 exec
.setCommandline(cmdline
);
46 int status
= exec
.execute();
47 } catch (IOException ex
) {
49 return handler
.getOutput();
51 private InputStream errorStream
;
52 private InputStream fromProcess
;
53 public CaptureStreamHandler() {
55 public String
[] getOutput() {
57 if (fromProcess
!= null) {
58 Vector lines
= new Vector(10);
60 BufferedReader reader
= new BufferedReader(
61 new InputStreamReader(errorStream
));
62 for (int i
= 0; i
< 2; i
++) {
63 for (int j
= 0; j
< 100; j
++) {
64 String line
= reader
.readLine();
66 reader
= new BufferedReader(new InputStreamReader(
70 lines
.addElement(line
);
73 } catch (IOException ex
) {
75 output
= new String
[lines
.size()];
76 lines
.copyInto(output
);
79 output
= new String
[0];
83 * Install a handler for the error stream of the subprocess.
86 * input stream to read from the error stream from the
89 public void setProcessErrorStream(InputStream is
) throws IOException
{
93 * Install a handler for the input stream of the subprocess.
96 * output stream to write to the standard input stream of the
99 public void setProcessInputStream(OutputStream os
) throws IOException
{
103 * Install a handler for the output stream of the subprocess.
106 * input stream to read from the error stream from the
109 public void setProcessOutputStream(InputStream is
) throws IOException
{
113 * Start handling of the streams.
115 public void start() throws IOException
{
118 * Stop handling of the streams - will not be restarted.