31
31
#include < zim/item.h>
32
32
33
33
/* Kiwix Reader JNI functions */
34
- JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchive (
35
- JNIEnv* env, jobject obj , jstring filename)
34
+ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchive (
35
+ JNIEnv* env, jobject thisObj , jstring filename)
36
36
{
37
37
std::string cPath = TO_C (filename);
38
38
39
39
LOG (" Attempting to create reader with: %s" , cPath.c_str ());
40
40
Lock l;
41
41
try {
42
- zim::Archive* reader = new zim::Archive (cPath);
43
- return reinterpret_cast <jlong>( new Handle <zim::Archive>(reader) );
42
+ auto archive = std::make_shared< zim::Archive> (cPath);
43
+ SET_PTR (archive );
44
44
} catch (std::exception & e) {
45
45
LOG (" Error opening ZIM file" );
46
46
LOG (" %s" , e.what ());
47
- return 0 ;
48
47
}
49
48
}
50
49
@@ -68,49 +67,45 @@ int jni2fd(const jobject& fdObj, JNIEnv* env)
68
67
69
68
} // unnamed namespace
70
69
71
- JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveByFD (
72
- JNIEnv* env, jobject obj , jobject fdObj)
70
+ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD (
71
+ JNIEnv* env, jobject thisObj , jobject fdObj)
73
72
{
74
73
#ifndef _WIN32
75
74
int fd = jni2fd (fdObj, env);
76
75
77
76
LOG (" Attempting to create reader with fd: %d" , fd);
78
77
Lock l;
79
78
try {
80
- zim::Archive* reader = new zim::Archive (fd);
81
- return reinterpret_cast <jlong>( new Handle <zim::Archive>(reader) );
79
+ auto archive = std::make_shared< zim::Archive> (fd);
80
+ SET_PTR (archive );
82
81
} catch (std::exception & e) {
83
82
LOG (" Error opening ZIM file" );
84
83
LOG (" %s" , e.what ());
85
- return 0 ;
86
84
}
87
85
#else
88
86
jclass exception = env->FindClass (" java/lang/UnsupportedOperationException" );
89
- env->ThrowNew (exception , " org.kiwix.libzim.Archive.getNativeArchiveByFD() is not supported under Windows" );
90
- return 0 ;
87
+ env->ThrowNew (exception , " org.kiwix.libzim.Archive.setNativeArchiveByFD() is not supported under Windows" );
91
88
#endif
92
89
}
93
90
94
- JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveEmbedded (
95
- JNIEnv* env, jobject obj , jobject fdObj, jlong offset, jlong size)
91
+ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded (
92
+ JNIEnv* env, jobject thisObj , jobject fdObj, jlong offset, jlong size)
96
93
{
97
94
#ifndef _WIN32
98
95
int fd = jni2fd (fdObj, env);
99
96
100
97
LOG (" Attempting to create reader with fd: %d" , fd);
101
98
Lock l;
102
99
try {
103
- zim::Archive* reader = new zim::Archive (fd, offset, size);
104
- return reinterpret_cast <jlong>( new Handle <zim::Archive>(reader) );
100
+ auto archive = std::make_shared< zim::Archive> (fd, offset, size);
101
+ SET_PTR (archive );
105
102
} catch (std::exception & e) {
106
103
LOG (" Error opening ZIM file" );
107
104
LOG (" %s" , e.what ());
108
- return 0 ;
109
105
}
110
106
#else
111
107
jclass exception = env->FindClass (" java/lang/UnsupportedOperationException" );
112
- env->ThrowNew (exception , " org.kiwix.libzim.Archive.getNativeArchiveEmbedded() is not supported under Windows" );
113
- return 0 ;
108
+ env->ThrowNew (exception , " org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows" );
114
109
#endif
115
110
}
116
111
@@ -120,7 +115,7 @@ Java_org_kiwix_libzim_Archive_dispose(JNIEnv* env, jobject thisObj)
120
115
dispose<zim::Archive>(env, thisObj);
121
116
}
122
117
123
- #define THIS ( Handle < zim::Archive>::getHandle(env, thisObj) )
118
+ #define THIS GET_PTR ( zim::Archive)
124
119
#define GETTER (retType, name ) JNIEXPORT retType JNICALL \
125
120
Java_org_kiwix_libzim_Archive_##name (JNIEnv* env, jobject thisObj) \
126
121
{ \
@@ -144,16 +139,16 @@ METHOD(jstring, Archive, getMetadata, jstring name) {
144
139
}
145
140
146
141
METHOD (jobject, Archive, getMetadataItem, jstring name) {
147
- auto item = THIS-> getMetadataItem ( TO_C (name) );
148
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Item" , item );
142
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Item " );
143
+ SET_HANDLE (zim:: Item, obj, THIS-> getMetadataItem ( TO_C (name)) );
149
144
return obj;
150
145
}
151
146
152
147
GETTER (jobjectArray, getMetadataKeys)
153
148
154
149
METHOD(jobject, Archive, getIllustrationItem, jint size) {
155
- auto item = THIS-> getIllustrationItem ( TO_C (size) );
156
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Item" , item );
150
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Item " );
151
+ SET_HANDLE (zim:: Item, obj, THIS-> getIllustrationItem ( TO_C (size)) );
157
152
return obj;
158
153
}
159
154
@@ -164,44 +159,44 @@ METHOD(jboolean, Archive, hasIllustration, jint size) {
164
159
GETTER (jlongArray, getIllustrationSizes)
165
160
166
161
METHOD(jobject, Archive, getEntryByPath, jlong index) {
167
- auto entry = THIS-> getEntryByPath ( TO_C ( index ) );
168
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Entry" , entry );
162
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Entry " );
163
+ SET_HANDLE (zim:: Entry, obj, THIS-> getEntryByPath ( TO_C ( index )) );
169
164
return obj;
170
165
}
171
166
172
167
METHOD (jobject, Archive, getEntryByPath, jstring path) {
173
- auto entry = THIS-> getEntryByPath ( TO_C (path) );
174
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Entry" , entry );
168
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Entry " );
169
+ SET_HANDLE (zim:: Entry, obj, THIS-> getEntryByPath ( TO_C (path)) );
175
170
return obj;
176
171
}
177
172
178
173
METHOD (jobject, Archive, getEntryByTitle, jlong index) {
179
- auto entry = THIS-> getEntryByTitle ( TO_C ( index ) );
180
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Entry" , entry );
174
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Entry " );
175
+ SET_HANDLE (zim:: Entry, obj, THIS-> getEntryByTitle ( TO_C ( index )) );
181
176
return obj;
182
177
}
183
178
184
179
METHOD (jobject, Archive, getEntryByTitle, jstring title) {
185
- auto entry = THIS-> getEntryByTitle ( TO_C (title) );
186
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Entry" , entry );
180
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Entry " );
181
+ SET_HANDLE (zim:: Entry, obj, THIS-> getEntryByTitle ( TO_C (title)) );
187
182
return obj;
188
183
}
189
184
190
185
METHOD (jobject, Archive, getEntryByClusterOrder, jlong index) {
191
- auto entry = THIS-> getEntryByClusterOrder ( TO_C ( index ) );
192
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Entry" , entry );
186
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Entry " );
187
+ SET_HANDLE (zim:: Entry, obj, THIS-> getEntryByClusterOrder ( TO_C ( index )) );
193
188
return obj;
194
189
}
195
190
196
191
METHOD0 (jobject, Archive, getMainEntry) {
197
- auto entry = THIS-> getMainEntry ( );
198
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Entry" , entry );
192
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Entry " );
193
+ SET_HANDLE (zim:: Entry, obj, THIS-> getMainEntry () );
199
194
return obj;
200
195
}
201
196
202
197
METHOD0 (jobject, Archive, getRandomEntry) {
203
- auto entry = THIS-> getRandomEntry ( );
204
- auto obj = CREATE_WRAPPER ( " org/kiwix/libzim/ Entry" , entry );
198
+ auto obj = NEW_OBJECT ( " org/kiwix/libzim/Entry " );
199
+ SET_HANDLE (zim:: Entry, obj, THIS-> getRandomEntry () );
205
200
return obj;
206
201
}
207
202
0 commit comments