Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HelperUtility.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. while ((line = br.readLine()) != null) {
  83. ret = ret + line + "\n";
  84. }
  85. p.waitFor();
  86. while ((line = br.readLine()) != null) {
  87. ret = ret + line + "\n";
  88. }
  89. br.close();
  90. if (ret.length() > 0) {
  91. ret = ret.substring(0, ret.length() - 1);
  92. }
  93. return ret;
  94. } catch(Exception e) {
  95. e.printStackTrace();
  96. }
  97. return null;
  98. }
  99. // From http://stackoverflow.com/questions/600146/run-exe-which-is-packaged-inside-jar
  100. private static URI getJarURI()
  101. throws URISyntaxException
  102. {
  103. final ProtectionDomain domain;
  104. final CodeSource source;
  105. final URL url;
  106. final URI uri;
  107. domain = cubeWorker.class.getProtectionDomain();
  108. source = domain.getCodeSource();
  109. url = source.getLocation();
  110. uri = url.toURI();
  111. return (uri);
  112. }
  113. private static URI getFile(final URI where,
  114. final String fileName)
  115. throws ZipException,
  116. IOException
  117. {
  118. final File location;
  119. final URI fileURI;
  120. location = new File(where);
  121. // not in a JAR, just return the path on disk
  122. if(location.isDirectory())
  123. {
  124. fileURI = URI.create(where.toString() + fileName);
  125. }
  126. else
  127. {
  128. final ZipFile zipFile;
  129. zipFile = new ZipFile(location);
  130. try
  131. {
  132. fileURI = extract(zipFile, fileName);
  133. }
  134. finally
  135. {
  136. zipFile.close();
  137. }
  138. }
  139. return (fileURI);
  140. }
  141. private static URI extract(final ZipFile zipFile,
  142. final String fileName)
  143. throws IOException
  144. {
  145. final File tempFile;
  146. final ZipEntry entry;
  147. final InputStream zipStream;
  148. OutputStream fileStream;
  149. tempFile = File.createTempFile(fileName, Long.toString(System.currentTimeMillis()));
  150. tempFile.deleteOnExit();
  151. entry = zipFile.getEntry(fileName);
  152. if(entry == null)
  153. {
  154. throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
  155. }
  156. zipStream = zipFile.getInputStream(entry);
  157. fileStream = null;
  158. try
  159. {
  160. final byte[] buf;
  161. int i;
  162. fileStream = new FileOutputStream(tempFile);
  163. buf = new byte[1024];
  164. i = 0;
  165. while((i = zipStream.read(buf)) != -1)
  166. {
  167. fileStream.write(buf, 0, i);
  168. }
  169. }
  170. finally
  171. {
  172. close(zipStream);
  173. close(fileStream);
  174. }
  175. return (tempFile.toURI());
  176. }
  177. private static void close(final Closeable stream)
  178. {
  179. if(stream != null)
  180. {
  181. try
  182. {
  183. stream.close();
  184. }
  185. catch(final IOException ex)
  186. {
  187. ex.printStackTrace();
  188. }
  189. }
  190. }
  191. }