|
15 | 15 | */
|
16 | 16 | public final class ShellUtils {
|
17 | 17 |
|
| 18 | + private static final String LINE_SEP = System.getProperty("line.separator"); |
| 19 | + |
18 | 20 | private ShellUtils() {
|
19 | 21 | throw new UnsupportedOperationException("u can't instantiate me...");
|
20 | 22 | }
|
@@ -101,25 +103,29 @@ public static CommandResult execCmd(final String[] commands, final boolean isRoo
|
101 | 103 | for (String command : commands) {
|
102 | 104 | if (command == null) continue;
|
103 | 105 | os.write(command.getBytes());
|
104 |
| - os.writeBytes("\n"); |
| 106 | + os.writeBytes(LINE_SEP); |
105 | 107 | os.flush();
|
106 | 108 | }
|
107 |
| - os.writeBytes("exit\n"); |
| 109 | + os.writeBytes("exit" + LINE_SEP); |
108 | 110 | os.flush();
|
109 | 111 | result = process.waitFor();
|
110 | 112 | if (isNeedResultMsg) {
|
111 | 113 | successMsg = new StringBuilder();
|
112 | 114 | errorMsg = new StringBuilder();
|
113 | 115 | successResult = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
|
114 | 116 | errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
|
115 |
| - String s; |
116 |
| - while ((s = successResult.readLine()) != null) { |
117 |
| - successMsg.append(System.getProperty("line.separator")); |
118 |
| - successMsg.append(System.getProperty("line.separator")); |
| 117 | + String line; |
| 118 | + if ((line = successResult.readLine()) != null) { |
| 119 | + successMsg.append(line); |
| 120 | + while ((line = successResult.readLine()) != null) { |
| 121 | + successMsg.append(LINE_SEP).append(line); |
| 122 | + } |
119 | 123 | }
|
120 |
| - while ((s = errorResult.readLine()) != null) { |
121 |
| - errorMsg.append("\n"); |
122 |
| - errorMsg.append(s); |
| 124 | + if ((line = errorResult.readLine()) != null) { |
| 125 | + errorMsg.append(line); |
| 126 | + while ((line = errorResult.readLine()) != null) { |
| 127 | + errorMsg.append(LINE_SEP).append(line); |
| 128 | + } |
123 | 129 | }
|
124 | 130 | }
|
125 | 131 | } catch (Exception e) {
|
|
0 commit comments