1
/*******************************************************************************
2
* Copyright (c) 2009 Progress Software, Inc.
4
* All rights reserved. This program and the accompanying materials
5
* are made available under the terms of the Eclipse Public License v1.0
6
* which accompanies this distribution, and is available at
7
* http://www.eclipse.org/legal/epl-v10.html
8
*******************************************************************************/
11
import java.util.Arrays;
13
import org.fusesource.hawtjni.runtime.ClassFlag;
14
import org.fusesource.hawtjni.runtime.JniArg;
15
import org.fusesource.hawtjni.runtime.JniClass;
16
import org.fusesource.hawtjni.runtime.JniField;
17
import org.fusesource.hawtjni.runtime.JniMethod;
18
import org.fusesource.hawtjni.runtime.Library;
20
import static org.fusesource.hawtjni.runtime.ArgFlag.*;
21
import static org.fusesource.hawtjni.runtime.FieldFlag.*;
22
import static org.fusesource.hawtjni.runtime.MethodFlag.*;
26
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
29
public class Example {
31
private static final Library LIBRARY = new Library("hawtjni-example", Example.class);
37
public static final void main(String args[]) {
39
System.out.println("Checking Operating System Constants:");
40
System.out.println(" O_RDONLY: "+O_RDONLY);
41
System.out.println(" O_WRONLY: "+O_WRONLY);
42
System.out.println(" O_RDWR: "+O_RDWR);
43
System.out.println("");
45
System.out.println("Allocating c structures on the heap...");
47
// We track memory pointers with longs.
48
long []ptrArray = new long[COUNT];
50
for( int i=0; i < COUNT; i++ ) {
51
// Allocate heap space of the structure..
52
ptrArray[i] = malloc(foo.SIZEOF);
54
// Configure some data for a structure...
59
byte[] src = "hello world".getBytes();
60
System.arraycopy(src, 0, f.c, 0, src.length);
65
// Copy the data values into the allocated space.
66
memmove(ptrArray[i], f, foo.SIZEOF);
71
// Display a couple of structures...
72
System.out.println("Dump of the first 2 structures:");
73
print_foo(ptrArray[0]);
74
print_foo(ptrArray[1]);
76
System.out.println("Passing a pointer array to a c function...");
77
long rc = foowork(ptrArray, COUNT);
78
System.out.println("Function result (expecting 55): "+rc);
80
System.out.println("freein up allocated memory.");
81
for( int i=0; i < COUNT; i++ ) {
86
// Example of how to load constants.
87
@JniMethod(flags={CONSTANT_INITIALIZER})
88
private static final native void init();
90
@JniField(flags={CONSTANT})
91
public static int O_RDONLY;
92
@JniField(flags={CONSTANT})
93
public static int O_WRONLY;
94
@JniField(flags={CONSTANT})
95
public static int O_RDWR;
97
@JniMethod(cast="void *")
98
public static final native long malloc(
99
@JniArg(cast="size_t") long size);
101
public static final native void free(
102
@JniArg(cast="void *") long ptr);
105
public static final native void memmove (
106
@JniArg(cast="void *") long dest,
107
@JniArg(cast="const void *") long src,
108
@JniArg(cast="size_t") long size);
110
public static final native void memmove (
111
@JniArg(cast="void *") long dest,
112
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) byte[] src,
113
@JniArg(cast="size_t") long size);
115
public static final native void memmove (
116
@JniArg(cast="void *") long dest,
117
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) char[] src,
118
@JniArg(cast="size_t") long size);
120
public static final native void memmove (
121
@JniArg(cast="void *") long dest,
122
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) short[] src,
123
@JniArg(cast="size_t") long size);
125
public static final native void memmove (
126
@JniArg(cast="void *") long dest,
127
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) int[] src,
128
@JniArg(cast="size_t") long size);
130
public static final native void memmove (
131
@JniArg(cast="void *") long dest,
132
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) long[] src,
133
@JniArg(cast="size_t") long size);
135
public static final native void memmove (
136
@JniArg(cast="void *") long dest,
137
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) float[] src,
138
@JniArg(cast="size_t") long size);
140
public static final native void memmove (
141
@JniArg(cast="void *") long dest,
142
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) double[] src,
143
@JniArg(cast="size_t") long size);
147
public static final native void memmove (
148
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) byte[] dest,
149
@JniArg(cast="const void *") long src,
150
@JniArg(cast="size_t") long size);
152
public static final native void memmove (
153
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) char[] dest,
154
@JniArg(cast="const void *") long src,
155
@JniArg(cast="size_t") long size);
157
public static final native void memmove (
158
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) short[] dest,
159
@JniArg(cast="const void *") long src,
160
@JniArg(cast="size_t") long size);
162
public static final native void memmove (
163
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) int[] dest,
164
@JniArg(cast="const void *") long src,
165
@JniArg(cast="size_t") long size);
167
public static final native void memmove (
168
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) long[] dest,
169
@JniArg(cast="const void *") long src,
170
@JniArg(cast="size_t") long size);
172
public static final native void memmove (
173
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) float[] dest,
174
@JniArg(cast="const void *") long src,
175
@JniArg(cast="size_t") long size);
177
public static final native void memmove (
178
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) double[] dest,
179
@JniArg(cast="const void *") long src,
180
@JniArg(cast="size_t") long size);
182
public static final native void memmove (
183
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) byte[] dest,
184
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) char[] src,
185
@JniArg(cast="size_t") long size);
187
public static final native void memmove (
188
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) int[] dest,
189
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) byte[] src,
190
@JniArg(cast="size_t") long size);
192
@JniMethod(cast="void *")
193
public static final native long memset (
194
@JniArg(cast="void *") long buffer,
196
@JniArg(cast="size_t") long num);
198
public static final native int strlen(
199
@JniArg(cast="char *")long s);
201
@JniClass(flags={ClassFlag.STRUCT})
202
static public class foo {
209
@JniMethod(flags={CONSTANT_INITIALIZER})
210
private static final native void init();
212
// public static final native int foo_sizeof ();
214
@JniField(flags={CONSTANT}, accessor="sizeof(struct foo)")
215
public static int SIZEOF;
219
@JniField(cast="size_t")
222
public byte c[] = new byte[20];
224
@JniField(accessor="c[5]")
227
@JniField(cast="void *")
231
public int hashCode() {
232
final int prime = 31;
234
result = prime * result + a;
235
result = prime * result + (int) (b ^ (b >>> 32));
236
result = prime * result + Arrays.hashCode(c);
237
result = prime * result + c5;
238
result = prime * result + (int) (prev ^ (prev >>> 32));
243
public boolean equals(Object obj) {
248
if (getClass() != obj.getClass())
250
foo other = (foo) obj;
255
if (!Arrays.equals(c, other.c))
259
if (prev != other.prev)
265
public String toString() {
266
return "foo [a=" + a + ", b=" + b + ", c=" + Arrays.toString(c) + ", c5=" + c5 + ", prev=" + prev + "]";
271
public static final native void memmove (
272
@JniArg(cast="void *") long dest,
273
@JniArg(cast="const void *", flags={NO_OUT, CRITICAL}) foo src,
274
@JniArg(cast="size_t") long size);
276
public static final native void memmove (
277
@JniArg(cast="void *", flags={NO_IN, CRITICAL}) foo dest,
278
@JniArg(cast="const void *") long src,
279
@JniArg(cast="size_t") long size);
281
public static final native void print_foo(@JniArg(cast="struct foo *")long ptr);
282
public static final native long foowork (@JniArg(cast="struct foo **") long[] foos, int count);
284
@JniClass(flags={ClassFlag.STRUCT, ClassFlag.TYPEDEF})
285
static public class point {
291
@JniMethod(flags={CONSTANT_INITIALIZER})
292
private static final native void init();
294
@JniField(flags={CONSTANT}, accessor="sizeof(point)")
295
public static int SIZEOF;
301
public static final native void callmeback(@JniArg(cast="void *")long ptr);