From 49cdb7e6295c15bb1698bb5b8fbb71af2c285dc2 Mon Sep 17 00:00:00 2001 From: Andy2 Date: Thu, 27 Jan 2011 21:03:37 -0800 Subject: [PATCH] use getChannel().size() rather than available() to decide how many bytes to read from downloaded dictionary file -- because available() will not always return the full size. I suspect this is why a German user is crashing when using the very large 2_15 German dict. --- .../src/org/eehouse/android/xw4/GameUtils.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java index 0216bdda0..6723b54ae 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java @@ -30,6 +30,7 @@ import java.io.InputStream; import android.net.Uri; import java.util.ArrayList; import android.content.res.AssetManager; +import junit.framework.Assert; import org.eehouse.android.xw4.jni.*; import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole; @@ -272,22 +273,23 @@ public class GameUtils { public static byte[] openDict( Context context, String name ) { byte[] bytes = null; - InputStream dict = null; name = addDictExtn( name ); - AssetManager am = context.getAssets(); try { - dict = am.open( name, + AssetManager am = context.getAssets(); + InputStream dict = am.open( name, android.content.res.AssetManager.ACCESS_RANDOM ); - int len = dict.available(); + int len = dict.available(); // this may not be the full length! bytes = new byte[len]; int nRead = dict.read( bytes, 0, len ); if ( nRead != len ) { Utils.logf( "**** warning ****; read only " + nRead + " of " + len + " bytes." ); } + // check that with len bytes we've read the whole file + Assert.assertTrue( -1 == dict.read() ); } catch ( java.io.IOException ee ){ Utils.logf( "%s failed to open; likely not built-in", name ); } @@ -296,7 +298,7 @@ public class GameUtils { if ( null == bytes ) { try { FileInputStream fis = context.openFileInput( name ); - int len = fis.available(); + int len = (int)fis.getChannel().size(); bytes = new byte[len]; fis.read( bytes, 0, len ); fis.close();