Skip to content

Commit e13889d

Browse files
committed
Make Blob.getData return a byte[] instead of a String.
Blob IS a `char[]`. C++ allow a char[] to be stored in a string but in java, a String is associated to a encoding. Content in a Blob may have no encoding so we cannot convert to a string. Fix the test part.
1 parent da4a0bf commit e13889d

4 files changed

Lines changed: 39 additions & 59 deletions

File tree

lib/src/main/cpp/libzim/blob.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
DISPOSE
3737

38-
METHOD0(jstring, getData) {
39-
return TO_JNI(std::string(*THIS));
38+
METHOD0(jbyteArray, getData) {
39+
return cArray2jni(THIS->data(), THIS->size(), env);
4040
}
4141
GETTER(jlong, size)

lib/src/main/cpp/utils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ template<> struct JTypeArray<bool>{
199199
env->SetBooleanArrayRegion(array, 0, length, reinterpret_cast<const jboolean*>(data));
200200
}
201201
};
202+
template<> struct JTypeArray<char>{
203+
typedef jbyteArray type_t;
204+
static jbyteArray createArray(JNIEnv* env, size_t length) {
205+
return env->NewByteArray(length);
206+
}
207+
static void setArray(JNIEnv* env, jbyteArray array, const char* data, size_t length) {
208+
env->SetByteArrayRegion(array, 0, length, reinterpret_cast<const signed char*>(data));
209+
}
210+
};
202211
template<> struct JTypeArray<int32_t>{
203212
typedef jintArray type_t;
204213
static jintArray createArray(JNIEnv* env, size_t length) {
@@ -278,6 +287,14 @@ inline typename JTypeArray<U>::type_t c2jni(const std::set<U>& val, JNIEnv* env)
278287
return c2jni(temp, env);
279288
}
280289

290+
template<typename U>
291+
inline typename JTypeArray<U>::type_t cArray2jni(const U* data, size_t length, JNIEnv* env)
292+
{
293+
auto array = JTypeArray<U>::createArray(env, length);
294+
JTypeArray<U>::setArray(env, array, data, length);
295+
return array;
296+
}
297+
281298
#define TO_JNI(VAL) c2jni(VAL, env)
282299

283300

lib/src/main/java/org/kiwix/libzim/Blob.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class Blob
2525
{
26-
public native String getData();
26+
public native byte[] getData();
2727
public native long size();
2828

2929

lib/src/test/test.java

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,38 @@ private static String getTextFileContent(String path)
4444
return new String(getFileContent(path));
4545
}
4646

47-
@Test
48-
public void testArchive()
49-
throws JNIKiwixException, IOException, ZimFileFormatException {
50-
Archive archive = new Archive("small.zim");
47+
private void testArchive(Archive archive)
48+
throws IOException {
5149
// test the zim file main page title
5250
assertEquals("Test ZIM file", archive.getMainEntry().getTitle());
5351
// test zim file size
5452
assertEquals(4070, archive.getFilesize()); // The file size is in KiB
5553
// test zim file main url
5654
assertEquals("A/main.html", archive.getMainEntry().getPath());
5755
// test zim file content
58-
String s = getTextFileContent("small_zimfile_data/main.html");
59-
String c = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
60-
assertEquals(s, c);
56+
byte[] mainData = getFileContent("small_zimfile_data/main.html");
57+
byte[] inZimMainData = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
58+
assert(Arrays.equals(mainData, inZimMainData));
6159

6260
// test zim file icon
63-
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
6461
assertEquals(true, archive.hasIllustration(48));
62+
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
6563
Item item = archive.getIllustrationItem(48);
6664
assertEquals(faviconData.length, item.getSize());
65+
assert(Arrays.equals(faviconData, item.getData().getData()));
6766

68-
DirectAccessInfo dai = archive.getEntryByPath("I/favicon.png").getItem(true).getDirectAccessInformation();
67+
// Checking direct access information
68+
DirectAccessInfo dai = item.getDirectAccessInformation();
6969
assertNotEquals("", dai.filename);
70-
c = new String(getFileContentPartial(dai.filename, (int) dai.offset, faviconData.length));
71-
assertEquals(new String(faviconData), c);
70+
byte[] readData = getFileContentPartial(dai.filename, (int) dai.offset, (int) item.getSize());
71+
assert(Arrays.equals(faviconData, readData));
72+
}
7273

74+
@Test
75+
public void testArchiveDirect()
76+
throws JNIKiwixException, IOException, ZimFileFormatException {
77+
Archive archive = new Archive("small.zim");
78+
testArchive(archive);
7379
archive.dispose();
7480

7581
// test reader with invalid zim file
@@ -87,28 +93,7 @@ public void testArchiveByFd()
8793
throws JNIKiwixException, IOException, ZimFileFormatException {
8894
FileInputStream fis = new FileInputStream("small.zim");
8995
Archive archive = new Archive(fis.getFD());
90-
// test the zim file main page title
91-
assertEquals("Test ZIM file", archive.getMainEntry().getTitle());
92-
// test zim file size
93-
assertEquals(4070, archive.getFilesize()); // The file size is in KiB
94-
// test zim file main url
95-
assertEquals("A/main.html", archive.getMainEntry().getPath());
96-
// test zim file content
97-
String s = getTextFileContent("small_zimfile_data/main.html");
98-
String c = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
99-
assertEquals(s, c);
100-
101-
// test zim file icon
102-
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
103-
assertEquals(true, archive.hasIllustration(48));
104-
Item item = archive.getIllustrationItem(48);
105-
assertEquals(faviconData.length, item.getSize());
106-
107-
DirectAccessInfo dai = archive.getEntryByPath("I/favicon.png").getItem(true).getDirectAccessInformation();
108-
assertNotEquals("", dai.filename);
109-
c = new String(getFileContentPartial(dai.filename, (int) dai.offset, faviconData.length));
110-
assertEquals(new String(faviconData), c);
111-
96+
testArchive(archive);
11297
archive.dispose();
11398
}
11499

@@ -118,29 +103,7 @@ public void testArchiveWithAnEmbeddedArchive()
118103
File plainArchive = new File("small.zim");
119104
FileInputStream fis = new FileInputStream("small.zim.embedded");
120105
Archive archive = new Archive(fis.getFD(), 8, plainArchive.length());
121-
// test the zim file main page title
122-
assertEquals("Test ZIM file", archive.getMainEntry().getTitle());
123-
// test zim file size
124-
assertEquals(4070, archive.getFilesize()); // The file size is in KiB
125-
// test zim file main url
126-
assertEquals("A/main.html", archive.getMainEntry().getPath());
127-
// test zim file content
128-
String s = getTextFileContent("small_zimfile_data/main.html");
129-
String c = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
130-
assertEquals(s, c);
131-
132-
// test zim file icon
133-
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
134-
assertEquals(true, archive.hasIllustration(48));
135-
Item item = archive.getIllustrationItem(48);
136-
assertEquals(faviconData.length, item.getSize());
137-
assertEquals(new String(faviconData), item.getData().getData());
138-
139-
DirectAccessInfo dai = archive.getEntryByPath("I/favicon.png").getItem(true).getDirectAccessInformation();
140-
assertNotEquals("", dai.filename);
141-
c = new String(getFileContentPartial(dai.filename, (int) dai.offset, faviconData.length));
142-
assertEquals(new String(faviconData), c);
143-
106+
testArchive(archive);
144107
archive.dispose();
145108
}
146109

0 commit comments

Comments
 (0)