2121import java .io .OutputStream ;
2222import java .nio .charset .StandardCharsets ;
2323import java .util .Arrays ;
24+ import java .util .List ;
2425import java .util .Locale ;
2526import java .util .function .Function ;
2627
@@ -112,12 +113,28 @@ public static OS getRunning() {
112113
113114 /** Eagerly detects the native and running JVM properties. */
114115 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 ) {
115121 if (NATIVE_OS == null ) {
116- NATIVE_OS = calculateNative (systemProperty , environmentVariable );
122+ NATIVE_OS = calculateNative (systemProperty , environmentVariable , executeCommand );
117123 RUNNING_OS = calculateRunning (systemProperty );
118124 }
119125 }
120126
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+
121138 private static void detectPlatform () {
122139 if (NATIVE_OS == null ) {
123140 detectPlatform (System ::getProperty , System ::getenv );
@@ -127,13 +144,13 @@ private static void detectPlatform() {
127144 private static OS NATIVE_OS ;
128145
129146 /** 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 ) {
131148 String os_name = systemProperty .apply ("os.name" ).toLowerCase (Locale .getDefault ());
132149 boolean isWin = os_name .contains ("win" );
133150 boolean isMac = os_name .contains ("mac" );
134151 boolean isLinux = Arrays .asList ("nix" , "nux" , "aix" ).stream ().anyMatch (os_name ::contains );
135152 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 ;
137154 } else if (isWin ) {
138155 boolean is64bit = environmentVariable .apply ("ProgramFiles(x86)" ) != null ;
139156 return is64bit ? WIN_x64 : WIN_x86 ;
@@ -154,17 +171,6 @@ private static OS calculateNative(Function<String, String> systemProperty, Funct
154171 }
155172 }
156173
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-
168174 private static void drain (InputStream input , OutputStream output ) throws IOException {
169175 byte [] buf = new byte [1024 ];
170176 int numRead ;
0 commit comments