This repository was archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.java
More file actions
109 lines (99 loc) · 3.03 KB
/
Copy pathCommon.java
File metadata and controls
109 lines (99 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) Steven P. Goldsmith. All rights reserved.
*/
package com.codeferm.periphery;
import static org.fusesource.hawtjni.runtime.ArgFlag.CRITICAL;
import static org.fusesource.hawtjni.runtime.ArgFlag.NO_IN;
import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT;
import org.fusesource.hawtjni.runtime.JniArg;
import org.fusesource.hawtjni.runtime.JniClass;
import org.fusesource.hawtjni.runtime.JniMethod;
import org.fusesource.hawtjni.runtime.Library;
/**
* Java Periphery common code.
*
* @author Steven P. Goldsmith
* @version 1.0.0
* @since 1.0.0
*/
@JniClass
public class Common {
/**
* Used for C reference character array arguments.
*/
static final int MAX_CHAR_ARRAY_LEN = 256;
/**
* java-periphery library.
*/
private static final Library LIBRARY = new Library("java-periphery", Common.class);
/**
* Load library.
*/
static {
LIBRARY.load();
}
/**
* Allocate native memory.
*
* @param size Amount of memory to allocate.
* @return Pointer to memory.
*/
@JniMethod(cast = "void *")
public static final native long malloc(@JniArg(cast = "size_t") long size);
/**
* Free native memory.
*
* @param ptr Pointer to memory.
*/
public static final native void free(@JniArg(cast = "void *") long ptr);
/**
* Move C memory to byte array.
*
* @param dest Java byte array.
* @param src Pointer to C memory.
* @param size Amount of bytes to move.
*/
@JniMethod(accessor = "memmove")
public static final native void memMove(@JniArg(cast = "void *", flags = {NO_IN, CRITICAL}) byte[] dest, @JniArg(cast
= "const void *") long src, @JniArg(cast = "size_t") long size);
/**
* Move byte array to C memory.
*
* @param dest Pointer to C memory.
* @param src Java byte array.
* @param size Amount of bytes to move.
*/
@JniMethod(accessor = "memmove")
public static final native void memMove(
@JniArg(cast = "void *") long dest, @JniArg(cast = "const void *", flags = {NO_OUT, CRITICAL}) byte[] src, @JniArg(cast
= "size_t") long size);
/**
* Convert C style string to Java String.
*
* @param str 0 terminated character array.
* @return Java String.
*/
public static String jString(final byte[] str) {
var i = 0;
// Find 0 terminator
while (i < str.length && str[i] != 0) {
i++;
}
return new String(str, 0, i);
}
/**
* Convert Java String to C style string. Remember to free native memory with free.
*
* @param str Java String.
* @return Pointer to C style string.
*/
public static long cString(final String str) {
// Add null terminator
final var cStr = str + "\0";
// Allocate native memory
final var strPtr = malloc(cStr.length());
// Copy Java String to native memory
memMove(strPtr, cStr.getBytes(), cStr.length());
return strPtr;
}
}