mirror of
https://github.com/kiwix/java-libkiwix.git
synced 2025-09-11 08:08:46 -04:00
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:
parent
81993af3c1
commit
915866a5fe
@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
DISPOSE
|
DISPOSE
|
||||||
|
|
||||||
METHOD0(jstring, getData) {
|
METHOD0(jbyteArray, getData) {
|
||||||
return TO_JNI(std::string(*THIS));
|
return cArray2jni(THIS->data(), THIS->size(), env);
|
||||||
}
|
}
|
||||||
GETTER(jlong, size)
|
GETTER(jlong, size)
|
||||||
|
@ -199,6 +199,15 @@ template<> struct JTypeArray<bool>{
|
|||||||
env->SetBooleanArrayRegion(array, 0, length, reinterpret_cast<const jboolean*>(data));
|
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>{
|
template<> struct JTypeArray<int32_t>{
|
||||||
typedef jintArray type_t;
|
typedef jintArray type_t;
|
||||||
static jintArray createArray(JNIEnv* env, size_t length) {
|
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);
|
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)
|
#define TO_JNI(VAL) c2jni(VAL, env)
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ import org.kiwix.libzim.Blob;
|
|||||||
|
|
||||||
public class Blob
|
public class Blob
|
||||||
{
|
{
|
||||||
public native String getData();
|
public native byte[] getData();
|
||||||
public native long size();
|
public native long size();
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,10 +44,8 @@ public class test {
|
|||||||
return new String(getFileContent(path));
|
return new String(getFileContent(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
private void testArchive(Archive archive)
|
||||||
public void testArchive()
|
throws IOException {
|
||||||
throws JNIKiwixException, IOException, ZimFileFormatException {
|
|
||||||
Archive archive = new Archive("small.zim");
|
|
||||||
// test the zim file main page title
|
// test the zim file main page title
|
||||||
assertEquals("Test ZIM file", archive.getMainEntry().getTitle());
|
assertEquals("Test ZIM file", archive.getMainEntry().getTitle());
|
||||||
// test zim file size
|
// test zim file size
|
||||||
@ -55,21 +53,29 @@ public class test {
|
|||||||
// test zim file main url
|
// test zim file main url
|
||||||
assertEquals("A/main.html", archive.getMainEntry().getPath());
|
assertEquals("A/main.html", archive.getMainEntry().getPath());
|
||||||
// test zim file content
|
// test zim file content
|
||||||
String s = getTextFileContent("small_zimfile_data/main.html");
|
byte[] mainData = getFileContent("small_zimfile_data/main.html");
|
||||||
String c = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
|
byte[] inZimMainData = archive.getEntryByPath("A/main.html").getItem(true).getData().getData();
|
||||||
assertEquals(s, c);
|
assert(Arrays.equals(mainData, inZimMainData));
|
||||||
|
|
||||||
// test zim file icon
|
// test zim file icon
|
||||||
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
|
||||||
assertEquals(true, archive.hasIllustration(48));
|
assertEquals(true, archive.hasIllustration(48));
|
||||||
|
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
|
||||||
Item item = archive.getIllustrationItem(48);
|
Item item = archive.getIllustrationItem(48);
|
||||||
assertEquals(faviconData.length, item.getSize());
|
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);
|
assertNotEquals("", dai.filename);
|
||||||
c = new String(getFileContentPartial(dai.filename, (int) dai.offset, faviconData.length));
|
byte[] readData = getFileContentPartial(dai.filename, (int) dai.offset, (int) item.getSize());
|
||||||
assertEquals(new String(faviconData), c);
|
assert(Arrays.equals(faviconData, readData));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArchiveDirect()
|
||||||
|
throws JNIKiwixException, IOException, ZimFileFormatException {
|
||||||
|
Archive archive = new Archive("small.zim");
|
||||||
|
testArchive(archive);
|
||||||
archive.dispose();
|
archive.dispose();
|
||||||
|
|
||||||
// test reader with invalid zim file
|
// test reader with invalid zim file
|
||||||
@ -87,28 +93,7 @@ public class test {
|
|||||||
throws JNIKiwixException, IOException, ZimFileFormatException {
|
throws JNIKiwixException, IOException, ZimFileFormatException {
|
||||||
FileInputStream fis = new FileInputStream("small.zim");
|
FileInputStream fis = new FileInputStream("small.zim");
|
||||||
Archive archive = new Archive(fis.getFD());
|
Archive archive = new Archive(fis.getFD());
|
||||||
// test the zim file main page title
|
testArchive(archive);
|
||||||
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);
|
|
||||||
|
|
||||||
archive.dispose();
|
archive.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,29 +103,7 @@ public class test {
|
|||||||
File plainArchive = new File("small.zim");
|
File plainArchive = new File("small.zim");
|
||||||
FileInputStream fis = new FileInputStream("small.zim.embedded");
|
FileInputStream fis = new FileInputStream("small.zim.embedded");
|
||||||
Archive archive = new Archive(fis.getFD(), 8, plainArchive.length());
|
Archive archive = new Archive(fis.getFD(), 8, plainArchive.length());
|
||||||
// test the zim file main page title
|
testArchive(archive);
|
||||||
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);
|
|
||||||
|
|
||||||
archive.dispose();
|
archive.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user