From fe472e0e9ae328741d82bf87fc46280223b60ce6 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 15 May 2015 07:07:39 -0700 Subject: [PATCH] fix use of InputStream, whose available() method can't be relied upon. --- .../org/eehouse/android/xw4/DictUtils.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java index 7bced6666..1929bf214 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java @@ -27,6 +27,7 @@ import android.content.res.AssetManager; import android.net.Uri; import android.os.Environment; import android.text.Html; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -200,7 +201,7 @@ public class DictUtils { fis.close(); loc = DictLoc.INTERNAL; } catch ( java.io.FileNotFoundException fnf ) { - DbgUtils.loge( fnf ); + // DbgUtils.loge( fnf ); } catch ( java.io.IOException ioe ) { DbgUtils.loge( ioe ); } @@ -315,21 +316,23 @@ public class DictUtils { if ( loc == DictLoc.UNKNOWN || loc == DictLoc.BUILT_IN ) { try { AssetManager am = context.getAssets(); - InputStream dict = am.open( name, android.content.res. - AssetManager.ACCESS_RANDOM ); + InputStream dict = am.open( name ); 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 ) { - DbgUtils.logf( "**** warning ****; read only %d of %d bytes.", - nRead, len ); + ByteArrayOutputStream bas = new ByteArrayOutputStream( len ); + byte[] tmp = new byte[1024*16]; + for ( ; ; ) { + int nRead = dict.read( tmp, 0, tmp.length ); + if ( 0 >= nRead ) { + break; + } + bas.write( tmp, 0, nRead ); } - // check that with len bytes we've read the whole file + Assert.assertTrue( -1 == dict.read() ); + bytes = bas.toByteArray(); } catch ( java.io.IOException ee ){ - // DbgUtils.logf( "%s failed to open; likely not built-in", name ); } }