1
2
3
4
5 package oshi.util;
6
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.nio.charset.Charset;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.List;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.sun.jna.Platform;
20
21 import oshi.annotation.concurrent.ThreadSafe;
22
23
24
25
26 @ThreadSafe
27 public final class ExecutingCommand {
28
29 private static final Logger LOG = LoggerFactory.getLogger(ExecutingCommand.class);
30
31 private static final String[] DEFAULT_ENV = getDefaultEnv();
32
33 private ExecutingCommand() {
34 }
35
36 private static String[] getDefaultEnv() {
37 if (Platform.isWindows()) {
38 return new String[] { "LANGUAGE=C" };
39 } else {
40 return new String[] { "LC_ALL=C" };
41 }
42 }
43
44
45
46
47
48
49
50
51
52
53 public static List<String> runNative(String cmdToRun) {
54 String[] cmd = cmdToRun.split(" ");
55 return runNative(cmd);
56 }
57
58
59
60
61
62
63
64
65
66
67 public static List<String> runNative(String[] cmdToRunWithArgs) {
68 return runNative(cmdToRunWithArgs, DEFAULT_ENV);
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public static List<String> runNative(String[] cmdToRunWithArgs, String[] envp) {
84 Process p = null;
85 try {
86 p = Runtime.getRuntime().exec(cmdToRunWithArgs, envp);
87 return getProcessOutput(p, cmdToRunWithArgs);
88 } catch (SecurityException | IOException e) {
89 LOG.trace("Couldn't run command {}: {}", Arrays.toString(cmdToRunWithArgs), e.getMessage());
90 } finally {
91
92 if (p != null) {
93
94
95 if (Platform.isWindows() || Platform.isSolaris()) {
96 try {
97 p.getOutputStream().close();
98 } catch (IOException e) {
99
100 }
101 try {
102 p.getInputStream().close();
103 } catch (IOException e) {
104
105 }
106 try {
107 p.getErrorStream().close();
108 } catch (IOException e) {
109
110 }
111 }
112 p.destroy();
113 }
114 }
115 return Collections.emptyList();
116 }
117
118 private static List<String> getProcessOutput(Process p, String[] cmd) {
119 ArrayList<String> sa = new ArrayList<>();
120 try (BufferedReader reader = new BufferedReader(
121 new InputStreamReader(p.getInputStream(), Charset.defaultCharset()))) {
122 String line;
123 while ((line = reader.readLine()) != null) {
124 sa.add(line);
125 }
126 p.waitFor();
127 } catch (IOException e) {
128 LOG.trace("Problem reading output from {}: {}", Arrays.toString(cmd), e.getMessage());
129 } catch (InterruptedException ie) {
130 LOG.trace("Interrupted while reading output from {}: {}", Arrays.toString(cmd), ie.getMessage());
131 Thread.currentThread().interrupt();
132 }
133 return sa;
134 }
135
136
137
138
139
140
141
142 public static String getFirstAnswer(String cmd2launch) {
143 return getAnswerAt(cmd2launch, 0);
144 }
145
146
147
148
149
150
151
152
153 public static String getAnswerAt(String cmd2launch, int answerIdx) {
154 List<String> sa = ExecutingCommand.runNative(cmd2launch);
155
156 if (answerIdx >= 0 && answerIdx < sa.size()) {
157 return sa.get(answerIdx);
158 }
159 return "";
160 }
161
162 }