Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HelperUtility.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * HelperUtility.java
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. import java.io.File;
  24. import java.io.Closeable;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.InputStreamReader;
  30. import java.io.OutputStream;
  31. import java.io.BufferedReader;
  32. import java.net.URI;
  33. import java.net.URISyntaxException;
  34. import java.net.URL;
  35. import java.security.CodeSource;
  36. import java.security.ProtectionDomain;
  37. import java.util.zip.ZipEntry;
  38. import java.util.zip.ZipException;
  39. import java.util.zip.ZipFile;
  40. import java.lang.Process;
  41. /**
  42. * This helper class extracts the serialHelper from the JAR file, makes it executable and executes it with the given Command line arguments.
  43. * @author Thomas Buck
  44. * @author Max Nuding
  45. * @author Felix Bäder
  46. * @version 1.0
  47. */
  48. public class HelperUtility {
  49. /**
  50. * Run the serialHelper with the given arguments
  51. * @param args Command line arguments for serialHelper
  52. * @return Output of helper
  53. */
  54. public static String runHelper(String[] args) {
  55. String[] helperName = new String[args.length + 1];
  56. boolean windows = false;
  57. if ((System.getProperty("os.name").toLowerCase()).indexOf("win") >= 0) {
  58. helperName[0] = "serialHelper.exe";
  59. windows = true;
  60. } else {
  61. helperName[0] = "serialHelper";
  62. }
  63. for (int i = 0; i < args.length; i++) {
  64. helperName[i + 1] = args[i];
  65. }
  66. String ret = "";
  67. try {
  68. File helper = new File(getFile(getJarURI(), helperName[0]));
  69. helperName[0] = helper.getAbsolutePath();
  70. if (!windows) {
  71. Process execute = Runtime.getRuntime().exec("chmod a+x " + helper.getAbsolutePath());
  72. execute.waitFor();
  73. if (execute.exitValue() != 0) {
  74. System.out.println("Could not set helper as executeable (" + execute.exitValue()+ ")");
  75. return null;
  76. }
  77. }
  78. Process p = Runtime.getRuntime().exec(helperName);
  79. BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
  80. String line;
  81. boolean fin = false;
  82. do { // Wait for process to finish... Doesn't work...?
  83. fin = false;
  84. try {
  85. p.waitFor();
  86. } catch (Exception e) {
  87. fin = true;
  88. }
  89. // Read output in same loop... Should work...!
  90. line = br.readLine();
  91. if (line != null) {
  92. ret = ret + line + "\n";
  93. fin = true;
  94. }
  95. } while (fin);
  96. br.close();
  97. if (ret.length() == 0) {
  98. ret = "g"; // We have added a last \n... We will remove it, so add garbage to be removed...
  99. }
  100. ret = ret.substring(0, ret.length() - 1);
  101. return ret;
  102. } catch(Exception e) {
  103. e.printStackTrace();
  104. }
  105. return null;
  106. }
  107. // From http://stackoverflow.com/questions/600146/run-exe-which-is-packaged-inside-jar
  108. private static URI getJarURI()
  109. throws URISyntaxException
  110. {
  111. final ProtectionDomain domain;
  112. final CodeSource source;
  113. final URL url;
  114. final URI uri;
  115. domain = cubeWorker.class.getProtectionDomain();
  116. source = domain.getCodeSource();
  117. url = source.getLocation();
  118. uri = url.toURI();
  119. return (uri);
  120. }
  121. private static URI getFile(final URI where,
  122. final String fileName)
  123. throws ZipException,
  124. IOException
  125. {
  126. final File location;
  127. final URI fileURI;
  128. location = new File(where);
  129. // not in a JAR, just return the path on disk
  130. if(location.isDirectory())
  131. {
  132. fileURI = URI.create(where.toString() + fileName);
  133. }
  134. else
  135. {
  136. final ZipFile zipFile;
  137. zipFile = new ZipFile(location);
  138. try
  139. {
  140. fileURI = extract(zipFile, fileName);
  141. }
  142. finally
  143. {
  144. zipFile.close();
  145. }
  146. }
  147. return (fileURI);
  148. }
  149. private static URI extract(final ZipFile zipFile,
  150. final String fileName)
  151. throws IOException
  152. {
  153. final File tempFile;
  154. final ZipEntry entry;
  155. final InputStream zipStream;
  156. OutputStream fileStream;
  157. tempFile = File.createTempFile(fileName, Long.toString(System.currentTimeMillis()));
  158. tempFile.deleteOnExit();
  159. entry = zipFile.getEntry(fileName);
  160. if(entry == null)
  161. {
  162. throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
  163. }
  164. zipStream = zipFile.getInputStream(entry);
  165. fileStream = null;
  166. try
  167. {
  168. final byte[] buf;
  169. int i;
  170. fileStream = new FileOutputStream(tempFile);
  171. buf = new byte[1024];
  172. i = 0;
  173. while((i = zipStream.read(buf)) != -1)
  174. {
  175. fileStream.write(buf, 0, i);
  176. }
  177. }
  178. finally
  179. {
  180. close(zipStream);
  181. close(fileStream);
  182. }
  183. return (tempFile.toURI());
  184. }
  185. private static void close(final Closeable stream)
  186. {
  187. if(stream != null)
  188. {
  189. try
  190. {
  191. stream.close();
  192. }
  193. catch(final IOException ex)
  194. {
  195. ex.printStackTrace();
  196. }
  197. }
  198. }
  199. }