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.
This commit is contained in:
Matthieu Gautier 2023-05-17 14:29:13 +02:00
parent 81993af3c1
commit 915866a5fe
4 changed files with 39 additions and 59 deletions

View File

@ -35,7 +35,7 @@
DISPOSE
METHOD0(jstring, getData) {
return TO_JNI(std::string(*THIS));
METHOD0(jbyteArray, getData) {
return cArray2jni(THIS->data(), THIS->size(), env);
}
GETTER(jlong, size)

View File

@ -199,6 +199,15 @@ template<> struct JTypeArray<bool>{
env->SetBooleanArrayRegion(array, 0, length, reinterpret_cast<const jboolean*>(data));
}
};
template<> struct JTypeArray<char>{
typedef jbyteArray type_t;
static jbyteArray createArray(JNIEnv* env, size_t length) {
return env->NewByteArray(length);
}
static void setArray(JNIEnv* env, jbyteArray array, const char* data, size_t length) {
env->SetByteArrayRegion(array, 0, length, reinterpret_cast<const signed char*>(data));
}
};
template<> struct JTypeArray<int32_t>{
typedef jintArray type_t;
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)
return c2jni(temp, env);
}
template<typename U>
inline typename JTypeArray<U>::type_t cArray2jni(const U* data, size_t length, JNIEnv* env)
{
auto array = JTypeArray<U>::createArray(env, length);
JTypeArray<U>::setArray(env, array, data, length);
return array;
}
#define TO_JNI(VAL) c2jni(VAL, env)

View File

@ -23,7 +23,7 @@ import org.kiwix.libzim.Blob;
public class Blob
{
public native String getData();
public native byte[] getData();
public native long size();

View File

@ -44,10 +44,8 @@ public class test {
return new String(getFileContent(path));
}
@Test
public void testArchive()
throws JNIKiwixException, IOException, ZimFileFormatException {
Archive archive = new Archive("small.zim");
private void testArchive(Archive archive)
throws IOException {
// test the zim file main page title
assertEquals("Test ZIM file", archive.getMainEntry().getTitle());
// test zim file size
@ -55,21 +53,29 @@ public class test {
// test zim file main url
assertEquals("A/main.html", archive.getMainEntry().getPath());
// test zim file content
String s = getTextFileContent("small_zimfile_data/main.html");
String c = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
assertEquals(s, c);
byte[] mainData = getFileContent("small_zimfile_data/main.html");
byte[] inZimMainData = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
assert(Arrays.equals(mainData, inZimMainData));
// test zim file icon
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
assertEquals(true, archive.hasIllustration(48));
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
Item item = archive.getIllustrationItem(48);
assertEquals(faviconData.length, item.getSize());
assert(Arrays.equals(faviconData, item.getData().getData()));
DirectAccessInfo dai = archive.getEntryByPath("I/favicon.png").getItem(true).getDirectAccessInformation();
// Checking direct access information
DirectAccessInfo dai = item.getDirectAccessInformation();
assertNotEquals("", dai.filename);
c = new String(getFileContentPartial(dai.filename, (int) dai.offset, faviconData.length));
assertEquals(new String(faviconData), c);
byte[] readData = getFileContentPartial(dai.filename, (int) dai.offset, (int) item.getSize());
assert(Arrays.equals(faviconData, readData));
}
@Test
public void testArchiveDirect()
throws JNIKiwixException, IOException, ZimFileFormatException {
Archive archive = new Archive("small.zim");
testArchive(archive);
archive.dispose();
// test reader with invalid zim file
@ -87,28 +93,7 @@ public class test {
throws JNIKiwixException, IOException, ZimFileFormatException {
FileInputStream fis = new FileInputStream("small.zim");
Archive archive = new Archive(fis.getFD());
// test the zim file main page title
assertEquals("Test ZIM file", archive.getMainEntry().getTitle());
// test zim file size
assertEquals(4070, archive.getFilesize()); // The file size is in KiB
// test zim file main url
assertEquals("A/main.html", archive.getMainEntry().getPath());
// test zim file content
String s = getTextFileContent("small_zimfile_data/main.html");
String c = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
assertEquals(s, c);
// test zim file icon
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
assertEquals(true, archive.hasIllustration(48));
Item item = archive.getIllustrationItem(48);
assertEquals(faviconData.length, item.getSize());
DirectAccessInfo dai = archive.getEntryByPath("I/favicon.png").getItem(true).getDirectAccessInformation();
assertNotEquals("", dai.filename);
c = new String(getFileContentPartial(dai.filename, (int) dai.offset, faviconData.length));
assertEquals(new String(faviconData), c);
testArchive(archive);
archive.dispose();
}
@ -118,29 +103,7 @@ public class test {
File plainArchive = new File("small.zim");
FileInputStream fis = new FileInputStream("small.zim.embedded");
Archive archive = new Archive(fis.getFD(), 8, plainArchive.length());
// test the zim file main page title
assertEquals("Test ZIM file", archive.getMainEntry().getTitle());
// test zim file size
assertEquals(4070, archive.getFilesize()); // The file size is in KiB
// test zim file main url
assertEquals("A/main.html", archive.getMainEntry().getPath());
// test zim file content
String s = getTextFileContent("small_zimfile_data/main.html");
String c = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
assertEquals(s, c);
// test zim file icon
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
assertEquals(true, archive.hasIllustration(48));
Item item = archive.getIllustrationItem(48);
assertEquals(faviconData.length, item.getSize());
assertEquals(new String(faviconData), item.getData().getData());
DirectAccessInfo dai = archive.getEntryByPath("I/favicon.png").getItem(true).getDirectAccessInformation();
assertNotEquals("", dai.filename);
c = new String(getFileContentPartial(dai.filename, (int) dai.offset, faviconData.length));
assertEquals(new String(faviconData), c);
testArchive(archive);
archive.dispose();
}