]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /*\r |
2 | * \r | |
3 | * Copyright 2001-2004 The Ant-Contrib project\r | |
4 | *\r | |
5 | * Licensed under the Apache License, Version 2.0 (the "License");\r | |
6 | * you may not use this file except in compliance with the License.\r | |
7 | * You may obtain a copy of the License at\r | |
8 | *\r | |
9 | * http://www.apache.org/licenses/LICENSE-2.0\r | |
10 | *\r | |
11 | * Unless required by applicable law or agreed to in writing, software\r | |
12 | * distributed under the License is distributed on an "AS IS" BASIS,\r | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r | |
14 | * See the License for the specific language governing permissions and\r | |
15 | * limitations under the License.\r | |
16 | */\r | |
17 | package net.sf.antcontrib.cpptasks.compiler;\r | |
18 | import java.io.BufferedReader;\r | |
19 | import java.io.IOException;\r | |
20 | import java.io.InputStream;\r | |
21 | import java.io.InputStreamReader;\r | |
22 | import java.io.OutputStream;\r | |
23 | import java.util.Vector;\r | |
24 | \r | |
25 | import org.apache.tools.ant.taskdefs.Execute;\r | |
26 | import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;\r | |
27 | /**\r | |
28 | * Implements ExecuteStreamHandler to capture the output of a Execute to an\r | |
29 | * array of strings\r | |
30 | * \r | |
31 | * @author Curt Arnold\r | |
32 | */\r | |
33 | public class CaptureStreamHandler implements ExecuteStreamHandler {\r | |
34 | /**\r | |
35 | * Runs an executable and captures the output in a String array\r | |
36 | * \r | |
37 | * @param cmdline\r | |
38 | * command line arguments\r | |
39 | * @return output of process\r | |
40 | */\r | |
41 | public static String[] run(String[] cmdline) {\r | |
42 | CaptureStreamHandler handler = new CaptureStreamHandler();\r | |
43 | Execute exec = new Execute(handler);\r | |
44 | exec.setCommandline(cmdline);\r | |
45 | try {\r | |
46 | int status = exec.execute();\r | |
47 | } catch (IOException ex) {\r | |
48 | }\r | |
49 | return handler.getOutput();\r | |
50 | }\r | |
51 | private InputStream errorStream;\r | |
52 | private InputStream fromProcess;\r | |
53 | public CaptureStreamHandler() {\r | |
54 | }\r | |
55 | public String[] getOutput() {\r | |
56 | String[] output;\r | |
57 | if (fromProcess != null) {\r | |
58 | Vector lines = new Vector(10);\r | |
59 | try {\r | |
60 | BufferedReader reader = new BufferedReader(\r | |
61 | new InputStreamReader(errorStream));\r | |
62 | for (int i = 0; i < 2; i++) {\r | |
63 | for (int j = 0; j < 100; j++) {\r | |
64 | String line = reader.readLine();\r | |
65 | if (line == null) {\r | |
66 | reader = new BufferedReader(new InputStreamReader(\r | |
67 | fromProcess));\r | |
68 | break;\r | |
69 | }\r | |
70 | lines.addElement(line);\r | |
71 | }\r | |
72 | }\r | |
73 | } catch (IOException ex) {\r | |
74 | }\r | |
75 | output = new String[lines.size()];\r | |
76 | lines.copyInto(output);\r | |
77 | return output;\r | |
78 | }\r | |
79 | output = new String[0];\r | |
80 | return output;\r | |
81 | }\r | |
82 | /**\r | |
83 | * Install a handler for the error stream of the subprocess.\r | |
84 | * \r | |
85 | * @param is\r | |
86 | * input stream to read from the error stream from the\r | |
87 | * subprocess\r | |
88 | */\r | |
89 | public void setProcessErrorStream(InputStream is) throws IOException {\r | |
90 | errorStream = is;\r | |
91 | }\r | |
92 | /**\r | |
93 | * Install a handler for the input stream of the subprocess.\r | |
94 | * \r | |
95 | * @param os\r | |
96 | * output stream to write to the standard input stream of the\r | |
97 | * subprocess\r | |
98 | */\r | |
99 | public void setProcessInputStream(OutputStream os) throws IOException {\r | |
100 | os.close();\r | |
101 | }\r | |
102 | /**\r | |
103 | * Install a handler for the output stream of the subprocess.\r | |
104 | * \r | |
105 | * @param is\r | |
106 | * input stream to read from the error stream from the\r | |
107 | * subprocess\r | |
108 | */\r | |
109 | public void setProcessOutputStream(InputStream is) throws IOException {\r | |
110 | fromProcess = is;\r | |
111 | }\r | |
112 | /**\r | |
113 | * Start handling of the streams.\r | |
114 | */\r | |
115 | public void start() throws IOException {\r | |
116 | }\r | |
117 | /**\r | |
118 | * Stop handling of the streams - will not be restarted.\r | |
119 | */\r | |
120 | public void stop() {\r | |
121 | }\r | |
122 | }\r |