1- /* Links to figure out how to make loading shared libraries work with GraalVM:
2-
3- * https://www.adamh.cz/blog/2012/12/how-to-load-native-jni-library-from-jar/
4- * https://github.com/adamheinrich/native-utils/blob/master/src/main/java/cz/adamh/utils/NativeUtils.java
5-
6- * https://github.com/Willena/sqlite-jdbc-crypt/tree/master?tab=readme-ov-file#graalvm-native-image-support
7- * https://github.com/Willena/sqlite-jdbc-crypt/issues/61
8- * https://github.com/Willena/sqlite-jdbc-crypt/pull/62/files
9- * https://github.com/oracle/graal/issues/4579
10- * https://github.com/Willena/sqlite-jdbc-crypt/blob/master/src/main/java/org/sqlite/SQLiteJDBCLoader.java
11-
12- * https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java
13- * https://github.com/oracle/graal/blob/graal-22.3.3/docs/reference-manual/native-image/Resources.md
14- * https://stackoverflow.com/questions/1983839/determine-which-jar-file-a-class-is-from
15- * https://stackoverflow.com/questions/1429172/how-to-list-the-files-inside-a-jar-file
16- * https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getSystemResources-java.lang.String-
17- */
18-
191package org .rapidyaml ;
202
213import java .net .URL ;
3113import java .nio .ByteOrder ;
3214
3315/**
34- * Interface with the shared librapidyaml library
16+ * Interface with the librapidyaml shared library
3517 */
3618public class Rapidyaml
3719{
38-
39- //------------------------
40- // JNI
41- //------------------------
42-
4320 public static String RAPIDYAML_NAME = "rapidyaml" ;
4421 public static String RAPIDYAML_VERSION = "0.8.0" ;
4522 public static String RAPIDYAML_LIBNAME =
46- String .format ("%s.%s" , RAPIDYAML_NAME , RAPIDYAML_VERSION );
23+ String .format (
24+ "%s.%s" ,
25+ RAPIDYAML_NAME ,
26+ RAPIDYAML_VERSION );
4727
4828 private native long ysparse_init ();
4929 private native void ysparse_destroy (long ysparse );
5030 private native void ysparse_timing_set (boolean yes );
51- private native int ysparse_parse (long ysparse , String filename ,
52- byte [] ys , int ys_length ,
53- int [] evt , int evt_length );
54- private native int ysparse_parse_buf (long ysparse , String filename ,
55- ByteBuffer ys , int ys_length ,
56- IntBuffer evt , int evt_length );
31+ private native int ysparse_parse (
32+ long ysparse , String filename ,
33+ byte [] ys , int ys_length ,
34+ int [] evt , int evt_length );
35+ private native int ysparse_parse_buf (
36+ long ysparse , String filename ,
37+ ByteBuffer ys , int ys_length ,
38+ IntBuffer evt , int evt_length );
5739
5840 private final long ysparse ;
5941
60- public static void main (String [] args ) throws Exception , IOException {
61- (new Rapidyaml ()).timingEnabled (true );
62- System .out .printf ("It works!\n " );
63- }
42+
43+
44+ // XXX this 'main' is for testing
45+ public static void main (
46+ String [] args
47+ ) throws Exception , IOException {
48+ (new Rapidyaml ()).timingEnabled (true );
49+ System .out .printf ("It works!\n " );
50+ }
51+
6452
6553
6654 //------------------------
6755 // CTOR/DTOR
6856 //------------------------
6957
70- public Rapidyaml () throws Exception , IOException
71- {
72- printJarInfo ();
73- System .out .printf ("RAPIDYAML_LIBNAME : >>>%s<<<\n " , RAPIDYAML_LIBNAME );
74-
58+ public Rapidyaml () throws Exception , IOException {
7559 System .loadLibrary (RAPIDYAML_LIBNAME );
76-
77- /*
78- if (System.getenv("YS_RAPIDYAML_MAVEN_TEST") != null) {
79- String library_name = "rapidyaml";
80- System.loadLibrary(library_name);
81- }
82- else {
83- //String library_name = "rapidyaml." + RAPIDYAML_VERSION;
84- //NativeLibLoader.loadLibraryFromJar(library_name);
85-
86- //System.out.printf("LOADING library from jar...\n");
87- //NativeLibLoader.loadLibraryFromJar("/librapidyaml.0.8.0.so");
88-
89- System.loadLibrary(RAPIDYAML_LIBNAME);
90- }
91- */
92-
93-
9460 this .ysparse = this .ysparse_init ();
9561 timingEnabled (false );
9662 }
9763
98- public String printJarInfo () {
99- Class class_ = Rapidyaml .class ;
100-
101- System .out .printf ("class name: >>>%s<<<\n " , class_ .getName ());
102-
103- URL location = class_ .getResource (
104- '/' + class_ .getName ().replace ('.' , '/' ) + ".class" );
105- System .out .printf ("location: >>>%s<<<\n " , location );
106-
107- System .out .printf ("jar: >>>%s<<<\n " ,
108- class_ .getProtectionDomain ().getCodeSource ().getLocation ());
109-
110- return null ;
111- }
112-
11364 // Likely bad idea to implement finalize:
11465 //
11566 // https://stackoverflow.com/questions/158174/why-would-you-ever-implement-finalize
11667 //
117- protected void finalize () throws Throwable
118- {
68+ protected void finalize () throws Throwable {
11969 try {
12070 this .ysparse_destroy (this .ysparse );
12171 }
@@ -129,46 +79,53 @@ protected void finalize() throws Throwable
12979 // EVT
13080 //------------------------
13181
132- public int parseYsToEvt (byte [] src , int [] evts ) throws Exception
133- {
82+ public int parseYsToEvt (
83+ byte [] src , int [] evts
84+ ) throws Exception {
13485 return parseYsToEvt ("yamlscript" , src , evts );
13586 }
13687
137- public int parseYsToEvtBuf (ByteBuffer src , IntBuffer evt ) throws Exception
138- {
88+ public int parseYsToEvtBuf (
89+ ByteBuffer src , IntBuffer evt
90+ ) throws Exception {
13991 return parseYsToEvtBuf ("yamlscript" , src , evt );
14092 }
14193
142- public int parseYsToEvt (String filename , byte [] src , int [] evts ) throws Exception
143- {
94+ public int parseYsToEvt (
95+ String filename , byte [] src , int [] evts
96+ ) throws Exception {
14497 long t = timingStart ("ysparse" );
145- int required_size = ysparse_parse (this .ysparse , filename , src , src .length , evts , evts .length );
98+ int required_size = ysparse_parse (
99+ this .ysparse , filename ,
100+ src , src .length , evts , evts .length );
146101 timingStop ("ysparse" , t , src .length );
147102 return required_size ;
148103 }
149104
150- public int parseYsToEvtBuf (String filename , ByteBuffer src , IntBuffer evt ) throws Exception
151- {
152- if (!src .isDirect ())
105+ public int parseYsToEvtBuf (
106+ String filename , ByteBuffer src , IntBuffer evt
107+ ) throws Exception {
108+ if (! src .isDirect ())
153109 throw new RuntimeException ("src must be direct" );
154- if (! evt .isDirect ())
110+ if (! evt .isDirect ())
155111 throw new RuntimeException ("evt must be direct" );
156112 // the byte order for src does not matter
157113 // but for evt it really does
158- if (evt .order () != ByteOrder .nativeOrder ())
114+ if (evt .order () != ByteOrder .nativeOrder ())
159115 throw new RuntimeException ("evt byte order must be native" );
160116 long t = timingStart ("ysparseBuf" );
161117 evt .position (evt .capacity ());
162- int reqsize = ysparse_parse_buf (this .ysparse , filename , src , src .position (), evt , evt .capacity ());
163- if (reqsize <= evt .capacity ()) {
118+ int reqsize = ysparse_parse_buf (
119+ this .ysparse , filename ,
120+ src , src .position (), evt , evt .capacity ());
121+ if (reqsize <= evt .capacity ()) {
164122 evt .position (reqsize );
165123 }
166124 timingStop ("ysparseBuf" , t , src .position ());
167125 return reqsize ;
168126 }
169127
170- public static IntBuffer mkIntBuffer (int numInts )
171- {
128+ public static IntBuffer mkIntBuffer (int numInts ) {
172129 ByteBuffer bb = ByteBuffer .allocateDirect (/*numBytes*/ 4 * numInts );
173130 // !!! need to explicitly set the byte order to the native order
174131 return bb .order (ByteOrder .nativeOrder ()).asIntBuffer ();
@@ -180,36 +137,35 @@ public static IntBuffer mkIntBuffer(int numInts)
180137
181138 private boolean showTiming = true ;
182139
183- public void timingEnabled (boolean yes )
184- {
140+ public void timingEnabled (boolean yes ) {
185141 showTiming = yes ;
186142 ysparse_timing_set (yes );
187143 }
188144
189- private long timingStart (String name )
190- {
145+ private long timingStart (String name ) {
191146 if (showTiming ) {
192147 System .out .printf (" java:%s...\n " , name );
193148 return System .nanoTime ();
194149 }
195150 return 0 ;
196151 }
197152
198- private void timingStop (String name , long t )
199- {
153+ private void timingStop (String name , long t ) {
200154 if (showTiming ) {
201155 t = System .nanoTime () - t ;
202- System .out .printf (" java:%s: %.6fms\n " , name , (float )t /1.e6f );
156+ System .out .printf (
157+ " java:%s: %.6fms\n " , name , (float ) t /1.e6f );
203158 }
204159 }
205160
206- private void timingStop (String name , long t , int numBytes )
207- {
161+ private void timingStop (String name , long t , int numBytes ) {
208162 if (showTiming ) {
209163 t = System .nanoTime () - t ;
210164 float dt = (float )t ;
211165 float fb = (float )numBytes ;
212- System .out .printf (" java:%s: %.6fms %.3fMB/s %dB\n " , name , dt /1.e6f , 1.e3f *fb /dt , numBytes );
166+ System .out .printf (
167+ " java:%s: %.6fms %.3fMB/s %dB\n " ,
168+ name , dt /1.e6f , 1.e3f *fb /dt , numBytes );
213169 }
214170 }
215171}
0 commit comments