1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 09:49:19 +02:00

InputStream.reset() support

This commit is contained in:
Nikolay Pultsin 2014-03-17 02:07:12 +02:00
parent 8de9981908
commit 184c1f615c
4 changed files with 38 additions and 9 deletions

View file

@ -62,6 +62,9 @@ shared_ptr<StringMethod> AndroidUtil::Method_java_util_Locale_getLanguage;
shared_ptr<VoidMethod> AndroidUtil::Method_java_io_InputStream_close; shared_ptr<VoidMethod> AndroidUtil::Method_java_io_InputStream_close;
shared_ptr<IntMethod> AndroidUtil::Method_java_io_InputStream_read; shared_ptr<IntMethod> AndroidUtil::Method_java_io_InputStream_read;
shared_ptr<LongMethod> AndroidUtil::Method_java_io_InputStream_skip; shared_ptr<LongMethod> AndroidUtil::Method_java_io_InputStream_skip;
shared_ptr<VoidMethod> AndroidUtil::Method_java_io_InputStream_mark;
shared_ptr<BooleanMethod> AndroidUtil::Method_java_io_InputStream_markSupported;
shared_ptr<VoidMethod> AndroidUtil::Method_java_io_InputStream_reset;
shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_ZLibrary_Instance; shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_ZLibrary_Instance;
shared_ptr<StringMethod> AndroidUtil::Method_ZLibrary_getVersionName; shared_ptr<StringMethod> AndroidUtil::Method_ZLibrary_getVersionName;
@ -139,6 +142,9 @@ bool AndroidUtil::init(JavaVM* jvm) {
Method_java_io_InputStream_close = new VoidMethod(Class_java_io_InputStream, "close", "()"); Method_java_io_InputStream_close = new VoidMethod(Class_java_io_InputStream, "close", "()");
Method_java_io_InputStream_read = new IntMethod(Class_java_io_InputStream, "read", "([BII)"); Method_java_io_InputStream_read = new IntMethod(Class_java_io_InputStream, "read", "([BII)");
Method_java_io_InputStream_skip = new LongMethod(Class_java_io_InputStream, "skip", "(J)"); Method_java_io_InputStream_skip = new LongMethod(Class_java_io_InputStream, "skip", "(J)");
Method_java_io_InputStream_mark = new VoidMethod(Class_java_io_InputStream, "mark", "(I)");
Method_java_io_InputStream_markSupported = new BooleanMethod(Class_java_io_InputStream, "markSupported", "()");
Method_java_io_InputStream_reset = new VoidMethod(Class_java_io_InputStream, "reset", "()");
StaticMethod_ZLibrary_Instance = new StaticObjectMethod(Class_ZLibrary, "Instance", Class_ZLibrary, "()"); StaticMethod_ZLibrary_Instance = new StaticObjectMethod(Class_ZLibrary, "Instance", Class_ZLibrary, "()");
Method_ZLibrary_getVersionName = new StringMethod(Class_ZLibrary, "getVersionName", "()"); Method_ZLibrary_getVersionName = new StringMethod(Class_ZLibrary, "getVersionName", "()");

View file

@ -84,6 +84,9 @@ public:
static shared_ptr<VoidMethod> Method_java_io_InputStream_close; static shared_ptr<VoidMethod> Method_java_io_InputStream_close;
static shared_ptr<IntMethod> Method_java_io_InputStream_read; static shared_ptr<IntMethod> Method_java_io_InputStream_read;
static shared_ptr<LongMethod> Method_java_io_InputStream_skip; static shared_ptr<LongMethod> Method_java_io_InputStream_skip;
static shared_ptr<VoidMethod> Method_java_io_InputStream_mark;
static shared_ptr<BooleanMethod> Method_java_io_InputStream_markSupported;
static shared_ptr<VoidMethod> Method_java_io_InputStream_reset;
static shared_ptr<StaticObjectMethod> StaticMethod_ZLibrary_Instance; static shared_ptr<StaticObjectMethod> StaticMethod_ZLibrary_Instance;
static shared_ptr<StringMethod> Method_ZLibrary_getVersionName; static shared_ptr<StringMethod> Method_ZLibrary_getVersionName;

View file

@ -22,7 +22,7 @@
#include <AndroidUtil.h> #include <AndroidUtil.h>
#include <JniEnvelope.h> #include <JniEnvelope.h>
JavaInputStream::JavaInputStream(const std::string &name) : myName(name), myNeedRepositionToStart(false) { JavaInputStream::JavaInputStream(const std::string &name, shared_ptr<FileEncryptionInfo> encryptionInfo) : myName(name), myEncryptionInfo(encryptionInfo), myNeedRepositionToStart(false), myMarkSupported(false) {
myJavaFile = 0; myJavaFile = 0;
myJavaInputStream = 0; myJavaInputStream = 0;
@ -45,14 +45,19 @@ JavaInputStream::~JavaInputStream() {
void JavaInputStream::initStream(JNIEnv *env) { void JavaInputStream::initStream(JNIEnv *env) {
if (myJavaFile == 0) { if (myJavaFile == 0) {
jobject javaFile = AndroidUtil::createJavaFile(env, myName); jobject javaFile = AndroidUtil::createJavaFile(env, myName);
myJavaFile = env->NewGlobalRef(javaFile); if (javaFile == 0) {
env->DeleteLocalRef(javaFile);
if (myJavaFile == 0) {
return; return;
} }
myJavaFile = env->NewGlobalRef(javaFile);
env->DeleteLocalRef(javaFile);
} }
jobject stream = AndroidUtil::Method_ZLFile_getInputStream->call(myJavaFile); jobject stream;
if (myEncryptionInfo.isNull()) {
stream = AndroidUtil::Method_ZLFile_getInputStream->call(myJavaFile);
} else {
stream = 0;
}
if (env->ExceptionCheck()) { if (env->ExceptionCheck()) {
env->ExceptionClear(); env->ExceptionClear();
} else { } else {
@ -60,6 +65,11 @@ void JavaInputStream::initStream(JNIEnv *env) {
myOffset = 0; myOffset = 0;
} }
env->DeleteLocalRef(stream); env->DeleteLocalRef(stream);
myMarkSupported = AndroidUtil::Method_java_io_InputStream_markSupported->call(myJavaInputStream);
if (myMarkSupported) {
AndroidUtil::Method_java_io_InputStream_mark->call(myJavaInputStream, sizeOfOpened());
}
} }
void JavaInputStream::closeStream(JNIEnv *env) { void JavaInputStream::closeStream(JNIEnv *env) {
@ -74,8 +84,14 @@ void JavaInputStream::closeStream(JNIEnv *env) {
void JavaInputStream::rewind(JNIEnv *env) { void JavaInputStream::rewind(JNIEnv *env) {
if (myOffset > 0) { if (myOffset > 0) {
closeStream(env); if (myMarkSupported) {
initStream(env); AndroidUtil::Method_java_io_InputStream_reset->call(myJavaInputStream);
AndroidUtil::Method_java_io_InputStream_mark->call(myJavaInputStream, sizeOfOpened());
myOffset = 0;
} else {
closeStream(env);
initStream(env);
}
} }
} }

View file

@ -22,12 +22,14 @@
#include <jni.h> #include <jni.h>
#include <shared_ptr.h>
#include <ZLInputStream.h> #include <ZLInputStream.h>
#include <FileEncryptionInfo.h>
class JavaInputStream : public ZLInputStream { class JavaInputStream : public ZLInputStream {
public: public:
JavaInputStream(const std::string &name); JavaInputStream(const std::string &name, shared_ptr<FileEncryptionInfo> encryptionInfo = 0);
~JavaInputStream(); ~JavaInputStream();
bool open(); bool open();
std::size_t read(char *buffer, std::size_t maxSize); std::size_t read(char *buffer, std::size_t maxSize);
@ -46,8 +48,10 @@ private:
std::size_t skip(JNIEnv *env, std::size_t offset); std::size_t skip(JNIEnv *env, std::size_t offset);
private: private:
std::string myName; const std::string myName;
const shared_ptr<FileEncryptionInfo> myEncryptionInfo;
bool myNeedRepositionToStart; bool myNeedRepositionToStart;
bool myMarkSupported;
jobject myJavaFile; jobject myJavaFile;