|
@@ -22,17 +22,81 @@
|
22
|
22
|
*/
|
23
|
23
|
|
24
|
24
|
/**
|
25
|
|
- * Helper class which runs our native library.
|
|
25
|
+ * Helper class which runs our native library, which is loaded from inside the Jar.
|
26
|
26
|
* @author Thomas Buck
|
27
|
27
|
* @author Max Nuding
|
28
|
28
|
* @author Felix Bäder
|
29
|
29
|
* @version 1.0
|
30
|
30
|
*/
|
31
|
31
|
|
|
32
|
+import java.io.*;
|
|
33
|
+import java.nio.channels.*;
|
|
34
|
+import java.nio.*;
|
|
35
|
+
|
32
|
36
|
public class HelperUtility {
|
33
|
37
|
|
|
38
|
+ // Load libraries, copy from Jar if needed
|
|
39
|
+ // Mostly from:
|
|
40
|
+ // http://stackoverflow.com/questions/1611357/how-to-make-a-jar-file-that-include-dll-files
|
34
|
41
|
static {
|
35
|
|
- System.loadLibrary("Serial");
|
|
42
|
+ // System.out.println("Loading Serial Library...");
|
|
43
|
+ loadFromJar();
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ /**
|
|
47
|
+ * When packaged into JAR extracts DLLs, places these into
|
|
48
|
+ */
|
|
49
|
+ private static void loadFromJar() {
|
|
50
|
+ // we need to put DLL to temp dir
|
|
51
|
+ String os = System.getProperty("os.name").toLowerCase();
|
|
52
|
+ String path = "CC_";
|
|
53
|
+ if (os.indexOf("windows") > -1) {
|
|
54
|
+ loadLib(path, "Serial.dll");
|
|
55
|
+ } else if (os.indexOf("mac") > -1) {
|
|
56
|
+ loadLib(path, "libSerial.jnilib");
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ /**
|
|
61
|
+ * Puts library to temp dir and loads to memory
|
|
62
|
+ */
|
|
63
|
+ private static void loadLib(String path, String name) {
|
|
64
|
+ try {
|
|
65
|
+ // have to use a stream
|
|
66
|
+ InputStream in = HelperUtility.class.getResourceAsStream(name);
|
|
67
|
+ // System.out.println("Input Stream: " + in);
|
|
68
|
+ File fileOut = new File(System.getProperty("java.io.tmpdir") + "/" + path + name);
|
|
69
|
+ OutputStream out = new FileOutputStream(fileOut);
|
|
70
|
+ ReadableByteChannel inChannel = Channels.newChannel(in);
|
|
71
|
+ WritableByteChannel outChannel = Channels.newChannel(out);
|
|
72
|
+ fastChannelCopy(inChannel, outChannel);
|
|
73
|
+ inChannel.close();
|
|
74
|
+ outChannel.close();
|
|
75
|
+ System.load(fileOut.toString());
|
|
76
|
+ } catch (Exception e) {
|
|
77
|
+ System.out.println("Failed to load Serial Library:");
|
|
78
|
+ e.printStackTrace();
|
|
79
|
+ }
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ // http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
|
|
83
|
+ public static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
|
|
84
|
+ ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
|
|
85
|
+ while (src.read(buffer) != -1) {
|
|
86
|
+ // prepare the buffer to be drained
|
|
87
|
+ buffer.flip();
|
|
88
|
+ // write to the channel, may block
|
|
89
|
+ dest.write(buffer);
|
|
90
|
+ // If partial transfer, shift remainder down
|
|
91
|
+ // If buffer is empty, same as doing clear()
|
|
92
|
+ buffer.compact();
|
|
93
|
+ }
|
|
94
|
+ // EOF will leave buffer in fill state
|
|
95
|
+ buffer.flip();
|
|
96
|
+ // make sure the buffer is fully drained.
|
|
97
|
+ while (buffer.hasRemaining()) {
|
|
98
|
+ dest.write(buffer);
|
|
99
|
+ }
|
36
|
100
|
}
|
37
|
101
|
|
38
|
102
|
/**
|