-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCodeBlocksProcessHelper.java
More file actions
134 lines (122 loc) · 4.58 KB
/
Copy pathCodeBlocksProcessHelper.java
File metadata and controls
134 lines (122 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Utility class for command execution and I/O redirection.
*
* @author halabelson@google.com (Hal Abelson)
*/
public final class CodeBlocksProcessHelper {
private static final boolean DEBUG = false;
/*
* Input stream handler used for stdout and stderr redirection.
*/
private static class RedirectStreamHandler extends Thread {
// Streams to redirect from and to
private final InputStream input;
private final StringBuffer output;
RedirectStreamHandler(InputStream input, StringBuffer output) {
this.input = input;
this.output = output;
start();
}
@Override
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line = reader.readLine()) != null) {
if (output != null) {
output.append(line).append("\n");
}
}
} catch (IOException ioe) {
// OK to ignore...
}
}
}
private CodeBlocksProcessHelper() {
}
/**
* Executes a command as a separate process.
*
* @param workingDir working directory for the command, can be null
* @param command command to execute and its arguments
* @param out where to redirect standard output, can be null
* @param err where to redirect standard error, can be null
* @param waitForOutput whether to wait for the output stream process
* @return {@code true} if the command succeeds, {@code false} otherwise
*/
private static boolean exec(File workingDir, String[] command, StringBuffer out,
StringBuffer err, boolean waitForOutput) {
try {
if (DEBUG) {
System.out.println("CodeBlocksProcessHelper exec private method on command");
}
Process process = Runtime.getRuntime().exec(command, null, workingDir);
// We create the out and err stream handlers even in the case where we don't wait
// for (or want) the output. The reason is that on (some?) Windows, if you
// don't handle stdout and stderr, a process that writes more than about
// 128 bytes will hang.
Thread outHandler = new RedirectStreamHandler(process.getInputStream(), out);
Thread errHandler = new RedirectStreamHandler(process.getErrorStream(), err);
int exitCode = process.waitFor();
if (waitForOutput) {
outHandler.join();
errHandler.join();
}
return exitCode == 0;
} catch (Exception e) {
System.out.println("CodeBlocksProcessHelper: " +
"Error trying to exec command: " + e.getMessage());
return false;
}
}
/**
* Executes a command as a separate process.
*
* @param command command to execute and its arguments
* @param waitForOutput whether to wait for the output stream process
* @return the process output. NOTE: The returned value and
* the thrown error message may be incorrect if waitForOutput is false
*/
public static String exec(String[] command, boolean waitForOutput) throws IOException {
if (DEBUG) {
System.out.println("CodeBlocksProcessHelper exec public method on command");
}
StringBuffer out = new StringBuffer();
StringBuffer err = new StringBuffer();
boolean result = exec(null, command, out, err, waitForOutput);
if (DEBUG) {
printIfNotEmpty(out.toString(), "command process response");
printIfNotEmpty(err.toString(), "command process error");
printIfNotEmpty(Boolean.toString(result), "command process result");
}
if (!result) {
throw new IOException(err.toString());
}
return out.toString();
}
static private void printIfNotEmpty(String output, String prefix) {
if (output.length() != 0) {
System.out.println(prefix + ": " + output);
}
}
// We can't just send a bare command string for Windows to exec. We need to
// explicitly invoke the command processor with cmd.exe
public static String execOnWindows(String commandString, boolean waitForOutput)
throws IOException {
String[] command = new String[] {"cmd.exe", "/C", commandString};
if (DEBUG) {
System.out.println("CodeBlocksProcessHelper execOnWindows: " + commandString);
}
return exec(command, waitForOutput);
}
}