From c7674a50bbaf74d3d327ed247784691826d406f4 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 19 Dec 2013 19:57:06 +0000 Subject: [PATCH] Some updates to Osal --- src/zutil/osal/OSAbstractionLayer.java | 47 ++++++++------------------ src/zutil/osal/OsalLinuxImpl.java | 4 +-- src/zutil/osal/OsalWindowsImpl.java | 3 +- 3 files changed, 18 insertions(+), 36 deletions(-) diff --git a/src/zutil/osal/OSAbstractionLayer.java b/src/zutil/osal/OSAbstractionLayer.java index da519bf..b3f8530 100644 --- a/src/zutil/osal/OSAbstractionLayer.java +++ b/src/zutil/osal/OSAbstractionLayer.java @@ -26,7 +26,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import java.util.LinkedList; +import java.util.ArrayList; /** * User: Ziver @@ -53,15 +53,15 @@ public abstract class OSAbstractionLayer { } /** - * Executes a command and returns the result + * Executes a command and returns the first line of the result * * @param cmd the command to run - * @return the first line of the output of the command or null if there where no output + * @return first line of the command */ - protected String runCommand(String cmd) throws InterruptedException, IOException { - LinkedList output = runCommand(cmd, 0, 1); - if(!output.isEmpty()) - return output.getFirst(); + protected String getFirstLineFromCommand(String cmd) throws InterruptedException, IOException { + String[] tmp = runCommand(cmd); + if(tmp.length > 1) + return tmp[0]; return null; } @@ -69,39 +69,22 @@ public abstract class OSAbstractionLayer { * Executes a command and returns the result * * @param cmd the command to run - * @param offsetLine the number of lines to skip at the beginning - * @return the first line after the offset of the output of the command or null if there where no output - */ - protected String runCommand(String cmd, int offsetLine) throws InterruptedException, IOException { - LinkedList output = runCommand(cmd, offsetLine, 1); - if(!output.isEmpty()) - return output.getFirst(); - return null; - } - - /** - * Executes a command and returns the result - * - * @param cmd the command to run - * @param offsetLine the number of lines to skip at the beginning - * @param lineCount the number of lines to read * @return a String list of the output of the command */ - protected LinkedList runCommand(String cmd, int offsetLine, int lineCount) throws InterruptedException, IOException { + public String[] runCommand(String cmd) throws InterruptedException, IOException { Runtime runtime = Runtime.getRuntime(); - Process proc = runtime.exec( cmd ); + Process proc = runtime.exec(cmd); proc.waitFor(); BufferedReader output = new BufferedReader(new InputStreamReader(proc.getInputStream())); - LinkedList ret = new LinkedList(); - String line = ""; - for(int i=0; (line = output.readLine()) != null && - (lineCount <= 0 || i < offsetLine+lineCount); i++) { - if(i >= offsetLine) - ret.addLast(line); + ArrayList ret = new ArrayList(); + String line; + while((line = output.readLine()) != null){ + ret.add(line); } output.close(); - return ret; + + return ret.toArray(new String[1]); } /** diff --git a/src/zutil/osal/OsalLinuxImpl.java b/src/zutil/osal/OsalLinuxImpl.java index 9d21603..d02dfef 100644 --- a/src/zutil/osal/OsalLinuxImpl.java +++ b/src/zutil/osal/OsalLinuxImpl.java @@ -46,7 +46,7 @@ public class OsalLinuxImpl extends OSAbstractionLayer { @Override public String getKernelVersion() { try{ - return super.runCommand("uname -r"); + return super.getFirstLineFromCommand("uname -r"); } catch(Exception e){ e.printStackTrace(); } @@ -56,7 +56,7 @@ public class OsalLinuxImpl extends OSAbstractionLayer { @Override public String getUsername() { try{ - return super.runCommand("whoami"); + return super.getFirstLineFromCommand("whoami"); } catch(Exception e){ e.printStackTrace(); } diff --git a/src/zutil/osal/OsalWindowsImpl.java b/src/zutil/osal/OsalWindowsImpl.java index 0dd6c9a..a2120b6 100644 --- a/src/zutil/osal/OsalWindowsImpl.java +++ b/src/zutil/osal/OsalWindowsImpl.java @@ -23,7 +23,6 @@ package zutil.osal; import java.io.File; -import java.io.IOException; /** * User: ezivkoc @@ -47,7 +46,7 @@ public class OsalWindowsImpl extends OSAbstractionLayer { @Override public String getKernelVersion() { try { - return runCommand("ver"); + return getFirstLineFromCommand("ver"); } catch (Exception e) { e.printStackTrace(); }