combine two jniutils methods into one

This commit is contained in:
Eric House 2012-09-15 15:21:31 -07:00
parent ad67f31573
commit 937bde6378
5 changed files with 43 additions and 47 deletions

View file

@ -346,9 +346,11 @@ parseDict( AndDictionaryCtxt* ctxt, XP_U8 const* ptr, XP_U32 dictLength,
#endif
) {
JNIEnv* env = ctxt->env;
jstring jsum = and_util_getMD5SumFor( ctxt->jniutil, ctxt->super.name );
jstring jsum = and_util_getMD5SumFor( ctxt->jniutil, ctxt->super.name,
NULL, 0 );
if ( NULL == jsum ) {
jsum = and_util_figureMD5Sum( ctxt->jniutil, ptr, end - ptr );
jsum = and_util_getMD5SumFor( ctxt->jniutil, ctxt->super.name,
ptr, end - ptr );
}
XP_UCHAR* md5Sum = getStringCopy( MPPARM(ctxt->super.mpool) env, jsum );
(*env)->DeleteLocalRef( env, jsum );

View file

@ -93,28 +93,20 @@ and_util_splitFaces( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len,
}
jstring
and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name )
and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name,
const XP_U8* bytes, jsize len )
{
JNIEnv* env = *jniutil->envp;
jmethodID mid = getMethodID( env, jniutil->jjniutil, "getMD5SumFor",
"(Ljava/lang/String;)Ljava/lang/String;" );
"(Ljava/lang/String;[B)Ljava/lang/String;" );
jstring jname = (*env)->NewStringUTF( env, name );
jbyteArray jbytes = NULL == bytes? NULL
: makeByteArray( env, len, (jbyte*)bytes );
jstring result =
(*env)->CallObjectMethod( env, jniutil->jjniutil, mid, jname );
(*env)->CallObjectMethod( env, jniutil->jjniutil, mid, jname, jbytes );
(*env)->DeleteLocalRef( env, jname );
if ( NULL != jbytes ) {
(*env)->DeleteLocalRef( env, jbytes );
}
return result;
}
jstring
and_util_figureMD5Sum( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len )
{
JNIEnv* env = *jniutil->envp;
jmethodID mid = getMethodID( env, jniutil->jjniutil, "figureMD5Sum",
"([B)Ljava/lang/String;" );
jbyteArray jbytes = makeByteArray( env, len, (jbyte*)bytes );
jstring sum =
(*env)->CallObjectMethod( env, jniutil->jjniutil, mid, jbytes );
(*env)->DeleteLocalRef( env, jbytes );
return sum;
}

View file

@ -35,8 +35,7 @@ jobject and_util_makeJBitmap( JNIUtilCtxt* jniu, int nCols, int nRows,
const jboolean* colors );
jobject and_util_splitFaces( JNIUtilCtxt* jniu, const XP_U8* bytes, int len,
XP_Bool isUTF8 );
jstring and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name );
jstring and_util_figureMD5Sum( JNIUtilCtxt* jniutil, const XP_U8* bytes,
jsize len );
jstring and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name,
const XP_U8* bytes, jsize len );
#endif

View file

@ -26,6 +26,5 @@ public interface JNIUtils {
// Stuff I can't do in C....
String[] splitFaces( byte[] chars, boolean isUTF8 );
String getMD5SumFor( String dictName );
String figureMD5Sum( byte[] bytes );
String getMD5SumFor( String dictName, byte[] bytes );
}

View file

@ -91,31 +91,35 @@ public class JNIUtilsImpl implements JNIUtils {
return result;
}
public String getMD5SumFor( String dictName )
public String getMD5SumFor( String dictName, byte[] bytes )
{
return DBUtils.dictsGetMD5Sum( m_context, dictName );
}
public String figureMD5Sum( byte[] bytes )
{
byte[] digest = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buf = new byte[128];
int nLeft = bytes.length;
int offset = 0;
while ( 0 < nLeft ) {
int len = Math.min( buf.length, nLeft );
System.arraycopy( bytes, offset, buf, 0, len );
md.update( buf, 0, len );
nLeft -= len;
offset += len;
DbgUtils.logf( "dictName(%H)", bytes );
String result = null;
if ( null == bytes ) {
result = DBUtils.dictsGetMD5Sum( m_context, dictName );
} else {
byte[] digest = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buf = new byte[128];
int nLeft = bytes.length;
int offset = 0;
while ( 0 < nLeft ) {
int len = Math.min( buf.length, nLeft );
System.arraycopy( bytes, offset, buf, 0, len );
md.update( buf, 0, len );
nLeft -= len;
offset += len;
}
digest = md.digest();
} catch ( java.security.NoSuchAlgorithmException nsae ) {
DbgUtils.loge( nsae );
}
digest = md.digest();
} catch ( java.security.NoSuchAlgorithmException nsae ) {
DbgUtils.loge( nsae );
}
result = Utils.digestToString( digest );
return Utils.digestToString( digest );
// Is this needed? Caller might be doing it anyway.
DBUtils.dictsSetMD5Sum( m_context, dictName, result );
}
return result;
}
}