From 598a15469343a6f7b490a2cc69b424a1bf30d8f6 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 19 Sep 2012 08:09:27 -0700 Subject: [PATCH] factor common code into function --- xwords4/android/XWords4/jni/andutils.c | 11 +++++++++++ xwords4/android/XWords4/jni/andutils.h | 1 + xwords4/android/XWords4/jni/xwjni.c | 19 ++++--------------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/xwords4/android/XWords4/jni/andutils.c b/xwords4/android/XWords4/jni/andutils.c index 5259a7c55..f0f3283d5 100644 --- a/xwords4/android/XWords4/jni/andutils.c +++ b/xwords4/android/XWords4/jni/andutils.c @@ -238,6 +238,17 @@ makeByteArray( JNIEnv *env, int siz, const jbyte* vals ) return array; } +jbyteArray +streamToBArray( JNIEnv *env, XWStreamCtxt* stream ) +{ + int nBytes = stream_getSize( stream ); + jbyteArray result = (*env)->NewByteArray( env, nBytes ); + jbyte* jelems = (*env)->GetByteArrayElements( env, result, NULL ); + stream_getBytes( stream, jelems, nBytes ); + (*env)->ReleaseByteArrayElements( env, result, jelems, 0 ); + return result; +} + void setBoolArray( JNIEnv* env, jbooleanArray jarr, int count, const jboolean* vals ) diff --git a/xwords4/android/XWords4/jni/andutils.h b/xwords4/android/XWords4/jni/andutils.h index 029f7cf31..bda6aa314 100644 --- a/xwords4/android/XWords4/jni/andutils.h +++ b/xwords4/android/XWords4/jni/andutils.h @@ -57,6 +57,7 @@ void setBoolArray( JNIEnv* env, jbooleanArray jarr, int count, jobjectArray makeStringArray( JNIEnv *env, int size, const XP_UCHAR** vals ); jstring streamToJString( JNIEnv* env, XWStreamCtxt* stream ); +jbyteArray streamToBArray( JNIEnv *env, XWStreamCtxt* stream ); /* Note: jmethodID can be cached. Should not look up more than once. */ jmethodID getMethodID( JNIEnv* env, jobject obj, const char* proc, diff --git a/xwords4/android/XWords4/jni/xwjni.c b/xwords4/android/XWords4/jni/xwjni.c index 2ff200356..8903e8d44 100644 --- a/xwords4/android/XWords4/jni/xwjni.c +++ b/xwords4/android/XWords4/jni/xwjni.c @@ -214,11 +214,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_gi_1to_1stream game_saveToStream( NULL, gi, stream, 0 ); destroyGI( MPPARM(mpool) &gi ); - int nBytes = stream_getSize( stream ); - result = (*env)->NewByteArray( env, nBytes ); - jbyte* jelems = (*env)->GetByteArrayElements( env, result, NULL ); - stream_getBytes( stream, jelems, nBytes ); - (*env)->ReleaseByteArrayElements( env, result, jelems, 0 ); + result = streamToBArray( env, stream ); stream_destroy( stream ); vtmgr_destroy( MPPARM(mpool) vtMgr ); @@ -545,13 +541,9 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1saveToStream destroyGI( MPPARM(mpool) &gi ); } - int nBytes = stream_getSize( stream ); - result = (*env)->NewByteArray( env, nBytes ); - jbyte* jelems = (*env)->GetByteArrayElements( env, result, NULL ); - stream_getBytes( stream, jelems, nBytes ); - (*env)->ReleaseByteArrayElements( env, result, jelems, 0 ); + state->lastSavedSize = stream_getSize( stream ); + result = streamToBArray( env, stream ); stream_destroy( stream ); - state->lastSavedSize = nBytes; XWJNI_END(); return result; @@ -1636,10 +1628,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_base64Decode XP_U8 out[inlen]; XP_U16 outlen = VSIZE(out); if ( smsToBin( out, &outlen, instr, inlen ) ) { - result = (*env)->NewByteArray( env, outlen ); - jbyte* jelems = (*env)->GetByteArrayElements( env, result, NULL ); - XP_MEMCPY( jelems, out, outlen ); - (*env)->ReleaseByteArrayElements( env, result, jelems, 0 ); + result = makeByteArray( env, outlen, (jbyte*)out ); } else { XP_ASSERT(0); }