From f444bd5526f796bc46db135b7ad8d6327bb0bfc1 Mon Sep 17 00:00:00 2001 From: Laurent Clouet Date: Sun, 25 Jan 2015 19:07:32 +0000 Subject: [PATCH] Cleanup: use generic list instead of low level array --- src/com/sheepit/client/Client.java | 31 ++++++++++---------------- src/com/sheepit/client/Utils.java | 15 ------------- src/com/sheepit/client/os/Linux.java | 14 +++++++----- src/com/sheepit/client/os/Mac.java | 12 +++++----- src/com/sheepit/client/os/OS.java | 3 ++- src/com/sheepit/client/os/Windows.java | 3 ++- 6 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/com/sheepit/client/Client.java b/src/com/sheepit/client/Client.java index 4044029..178c758 100644 --- a/src/com/sheepit/client/Client.java +++ b/src/com/sheepit/client/Client.java @@ -32,10 +32,10 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; @@ -529,19 +529,16 @@ public class Client { size_command += 2; } - String[] command = new String[size_command]; + List command = new ArrayList(size_command); Map new_env = new HashMap(); new_env.put("BLENDER_USER_CONFIG", this.config.workingDirectory.getAbsolutePath().replace("\\", "\\\\")); - int index = 0; for (String arg : command1) { if (arg.equals(".c")) { - command[index] = ajob.getScenePath(); - index += 1; - command[index] = "-P"; - index += 1; + command.add(ajob.getScenePath()); + command.add("-P"); try { script_file = File.createTempFile("script_", "", this.config.workingDirectory); @@ -556,7 +553,7 @@ public class Client { out.write("\n"); // GPU part out.close(); - command[index] = script_file.getAbsolutePath(); + command.add(script_file.getAbsolutePath()); } catch (IOException e) { return Error.Type.UNKNOWN; @@ -564,26 +561,22 @@ public class Client { script_file.deleteOnExit(); } else if (arg.equals(".e")) { - command[index] = ajob.getRendererPath(); + command.add(ajob.getRendererPath()); // the number of cores has to be put after the binary and before the scene arg if (this.config.getNbCores() > 0) { - index += 1; - command[index] = "-t"; - index += 1; - command[index] = Integer.toString(this.config.getNbCores()); - //index += 1; // do not do it, it will be done at the end of the loop + command.add("-t"); + command.add(Integer.toString(this.config.getNbCores())); } } else if (arg.equals(".o")) { - command[index] = this.config.workingDirectory.getAbsolutePath() + File.separator + ajob.getPrefixOutputImage(); + command.add(this.config.workingDirectory.getAbsolutePath() + File.separator + ajob.getPrefixOutputImage()); } else if (arg.equals(".f")) { - command[index] = ajob.getFrameNumber(); + command.add(ajob.getFrameNumber()); } else { - command[index] = arg; + command.add(arg); } - index += 1; } long rending_start = new Date().getTime(); @@ -591,7 +584,7 @@ public class Client { int nb_lines = 0; try { String line; - this.log.debug(Arrays.toString(command)); + this.log.debug(command.toString()); OS os = OS.getOS(); ajob.setProcess(os.exec(command, new_env)); BufferedReader input = new BufferedReader(new InputStreamReader(ajob.getProcess().getInputStream())); diff --git a/src/com/sheepit/client/Utils.java b/src/com/sheepit/client/Utils.java index 0cc36aa..70ce893 100644 --- a/src/com/sheepit/client/Utils.java +++ b/src/com/sheepit/client/Utils.java @@ -28,7 +28,6 @@ import java.io.InputStream; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.ZipEntry; @@ -233,18 +232,4 @@ public class Utils { } return Math.round(Double.parseDouble(m.group(1)) * scale); } - - public static T[] concatAll(T[] first, T[]... rest) { - int totalLength = first.length; - for (T[] array : rest) { - totalLength += array.length; - } - T[] result = Arrays.copyOf(first, totalLength); - int offset = first.length; - for (T[] array : rest) { - System.arraycopy(array, 0, result, offset, array.length); - offset += array.length; - } - return result; - } } diff --git a/src/com/sheepit/client/os/Linux.java b/src/com/sheepit/client/os/Linux.java index 9e7b4a5..1d965b9 100644 --- a/src/com/sheepit/client/os/Linux.java +++ b/src/com/sheepit/client/os/Linux.java @@ -21,11 +21,11 @@ package com.sheepit.client.os; import java.io.File; import java.io.IOException; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Scanner; import com.sheepit.client.Log; -import com.sheepit.client.Utils; import com.sheepit.client.hardware.cpu.CPU; public class Linux extends OS { @@ -122,7 +122,7 @@ public class Linux extends OS { } @Override - public Process exec(String[] command, Map env_overight) throws IOException { + public Process exec(List command, Map env_overight) throws IOException { // the renderer have a lib directory so add to the LD_LIBRARY_PATH // (even if we are not sure that it is the renderer who is launch @@ -130,7 +130,7 @@ public class Linux extends OS { new_env.putAll(java.lang.System.getenv()); // clone the env Boolean has_ld_library_path = new_env.containsKey("LD_LIBRARY_PATH"); - String lib_dir = (new File(command[0])).getParent() + File.separator + "lib"; + String lib_dir = (new File(command.get(0))).getParent() + File.separator + "lib"; String new_ld_library_path = "/lib:/lib64:/usr/lib:/usr/lib64:/lib/i386-linux-gnu:/lib/x86_64-linux-gnu:/usr/share/local" + ":" + lib_dir; if (has_ld_library_path == false) { new_env.put("LD_LIBRARY_PATH", new_ld_library_path); @@ -139,13 +139,15 @@ public class Linux extends OS { new_env.put("LD_LIBRARY_PATH", new_env.get("LD_LIBRARY_PATH") + ":" + new_ld_library_path); } - String[] actual_command = command; + List actual_command = command; if (this.hasNiceBinary == null) { this.checkNiceAvailability(); } if (this.hasNiceBinary.booleanValue()) { - String[] low = { NICE_BINARY_PATH, "-n", "19" }; // launch the process in lowest priority - actual_command = Utils.concatAll(low, command); + // launch the process in lowest priority + actual_command.add(0, "19"); + actual_command.add(0, "-n"); + actual_command.add(0, NICE_BINARY_PATH); } else { Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority"); diff --git a/src/com/sheepit/client/os/Mac.java b/src/com/sheepit/client/os/Mac.java index b4bc02a..fba30cc 100644 --- a/src/com/sheepit/client/os/Mac.java +++ b/src/com/sheepit/client/os/Mac.java @@ -22,10 +22,10 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.util.List; import java.util.Map; import com.sheepit.client.Log; -import com.sheepit.client.Utils; import com.sheepit.client.hardware.cpu.CPU; public class Mac extends OS { @@ -141,14 +141,16 @@ public class Mac extends OS { } @Override - public Process exec(String[] command, Map env) throws IOException { - String[] actual_command = command; + public Process exec(List command, Map env) throws IOException { + List actual_command = command; if (this.hasNiceBinary == null) { this.checkNiceAvailability(); } if (this.hasNiceBinary.booleanValue()) { - String[] low = { NICE_BINARY_PATH, "-n", "19" }; // launch the process in lowest priority - actual_command = Utils.concatAll(low, command); + // launch the process in lowest priority + actual_command.add(0, "19"); + actual_command.add(0, "-n"); + actual_command.add(0, NICE_BINARY_PATH); } else { Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority"); diff --git a/src/com/sheepit/client/os/OS.java b/src/com/sheepit/client/os/OS.java index 0c4f9bf..ed12578 100644 --- a/src/com/sheepit/client/os/OS.java +++ b/src/com/sheepit/client/os/OS.java @@ -19,6 +19,7 @@ package com.sheepit.client.os; import java.io.IOException; +import java.util.List; import java.util.Map; import com.sheepit.client.hardware.cpu.CPU; @@ -38,7 +39,7 @@ public abstract class OS { return null; } - public Process exec(String[] command, Map env) throws IOException { + public Process exec(List command, Map env) throws IOException { ProcessBuilder builder = new ProcessBuilder(command); builder.redirectErrorStream(true); if (env != null) { diff --git a/src/com/sheepit/client/os/Windows.java b/src/com/sheepit/client/os/Windows.java index 9e04b0e..cf9f0e9 100644 --- a/src/com/sheepit/client/os/Windows.java +++ b/src/com/sheepit/client/os/Windows.java @@ -19,6 +19,7 @@ package com.sheepit.client.os; import java.io.IOException; +import java.util.List; import java.util.Map; import com.sheepit.client.hardware.cpu.CPU; @@ -107,7 +108,7 @@ public class Windows extends OS { } @Override - public Process exec(String[] command, Map env) throws IOException { + public Process exec(List command, Map env) throws IOException { // disable a popup because the renderer might crash (seg fault) Kernel32Lib kernel32lib = null; try {