ShellUtils.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package hm.wine.seller.lib;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.List;
  7. /**
  8. * ShellUtils
  9. * <ul>
  10. * <strong>Check root</strong>
  11. * <li>{@link ShellUtils#checkRootPermission()}</li>
  12. * </ul>
  13. * <ul>
  14. * <strong>Execte command</strong>
  15. * <li>{@link ShellUtils#execCommand(String, boolean)}</li>
  16. * <li>{@link ShellUtils#execCommand(String, boolean, boolean)}</li>
  17. * <li>{@link ShellUtils#execCommand(List, boolean)}</li>
  18. * <li>{@link ShellUtils#execCommand(List, boolean, boolean)}</li>
  19. * <li>{@link ShellUtils#execCommand(String[], boolean)}</li>
  20. * <li>{@link ShellUtils#execCommand(String[], boolean, boolean)}</li>
  21. * </ul>
  22. *
  23. * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16
  24. */
  25. public class ShellUtils {
  26. public static final String COMMAND_SU = "su";
  27. public static final String COMMAND_SH = "sh";
  28. public static final String COMMAND_EXIT = "exit\n";
  29. public static final String COMMAND_LINE_END = "\n";
  30. //return ShellUtils.execAutoRoot("/system/bin/pm install -r " + apkPath);
  31. private ShellUtils() {
  32. throw new AssertionError();
  33. }
  34. /**
  35. * check whether has root permission
  36. *
  37. * @return
  38. */
  39. public static boolean checkRootPermission() {
  40. return execCommand("echo root", true, false).result == 0;
  41. }
  42. /**
  43. * execute shell command, default return result msg
  44. *
  45. * @param command command
  46. * @param isRoot whether need to run with root
  47. * @return
  48. * @see ShellUtils#execCommand(String[], boolean, boolean)
  49. */
  50. public static CommandResult execCommand(String command, boolean isRoot) {
  51. return execCommand(new String[]{command}, isRoot, true);
  52. }
  53. /**
  54. * execute shell commands, default return result msg
  55. *
  56. * @param commands command list
  57. * @param isRoot whether need to run with root
  58. * @return
  59. * @see ShellUtils#execCommand(String[], boolean, boolean)
  60. */
  61. public static CommandResult execCommand(List<String> commands, boolean isRoot) {
  62. return execCommand(commands == null ? null : commands.toArray(new String[]{}), isRoot, true);
  63. }
  64. /**
  65. * execute shell commands, default return result msg
  66. *
  67. * @param commands command array
  68. * @param isRoot whether need to run with root
  69. * @return
  70. * @see ShellUtils#execCommand(String[], boolean, boolean)
  71. */
  72. public static CommandResult execCommand(String[] commands, boolean isRoot) {
  73. return execCommand(commands, isRoot, true);
  74. }
  75. /**
  76. * execute shell command
  77. *
  78. * @param command command
  79. * @param isRoot whether need to run with root
  80. * @param isNeedResultMsg whether need result msg
  81. * @return
  82. * @see ShellUtils#execCommand(String[], boolean, boolean)
  83. */
  84. public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
  85. return execCommand(new String[]{command}, isRoot, isNeedResultMsg);
  86. }
  87. /**
  88. * execute shell commands
  89. *
  90. * @param commands command list
  91. * @param isRoot whether need to run with root
  92. * @param isNeedResultMsg whether need result msg
  93. * @return
  94. * @see ShellUtils#execCommand(String[], boolean, boolean)
  95. */
  96. public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {
  97. return execCommand(commands == null ? null : commands.toArray(new String[]{}), isRoot, isNeedResultMsg);
  98. }
  99. /**
  100. * execute shell commands
  101. *
  102. * @param commands command array
  103. * @param isRoot whether need to run with root
  104. * @param isNeedResultMsg whether need result msg
  105. * @return <ul>
  106. * <li>if isNeedResultMsg is false, {@link CommandResult#successMsg} is null and
  107. * {@link CommandResult#errorMsg} is null.</li>
  108. * <li>if {@link CommandResult#result} is -1, there maybe some excepiton.</li>
  109. * </ul>
  110. */
  111. public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
  112. int result = -1;
  113. if (commands == null || commands.length == 0) {
  114. return new CommandResult(result, null, null);
  115. }
  116. Process process = null;
  117. BufferedReader successResult = null;
  118. BufferedReader errorResult = null;
  119. StringBuilder successMsg = null;
  120. StringBuilder errorMsg = null;
  121. DataOutputStream os = null;
  122. try {
  123. process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
  124. os = new DataOutputStream(process.getOutputStream());
  125. for (String command : commands) {
  126. if (command == null) {
  127. continue;
  128. }
  129. // donnot use os.writeBytes(commmand), avoid chinese charset error
  130. os.write(command.getBytes());
  131. os.writeBytes(COMMAND_LINE_END);
  132. os.flush();
  133. }
  134. os.writeBytes(COMMAND_EXIT);
  135. os.flush();
  136. result = process.waitFor();
  137. // get command result
  138. if (isNeedResultMsg) {
  139. successMsg = new StringBuilder();
  140. errorMsg = new StringBuilder();
  141. successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
  142. errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  143. String s;
  144. while ((s = successResult.readLine()) != null) {
  145. successMsg.append(s);
  146. }
  147. while ((s = errorResult.readLine()) != null) {
  148. errorMsg.append(s);
  149. }
  150. }
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. } finally {
  156. try {
  157. if (os != null) {
  158. os.close();
  159. }
  160. if (successResult != null) {
  161. successResult.close();
  162. }
  163. if (errorResult != null) {
  164. errorResult.close();
  165. }
  166. } catch (IOException e) {
  167. e.printStackTrace();
  168. }
  169. if (process != null) {
  170. process.destroy();
  171. }
  172. }
  173. return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
  174. : errorMsg.toString());
  175. }
  176. public static CommandResult execAutoRoot(String cmd) {
  177. CommandResult commandResult = ShellUtils.execCommand(cmd, true);
  178. if (commandResult.result != 0) {
  179. commandResult = ShellUtils.execCommand(cmd, false);
  180. }
  181. return commandResult;
  182. }
  183. /**
  184. * result of command
  185. * <ul>
  186. * <li>{@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in
  187. * linux shell</li>
  188. * <li>{@link CommandResult#successMsg} means success message of command result</li>
  189. * <li>{@link CommandResult#errorMsg} means error message of command result</li>
  190. * </ul>
  191. *
  192. * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16
  193. */
  194. public static class CommandResult {
  195. /**
  196. * result of command
  197. **/
  198. public int result;
  199. /**
  200. * success message of command result
  201. **/
  202. public String successMsg;
  203. /**
  204. * error message of command result
  205. **/
  206. public String errorMsg;
  207. public CommandResult(int result) {
  208. this.result = result;
  209. }
  210. public CommandResult(int result, String successMsg, String errorMsg) {
  211. this.result = result;
  212. this.successMsg = successMsg;
  213. this.errorMsg = errorMsg;
  214. }
  215. }
  216. }