factor common code into function

This commit is contained in:
Eric House 2012-09-19 08:09:27 -07:00
parent 81d3f7d15f
commit 598a154693
3 changed files with 16 additions and 15 deletions

View file

@ -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 )

View file

@ -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,

View file

@ -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);
}