From 3eb6111be43af64156c43eb55fde8560d5f8e2d7 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Mon, 18 Jul 2016 01:46:47 +0800 Subject: JNI: Implement PDFOBject.write*() methods. --- platform/java/mupdf_native.c | 136 +++++++++++++++++++++ platform/java/mupdf_native.h | 40 ++++++ .../java/src/com/artifex/mupdf/fitz/PDFObject.java | 22 ++++ 3 files changed, 198 insertions(+) (limited to 'platform/java') diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c index eda2a440..03250605 100644 --- a/platform/java/mupdf_native.c +++ b/platform/java/mupdf_native.c @@ -6156,6 +6156,142 @@ FUN(PDFObject_readRawStream)(JNIEnv *env, jobject self) return arr; } +JNIEXPORT void JNICALL +FUN(PDFObject_writeObject)(JNIEnv *env, jobject self, jobject jobj) +{ + fz_context *ctx = get_context(env); + pdf_obj *ref = from_PDFObject(env, self); + pdf_document *pdf = pdf_get_bound_document(ctx, ref); + pdf_obj *obj = from_PDFObject(env, jobj); + + if (ctx == NULL || pdf == NULL || obj == NULL) + return; + + fz_try(ctx) + pdf_update_object(ctx, pdf, pdf_to_num(ctx, ref), obj); + fz_catch(ctx) + jni_rethrow(env, ctx); +} + +JNIEXPORT void JNICALL +FUN(PDFObject_writeStreamBuffer)(JNIEnv *env, jobject self, jobject jbuf) +{ + fz_context *ctx = get_context(env); + pdf_obj *obj = from_PDFObject(env, self); + pdf_document *pdf = pdf_get_bound_document(ctx, obj); + fz_buffer *buf = from_Buffer(env, jbuf); + + if (ctx == NULL || pdf == NULL || buf == NULL) + return; + + fz_try(ctx) + pdf_update_stream(ctx, pdf, obj, buf, 0); + fz_catch(ctx) + jni_rethrow(env, ctx); +} + +JNIEXPORT void JNICALL +FUN(PDFObject_writeStreamString)(JNIEnv *env, jobject self, jstring jstr) +{ + fz_context *ctx = get_context(env); + pdf_obj *obj = from_PDFObject(env, self); + pdf_document *pdf = pdf_get_bound_document(ctx, obj); + const char *str = NULL; + unsigned char *data = NULL; + fz_buffer *buf = NULL; + + if (ctx == NULL || pdf == NULL || jstr == NULL) + return; + + str = (*env)->GetStringUTFChars(env, jstr, NULL); + if (str == NULL) + { + jni_throw(env, FZ_ERROR_GENERIC, "writeStream failed"); + return; + } + + fz_var(data); + fz_var(buf); + + fz_try(ctx) + { + int len = strlen(str); + data = fz_malloc(ctx, len); + memcpy(data, str, len); + buf = fz_new_buffer_from_data(ctx, data, len); + data = NULL; + pdf_update_stream(ctx, pdf, obj, buf, 0); + } + fz_always(ctx) + { + fz_drop_buffer(ctx, buf); + fz_free(ctx, data); + (*env)->ReleaseStringUTFChars(env, jstr, str); + } + fz_catch(ctx) + jni_rethrow(env, ctx); +} + +JNIEXPORT void JNICALL +FUN(PDFObject_writeRawStreamBuffer)(JNIEnv *env, jobject self, jobject jbuf) +{ + fz_context *ctx = get_context(env); + pdf_obj *obj = from_PDFObject(env, self); + pdf_document *pdf = pdf_get_bound_document(ctx, obj); + fz_buffer *buf = from_Buffer(env, jbuf); + + if (ctx == NULL || pdf == NULL || buf == NULL) + return; + + fz_try(ctx) + pdf_update_stream(ctx, pdf, obj, buf, 1); + fz_catch(ctx) + jni_rethrow(env, ctx); +} + +JNIEXPORT void JNICALL +FUN(PDFObject_writeRawStreamString)(JNIEnv *env, jobject self, jstring jstr) +{ + fz_context *ctx = get_context(env); + pdf_obj *obj = from_PDFObject(env, self); + pdf_document *pdf = pdf_get_bound_document(ctx, obj); + const char *str = NULL; + unsigned char *data = NULL; + fz_buffer *buf = NULL; + + if (ctx == NULL || pdf == NULL || jstr == NULL) + return; + + str = (*env)->GetStringUTFChars(env, jstr, NULL); + if (str == NULL) + { + jni_throw(env, FZ_ERROR_GENERIC, "writeStream failed"); + return; + } + + fz_var(data); + fz_var(buf); + + fz_try(ctx) + { + int len = strlen(str); + data = fz_malloc(ctx, len); + memcpy(data, str, len); + buf = fz_new_buffer_from_data(ctx, data, len); + data = NULL; + pdf_update_stream(ctx, pdf, obj, buf, 1); + } + fz_always(ctx) + { + fz_drop_buffer(ctx, buf); + fz_free(ctx, data); + (*env)->ReleaseStringUTFChars(env, jstr, str); + } + fz_catch(ctx) + jni_rethrow(env, ctx); +} + + JNIEXPORT jobject JNICALL FUN(PDFObject_resolve)(JNIEnv *env, jobject self) { diff --git a/platform/java/mupdf_native.h b/platform/java/mupdf_native.h index a876d1b1..7afcccc0 100644 --- a/platform/java/mupdf_native.h +++ b/platform/java/mupdf_native.h @@ -1687,6 +1687,46 @@ JNIEXPORT jbyteArray JNICALL Java_com_artifex_mupdf_fitz_PDFObject_readStream JNIEXPORT jbyteArray JNICALL Java_com_artifex_mupdf_fitz_PDFObject_readRawStream (JNIEnv *, jobject); +/* + * Class: com_artifex_mupdf_fitz_PDFObject + * Method: writeObject + * Signature: (Lcom/artifex/mupdf/fitz/PDFObject;)V + */ +JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_PDFObject_writeObject + (JNIEnv *, jobject, jobject); + +/* + * Class: com_artifex_mupdf_fitz_PDFObject + * Method: writeStreamBuffer + * Signature: (Lcom/artifex/mupdf/fitz/Buffer;)V + */ +JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_PDFObject_writeStreamBuffer + (JNIEnv *, jobject, jobject); + +/* + * Class: com_artifex_mupdf_fitz_PDFObject + * Method: writeStreamString + * Signature: (Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_PDFObject_writeStreamString + (JNIEnv *, jobject, jstring); + +/* + * Class: com_artifex_mupdf_fitz_PDFObject + * Method: writeRawStreamBuffer + * Signature: (Lcom/artifex/mupdf/fitz/Buffer;)V + */ +JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_PDFObject_writeRawStreamBuffer + (JNIEnv *, jobject, jobject); + +/* + * Class: com_artifex_mupdf_fitz_PDFObject + * Method: writeRawStreamString + * Signature: (Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_PDFObject_writeRawStreamString + (JNIEnv *, jobject, jstring); + /* * Class: com_artifex_mupdf_fitz_PDFObject * Method: getArray diff --git a/platform/java/src/com/artifex/mupdf/fitz/PDFObject.java b/platform/java/src/com/artifex/mupdf/fitz/PDFObject.java index 90575582..a099b438 100644 --- a/platform/java/src/com/artifex/mupdf/fitz/PDFObject.java +++ b/platform/java/src/com/artifex/mupdf/fitz/PDFObject.java @@ -47,6 +47,28 @@ public class PDFObject public native byte[] readStream(); public native byte[] readRawStream(); + public native void writeObject(PDFObject obj); + private native void writeStreamBuffer(Buffer buf); + private native void writeStreamString(String str); + private native void writeRawStreamBuffer(Buffer buf); + private native void writeRawStreamString(String str); + + public void writeStream(Buffer buf) { + writeStreamBuffer(buf); + } + + public void writeStream(String str) { + writeStreamString(str); + } + + public void writeRawStream(Buffer buf) { + writeRawStreamBuffer(buf); + } + + public void writeRawStream(String str) { + writeRawStreamString(str); + } + private native PDFObject getArray(int index); private native PDFObject getDictionary(String name); -- cgit v1.2.3