]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/compiler/CaptureStreamHandler.java
Changed spelling to manifest
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / compiler / CaptureStreamHandler.java
1 /*
2 *
3 * Copyright 2001-2004 The Ant-Contrib project
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
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;
24
25 import org.apache.tools.ant.taskdefs.Execute;
26 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
27 /**
28 * Implements ExecuteStreamHandler to capture the output of a Execute to an
29 * array of strings
30 *
31 * @author Curt Arnold
32 */
33 public class CaptureStreamHandler implements ExecuteStreamHandler {
34 /**
35 * Runs an executable and captures the output in a String array
36 *
37 * @param cmdline
38 * command line arguments
39 * @return output of process
40 */
41 public static String[] run(String[] cmdline) {
42 CaptureStreamHandler handler = new CaptureStreamHandler();
43 Execute exec = new Execute(handler);
44 exec.setCommandline(cmdline);
45 try {
46 int status = exec.execute();
47 } catch (IOException ex) {
48 }
49 return handler.getOutput();
50 }
51 private InputStream errorStream;
52 private InputStream fromProcess;
53 public CaptureStreamHandler() {
54 }
55 public String[] getOutput() {
56 String[] output;
57 if (fromProcess != null) {
58 Vector lines = new Vector(10);
59 try {
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();
65 if (line == null) {
66 reader = new BufferedReader(new InputStreamReader(
67 fromProcess));
68 break;
69 }
70 lines.addElement(line);
71 }
72 }
73 } catch (IOException ex) {
74 }
75 output = new String[lines.size()];
76 lines.copyInto(output);
77 return output;
78 }
79 output = new String[0];
80 return output;
81 }
82 /**
83 * Install a handler for the error stream of the subprocess.
84 *
85 * @param is
86 * input stream to read from the error stream from the
87 * subprocess
88 */
89 public void setProcessErrorStream(InputStream is) throws IOException {
90 errorStream = is;
91 }
92 /**
93 * Install a handler for the input stream of the subprocess.
94 *
95 * @param os
96 * output stream to write to the standard input stream of the
97 * subprocess
98 */
99 public void setProcessInputStream(OutputStream os) throws IOException {
100 os.close();
101 }
102 /**
103 * Install a handler for the output stream of the subprocess.
104 *
105 * @param is
106 * input stream to read from the error stream from the
107 * subprocess
108 */
109 public void setProcessOutputStream(InputStream is) throws IOException {
110 fromProcess = is;
111 }
112 /**
113 * Start handling of the streams.
114 */
115 public void start() throws IOException {
116 }
117 /**
118 * Stop handling of the streams - will not be restarted.
119 */
120 public void stop() {
121 }
122 }