21
21
import java .io .OutputStream ;
22
22
import java .nio .charset .StandardCharsets ;
23
23
import java .util .Arrays ;
24
+ import java .util .List ;
24
25
import java .util .Locale ;
25
26
import java .util .function .Function ;
26
27
@@ -112,12 +113,28 @@ public static OS getRunning() {
112
113
113
114
/** Eagerly detects the native and running JVM properties. */
114
115
public static void detectPlatform (Function <String , String > systemProperty , Function <String , String > environmentVariable ) {
116
+ detectPlatform (systemProperty , environmentVariable , OS ::exec );
117
+ }
118
+
119
+ /** Eagerly detects the native and running JVM properties. */
120
+ public static void detectPlatform (Function <String , String > systemProperty , Function <String , String > environmentVariable , Function <List <String >, String > executeCommand ) {
115
121
if (NATIVE_OS == null ) {
116
- NATIVE_OS = calculateNative (systemProperty , environmentVariable );
122
+ NATIVE_OS = calculateNative (systemProperty , environmentVariable , executeCommand );
117
123
RUNNING_OS = calculateRunning (systemProperty );
118
124
}
119
125
}
120
126
127
+ private static String exec (List <String > cmd ) {
128
+ try {
129
+ Process process = Runtime .getRuntime ().exec (cmd .toArray (new String [0 ]));
130
+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
131
+ drain (process .getInputStream (), output );
132
+ return new String (output .toByteArray (), StandardCharsets .UTF_8 );
133
+ } catch (IOException e ) {
134
+ throw new RuntimeException (e );
135
+ }
136
+ }
137
+
121
138
private static void detectPlatform () {
122
139
if (NATIVE_OS == null ) {
123
140
detectPlatform (System ::getProperty , System ::getenv );
@@ -127,13 +144,13 @@ private static void detectPlatform() {
127
144
private static OS NATIVE_OS ;
128
145
129
146
/** Calculates the native OS. */
130
- private static OS calculateNative (Function <String , String > systemProperty , Function <String , String > environmentVariable ) {
147
+ private static OS calculateNative (Function <String , String > systemProperty , Function <String , String > environmentVariable , Function < List < String >, String > executeCommand ) {
131
148
String os_name = systemProperty .apply ("os.name" ).toLowerCase (Locale .getDefault ());
132
149
boolean isWin = os_name .contains ("win" );
133
150
boolean isMac = os_name .contains ("mac" );
134
151
boolean isLinux = Arrays .asList ("nix" , "nux" , "aix" ).stream ().anyMatch (os_name ::contains );
135
152
if (isMac ) {
136
- return exec ( "uname" , "-a" ).contains ("_ARM64_" ) ? MAC_silicon : MAC_x64 ;
153
+ return executeCommand . apply ( List . of ( "uname" , "-a" ) ).contains ("_ARM64_" ) ? MAC_silicon : MAC_x64 ;
137
154
} else if (isWin ) {
138
155
boolean is64bit = environmentVariable .apply ("ProgramFiles(x86)" ) != null ;
139
156
return is64bit ? WIN_x64 : WIN_x86 ;
@@ -154,17 +171,6 @@ private static OS calculateNative(Function<String, String> systemProperty, Funct
154
171
}
155
172
}
156
173
157
- private static String exec (String ... cmd ) {
158
- try {
159
- Process process = Runtime .getRuntime ().exec (cmd );
160
- ByteArrayOutputStream output = new ByteArrayOutputStream ();
161
- drain (process .getInputStream (), output );
162
- return new String (output .toByteArray (), StandardCharsets .UTF_8 );
163
- } catch (IOException e ) {
164
- throw new RuntimeException (e );
165
- }
166
- }
167
-
168
174
private static void drain (InputStream input , OutputStream output ) throws IOException {
169
175
byte [] buf = new byte [1024 ];
170
176
int numRead ;
0 commit comments