|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.DataOutputStream; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * <pre> |
| 9 | + * author: Blankj |
| 10 | + * blog : http://blankj.com |
| 11 | + * time : 2016/08/07 |
| 12 | + * desc : utils about shell |
| 13 | + * </pre> |
| 14 | + */ |
| 15 | +public final class ShellUtils { |
| 16 | + |
| 17 | + private static final String LINE_SEP = System.getProperty("line.separator"); |
| 18 | + |
| 19 | + private ShellUtils() { |
| 20 | + throw new UnsupportedOperationException("u can't instantiate me..."); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Execute the command. |
| 25 | + * |
| 26 | + * @param command The command. |
| 27 | + * @return the single {@link CommandResult} instance |
| 28 | + */ |
| 29 | + public static CommandResult execCmd(final String command) { |
| 30 | + return execCmd(new String[]{command}, false, true); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Execute the command. |
| 35 | + * |
| 36 | + * @param command The command. |
| 37 | + * @param isRooted True to use root, false otherwise. |
| 38 | + * @return the single {@link CommandResult} instance |
| 39 | + */ |
| 40 | + public static CommandResult execCmd(final String command, final boolean isRooted) { |
| 41 | + return execCmd(new String[]{command}, isRooted, true); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Execute the command. |
| 46 | + * |
| 47 | + * @param commands The commands. |
| 48 | + * @return the single {@link CommandResult} instance |
| 49 | + */ |
| 50 | + public static CommandResult execCmd(final List<String> commands) { |
| 51 | + return execCmd(commands == null ? null : commands.toArray(new String[]{}), false, true); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Execute the command. |
| 56 | + * |
| 57 | + * @param commands The commands. |
| 58 | + * @param isRooted True to use root, false otherwise. |
| 59 | + * @return the single {@link CommandResult} instance |
| 60 | + */ |
| 61 | + public static CommandResult execCmd(final List<String> commands, final boolean isRooted) { |
| 62 | + return execCmd(commands == null ? null : commands.toArray(new String[]{}), isRooted, true); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Execute the command. |
| 67 | + * |
| 68 | + * @param commands The commands. |
| 69 | + * @return the single {@link CommandResult} instance |
| 70 | + */ |
| 71 | + public static CommandResult execCmd(final String[] commands) { |
| 72 | + return execCmd(commands, false, true); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Execute the command. |
| 77 | + * |
| 78 | + * @param commands The commands. |
| 79 | + * @param isRooted True to use root, false otherwise. |
| 80 | + * @return the single {@link CommandResult} instance |
| 81 | + */ |
| 82 | + public static CommandResult execCmd(final String[] commands, final boolean isRooted) { |
| 83 | + return execCmd(commands, isRooted, true); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Execute the command. |
| 88 | + * |
| 89 | + * @param command The command. |
| 90 | + * @param isRooted True to use root, false otherwise. |
| 91 | + * @param isNeedResultMsg True to return the message of result, false otherwise. |
| 92 | + * @return the single {@link CommandResult} instance |
| 93 | + */ |
| 94 | + public static CommandResult execCmd(final String command, |
| 95 | + final boolean isRooted, |
| 96 | + final boolean isNeedResultMsg) { |
| 97 | + return execCmd(new String[]{command}, isRooted, isNeedResultMsg); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Execute the command. |
| 102 | + * |
| 103 | + * @param commands The commands. |
| 104 | + * @param isRooted True to use root, false otherwise. |
| 105 | + * @param isNeedResultMsg True to return the message of result, false otherwise. |
| 106 | + * @return the single {@link CommandResult} instance |
| 107 | + */ |
| 108 | + public static CommandResult execCmd(final List<String> commands, |
| 109 | + final boolean isRooted, |
| 110 | + final boolean isNeedResultMsg) { |
| 111 | + return execCmd(commands == null ? null : commands.toArray(new String[]{}), |
| 112 | + isRooted, |
| 113 | + isNeedResultMsg); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Execute the command. |
| 118 | + * |
| 119 | + * @param commands The commands. |
| 120 | + * @param isRooted True to use root, false otherwise. |
| 121 | + * @param isNeedResultMsg True to return the message of result, false otherwise. |
| 122 | + * @return the single {@link CommandResult} instance |
| 123 | + */ |
| 124 | + public static CommandResult execCmd(final String[] commands, |
| 125 | + final boolean isRooted, |
| 126 | + final boolean isNeedResultMsg) { |
| 127 | + int result = -1; |
| 128 | + if (commands == null || commands.length == 0) { |
| 129 | + return new CommandResult(result, "", ""); |
| 130 | + } |
| 131 | + Process process = null; |
| 132 | + BufferedReader successResult = null; |
| 133 | + BufferedReader errorResult = null; |
| 134 | + StringBuilder successMsg = null; |
| 135 | + StringBuilder errorMsg = null; |
| 136 | + DataOutputStream os = null; |
| 137 | + try { |
| 138 | + process = Runtime.getRuntime().exec(isRooted ? "su" : "sh"); |
| 139 | + os = new DataOutputStream(process.getOutputStream()); |
| 140 | + for (String command : commands) { |
| 141 | + if (command == null) continue; |
| 142 | + os.write(command.getBytes()); |
| 143 | + os.writeBytes(LINE_SEP); |
| 144 | + os.flush(); |
| 145 | + } |
| 146 | + os.writeBytes("exit" + LINE_SEP); |
| 147 | + os.flush(); |
| 148 | + result = process.waitFor(); |
| 149 | + if (isNeedResultMsg) { |
| 150 | + successMsg = new StringBuilder(); |
| 151 | + errorMsg = new StringBuilder(); |
| 152 | + successResult = new BufferedReader( |
| 153 | + new InputStreamReader(process.getInputStream(), "UTF-8") |
| 154 | + ); |
| 155 | + errorResult = new BufferedReader( |
| 156 | + new InputStreamReader(process.getErrorStream(), "UTF-8") |
| 157 | + ); |
| 158 | + String line; |
| 159 | + if ((line = successResult.readLine()) != null) { |
| 160 | + successMsg.append(line); |
| 161 | + while ((line = successResult.readLine()) != null) { |
| 162 | + successMsg.append(LINE_SEP).append(line); |
| 163 | + } |
| 164 | + } |
| 165 | + if ((line = errorResult.readLine()) != null) { |
| 166 | + errorMsg.append(line); |
| 167 | + while ((line = errorResult.readLine()) != null) { |
| 168 | + errorMsg.append(LINE_SEP).append(line); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } catch (Exception e) { |
| 173 | + e.printStackTrace(); |
| 174 | + } finally { |
| 175 | + try { |
| 176 | + if (os != null) { |
| 177 | + os.close(); |
| 178 | + } |
| 179 | + } catch (IOException e) { |
| 180 | + e.printStackTrace(); |
| 181 | + } |
| 182 | + try { |
| 183 | + if (successResult != null) { |
| 184 | + successResult.close(); |
| 185 | + } |
| 186 | + } catch (IOException e) { |
| 187 | + e.printStackTrace(); |
| 188 | + } |
| 189 | + try { |
| 190 | + if (errorResult != null) { |
| 191 | + errorResult.close(); |
| 192 | + } |
| 193 | + } catch (IOException e) { |
| 194 | + e.printStackTrace(); |
| 195 | + } |
| 196 | + if (process != null) { |
| 197 | + process.destroy(); |
| 198 | + } |
| 199 | + } |
| 200 | + return new CommandResult( |
| 201 | + result, |
| 202 | + successMsg == null ? "" : successMsg.toString(), |
| 203 | + errorMsg == null ? "" : errorMsg.toString() |
| 204 | + ); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * The result of command. |
| 209 | + */ |
| 210 | + public static class CommandResult { |
| 211 | + public int result; |
| 212 | + public String successMsg; |
| 213 | + public String errorMsg; |
| 214 | + |
| 215 | + public CommandResult(final int result, final String successMsg, final String errorMsg) { |
| 216 | + this.result = result; |
| 217 | + this.successMsg = successMsg; |
| 218 | + this.errorMsg = errorMsg; |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public String toString() { |
| 223 | + return "result: " + result + "\n" + |
| 224 | + "successMsg: " + successMsg + "\n" + |
| 225 | + "errorMsg: " + errorMsg; |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments