Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathShellUtils.java
More file actions
Latest commit
executable file
·218 lines (199 loc) · 7.38 KB
/
Copy pathShellUtils.java
File metadata and controls
executable file
·218 lines (199 loc) · 7.38 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
packagecom.cj.ScreenShotUtil;
importjava.io.BufferedReader;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.List;
/**
* ShellUtils
* <ul>
* <strong>Check root</strong>
* <li>{@link ShellUtils#checkRootPermission()}</li>
* </ul>
* <ul>
* <strong>Execte command</strong>
* <li>{@link ShellUtils#execCommand(String, boolean)}</li>
* <li>{@link ShellUtils#execCommand(String, boolean, boolean)}</li>
* <li>{@link ShellUtils#execCommand(List, boolean)}</li>
* <li>{@link ShellUtils#execCommand(List, boolean, boolean)}</li>
* <li>{@link ShellUtils#execCommand(String[], boolean)}</li>
* <li>{@link ShellUtils#execCommand(String[], boolean, boolean)}</li>
* </ul>
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16
*/
publicclassShellUtils {
publicstaticfinalStringCOMMAND_SU = "su";
publicstaticfinalStringCOMMAND_SH = "sh";
publicstaticfinalStringCOMMAND_EXIT = "exit\n";
publicstaticfinalStringCOMMAND_LINE_END = "\n";
/**
* check whether has root permission
*
* @return
*/
publicstaticbooleancheckRootPermission() {
returnexecCommand("echo root", true, false).result == 0;
}
/**
* execute shell command, default return result msg
*
* @param command command
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResultexecCommand(Stringcommand, booleanisRoot) {
returnexecCommand(newString[] {command}, isRoot, true);
}
/**
* execute shell commands, default return result msg
*
* @param commands command list
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResultexecCommand(List<String> commands, booleanisRoot) {
returnexecCommand(commands == null ? null : commands.toArray(newString[] {}), isRoot, true);
}
/**
* execute shell commands, default return result msg
*
* @param commands command array
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResultexecCommand(String[] commands, booleanisRoot) {
returnexecCommand(commands, isRoot, true);
}
/**
* execute shell command
*
* @param command command
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResultexecCommand(Stringcommand, booleanisRoot, booleanisNeedResultMsg) {
returnexecCommand(newString[] {command}, isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands command list
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResultexecCommand(List<String> commands, booleanisRoot, booleanisNeedResultMsg) {
returnexecCommand(commands == null ? null : commands.toArray(newString[] {}), isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands command array
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return <ul>
* <li>if isNeedResultMsg is false, {@link CommandResult#successMsg} is null and
* {@link CommandResult#errorMsg} is null.</li>
* <li>if {@link CommandResult#result} is -1, there maybe some excepiton.</li>
* </ul>
*/
publicstaticCommandResultexecCommand(String[] commands, booleanisRoot, booleanisNeedResultMsg) {
intresult = -1;
if (commands == null || commands.length == 0) {
returnnewCommandResult(result, null, null);
}
Processprocess = null;
BufferedReadersuccessResult = null;
BufferedReadererrorResult = null;
StringBuildersuccessMsg = null;
StringBuildererrorMsg = null;
DataOutputStreamos = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = newDataOutputStream(process.getOutputStream());
for (Stringcommand : commands) {
if (command == null) {
continue;
}
// donnot use os.writeBytes(commmand), avoid chinese charset error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result = process.waitFor();
// get command result
if (isNeedResultMsg) {
successMsg = newStringBuilder();
errorMsg = newStringBuilder();
successResult = newBufferedReader(newInputStreamReader(process.getInputStream()));
errorResult = newBufferedReader(newInputStreamReader(process.getErrorStream()));
Strings;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOExceptione) {
e.printStackTrace();
} catch (Exceptione) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOExceptione) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
returnnewCommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
}
/**
* result of command
* <ul>
* <li>{@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in
* linux shell</li>
* <li>{@link CommandResult#successMsg} means success message of command result</li>
* <li>{@link CommandResult#errorMsg} means error message of command result</li>
* </ul>
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16
*/
publicstaticclassCommandResult {
/** result of command **/
publicintresult;
/** success message of command result **/
publicStringsuccessMsg;
/** error message of command result **/
publicStringerrorMsg;
publicCommandResult(intresult) {
this.result = result;
}
publicCommandResult(intresult, StringsuccessMsg, StringerrorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
}
}