Simple single-color 8x8x8 LED Cube with AVRs
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

HelperUtility.java 5.9KB

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