11#include <jni.h>
22#include <libpostal/libpostal.h>
3+ #include <string.h>
34
45JNIEXPORT void JNICALL Java_com_mapzen_jpostal_AddressParser_setup
56 (JNIEnv * env , jclass cls ) {
@@ -24,9 +25,19 @@ JNIEXPORT void JNICALL Java_com_mapzen_jpostal_AddressParser_setupDataDir
2425}
2526
2627JNIEXPORT jobjectArray JNICALL Java_com_mapzen_jpostal_AddressParser_libpostalParse
27- (JNIEnv * env , jobject thisObj , jstring jAddress , jobject jOptions ) {
28+ (JNIEnv * env , jobject thisObj , jbyteArray jAddress , jobject jOptions ) {
29+
30+ jbyte * addressElements = (* env )-> GetByteArrayElements (env , jAddress , NULL );
31+ jsize size = (* env )-> GetArrayLength (env , jAddress );
32+ char address [size + 1 ];
33+
34+ for (int i = 0 ; i < size ; ++ i ) {
35+ address [i ] = addressElements [i ];
36+ }
37+ (* env ) -> ReleaseByteArrayElements (env , jAddress , addressElements , 0 );
38+
39+ address [size ] = '\0' ;
2840
29- const char * address = (* env )-> GetStringUTFChars (env , jAddress , 0 );
3041
3142 libpostal_address_parser_options_t options = libpostal_get_address_parser_default_options ();
3243
@@ -58,8 +69,6 @@ JNIEXPORT jobjectArray JNICALL Java_com_mapzen_jpostal_AddressParser_libpostalPa
5869
5970 libpostal_address_parser_response_t * response = libpostal_parse_address ((char * )address , options );
6071
61- (* env )-> ReleaseStringUTFChars (env , jAddress , address );
62-
6372 if (jLanguage != NULL ) {
6473 (* env )-> ReleaseStringUTFChars (env , jLanguage , 0 );
6574 }
@@ -71,7 +80,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_mapzen_jpostal_AddressParser_libpostalPa
7180 jmethodID mid ;
7281
7382 jclass parsedComponentClass = (* env )-> FindClass (env , "com/mapzen/jpostal/ParsedComponent" );
74- mid = (* env )-> GetMethodID (env , parsedComponentClass , "<init>" , "(Ljava/lang/String;Ljava /lang/String;)V" );
83+ mid = (* env )-> GetMethodID (env , parsedComponentClass , "<init>" , "([BLjava /lang/String;)V" );
7584
7685 size_t num_components = response != NULL ? response -> num_components : 0 ;
7786
@@ -82,12 +91,16 @@ JNIEXPORT jobjectArray JNICALL Java_com_mapzen_jpostal_AddressParser_libpostalPa
8291
8392 if (num_components > 0 ) {
8493 for (size_t i = 0 ; i < num_components ; i ++ ) {
85- jstring jComponent = (* env )-> NewStringUTF (env , response -> components [i ]);
8694 jstring jLabel = (* env )-> NewStringUTF (env , response -> labels [i ]);
87-
88- jobject jParsedComponent = (* env )-> NewObject (env , parsedComponentClass , mid , jComponent , jLabel );
89-
95+ jbyteArray bytes = ( * env ) -> NewByteArray ( env , strlen ( response -> components [ i ]));
96+ (* env )-> SetByteArrayRegion (env , bytes , 0 , strlen ( response -> components [ i ]), ( jbyte * ) response -> components [ i ] );
97+ jobject jParsedComponent = ( * env ) -> NewObject ( env , parsedComponentClass , mid , bytes , jLabel );
9098 (* env )-> SetObjectArrayElement (env , ret , i , jParsedComponent );
99+
100+ // // These might be necessary to help ensure we're not leaking memory in the cluster.
101+ // (*env)->DeleteLocalRef(env, bytes);
102+ // (*env)->DeleteLocalRef(env, jLabel);
103+ // (*env)->DeleteLocalRef(env, jParsedComponent);
91104 }
92105 }
93106
0 commit comments