finish: using java's base64 en/decoder from java

Added a wrapper function since for compatibility with the jni's encoding
the flags matter and need to be the same everywhere. Or at least there
should be no chance of their getting changed.
This commit is contained in:
Eric House 2017-07-13 07:40:28 -07:00
parent 94dca807ef
commit 64014944f3
8 changed files with 18 additions and 68 deletions

View file

@ -356,7 +356,7 @@ public class ConnStatusHandler {
R.string.key_connstat_data );
if ( null != as64 && 0 < as64.length() ) {
try {
byte[] bytes = XwJNI.base64DecodeJava( as64 );
byte[] bytes = Utils.base64Decode( as64 );
ObjectInputStream ois =
new ObjectInputStream( new ByteArrayInputStream(bytes) );
s_records =
@ -477,8 +477,7 @@ public class ConnStatusHandler {
= new ObjectOutputStream( bas );
out.writeObject( s_records );
out.flush();
String as64 =
XwJNI.base64EncodeJava( bas.toByteArray() );
String as64 = Utils.base64Encode( bas.toByteArray() );
XWPrefs.setPrefsString( context, R.string.key_connstat_data,
as64 );
} catch ( java.io.IOException ioe ) {

View file

@ -2381,7 +2381,7 @@ public class DBUtils {
public static void setBytesFor( Context context, String key, byte[] bytes )
{
// DbgUtils.logf( "setBytesFor: writing %d bytes", bytes.length );
String asStr = XwJNI.base64EncodeJava( bytes );
String asStr = Utils.base64Encode( bytes );
setStringFor( context, key, asStr );
}
@ -2390,7 +2390,7 @@ public class DBUtils {
byte[] bytes = null;
String asStr = getStringFor( context, key, null );
if ( null != asStr ) {
bytes = XwJNI.base64DecodeJava( asStr );
bytes = Utils.base64Decode( asStr );
// DbgUtils.logf( "getBytesFor: loaded %d bytes", bytes.length );
}
return bytes;

View file

@ -1345,7 +1345,7 @@ public class RelayService extends XWService
byte[][][] msgs = new byte[1][count][];
for ( int ii = 0; ii < count; ++ii ) {
msgs[0][ii] = XwJNI.base64DecodeJava( msgs64[ii] );
msgs[0][ii] = Utils.base64Decode( msgs64[ii] );
}
return msgs;
}

View file

@ -43,6 +43,7 @@ import android.support.v4.app.NotificationCompat;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -486,6 +487,16 @@ public class Utils {
return result;
}
public static String base64Encode( byte[] in )
{
return Base64.encodeToString( in, Base64.NO_WRAP );
}
public static byte[] base64Decode( String in )
{
return Base64.decode( in, Base64.NO_WRAP );
}
private static void setFirstBootStatics( Context context )
{
if ( null == s_isFirstBootThisVersion ) {

View file

@ -319,7 +319,7 @@ public class WiDirService extends XWService {
if ( null != wrap ) {
XWPacket packet = new XWPacket( XWPacket.CMD.MSG )
.put( KEY_SRC, getMyMacAddress() )
.put( KEY_DATA, XwJNI.base64EncodeJava( buf ) )
.put( KEY_DATA, Utils.base64Encode( buf ) )
.put( KEY_GAMEID, gameID )
;
if ( forwarding[0] ) {
@ -746,7 +746,7 @@ public class WiDirService extends XWService {
{
Log.d( TAG, "handleGotMessage(%s)", intent.toString() );
int gameID = intent.getIntExtra( KEY_GAMEID, 0 );
byte[] data = XwJNI.base64DecodeJava( intent.getStringExtra( KEY_DATA ) );
byte[] data = Utils.base64Decode( intent.getStringExtra( KEY_DATA ) );
String macAddress = intent.getStringExtra( KEY_RETADDR );
CommsAddrRec addr = new CommsAddrRec( CommsConnType.COMMS_CONN_P2P )

View file

@ -21,7 +21,6 @@
package org.eehouse.android.xw4.jni;
import android.graphics.Rect;
import android.util.Base64;
import java.util.Arrays;
@ -469,26 +468,6 @@ public class XwJNI {
String prefix );
public static native String dict_iter_getDesc( int closure );
// base64 stuff since 2.1 doesn't support it in java
private static native String base64Encode( byte[] in );
private static native byte[] base64Decode( String in );
public static String base64EncodeJava( byte[] in )
{
String str1 = base64Encode( in );
String str2 = Base64.encodeToString( in, Base64.NO_WRAP );
Assert.assertTrue( str2.equals(str1) );
return str1;
}
public static byte[] base64DecodeJava( String in )
{
byte[] b1 = base64Decode( in );
byte[] b2 = Base64.decode( in, Base64.NO_WRAP );
Assert.assertTrue( Arrays.equals( b1, b2 ) );
return b1;
}
// Private methods -- called only here
private static native int initGlobals();
private static native void cleanGlobals( int globals );

View file

@ -37,7 +37,6 @@ LOCAL_DEFINES += \
-DXWFEATURE_BONUSALL \
-DMAX_ROWS=32 \
-DHASH_STREAM \
-DXWFEATURE_BASE64 \
-DXWFEATURE_DEVID \
-DXWFEATURE_CHAT \
-DCOMMON_LAYOUT \

View file

@ -2346,42 +2346,4 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1getDesc
return result;
}
#ifdef XWFEATURE_BASE64
JNIEXPORT jstring JNICALL
Java_org_eehouse_android_xw4_jni_XwJNI_base64Encode
( JNIEnv* env, jclass C, jbyteArray jbytes )
{
int inlen = (*env)->GetArrayLength( env, jbytes );
jbyte* elems = (*env)->GetByteArrayElements( env, jbytes, NULL );
XP_ASSERT( !!elems );
XP_UCHAR out[4+(inlen*4/3)];
XP_U16 outlen = VSIZE( out );
binToSms( out, &outlen, (const XP_U8*)elems, inlen );
(*env)->ReleaseByteArrayElements( env, jbytes, elems, 0 );
jstring result = (*env)->NewStringUTF( env, out );
return result;
}
JNIEXPORT jbyteArray JNICALL
Java_org_eehouse_android_xw4_jni_XwJNI_base64Decode
( JNIEnv* env, jclass C, jstring jstr )
{
jbyteArray result = NULL;
const char* instr = (*env)->GetStringUTFChars( env, jstr, NULL );
XP_U16 inlen = (*env)->GetStringUTFLength( env, jstr );
XP_U8 out[inlen];
XP_U16 outlen = VSIZE(out);
if ( smsToBin( out, &outlen, instr, inlen ) ) {
result = makeByteArray( env, outlen, (jbyte*)out );
} else {
XP_ASSERT(0);
}
(*env)->ReleaseStringUTFChars( env, jstr, instr );
return result;
}
#endif
#endif /* XWFEATURE_BOARDWORDS */