fix use of InputStream, whose available() method can't be relied upon.

This commit is contained in:
Eric House 2015-05-15 07:07:39 -07:00
parent 3e077a2dc2
commit fe472e0e9a

View file

@ -27,6 +27,7 @@ import android.content.res.AssetManager;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.text.Html; import android.text.Html;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -200,7 +201,7 @@ public class DictUtils {
fis.close(); fis.close();
loc = DictLoc.INTERNAL; loc = DictLoc.INTERNAL;
} catch ( java.io.FileNotFoundException fnf ) { } catch ( java.io.FileNotFoundException fnf ) {
DbgUtils.loge( fnf ); // DbgUtils.loge( fnf );
} catch ( java.io.IOException ioe ) { } catch ( java.io.IOException ioe ) {
DbgUtils.loge( ioe ); DbgUtils.loge( ioe );
} }
@ -315,21 +316,23 @@ public class DictUtils {
if ( loc == DictLoc.UNKNOWN || loc == DictLoc.BUILT_IN ) { if ( loc == DictLoc.UNKNOWN || loc == DictLoc.BUILT_IN ) {
try { try {
AssetManager am = context.getAssets(); AssetManager am = context.getAssets();
InputStream dict = am.open( name, android.content.res. InputStream dict = am.open( name );
AssetManager.ACCESS_RANDOM );
int len = dict.available(); // this may not be the int len = dict.available(); // this may not be the
// full length! // full length!
bytes = new byte[len]; ByteArrayOutputStream bas = new ByteArrayOutputStream( len );
int nRead = dict.read( bytes, 0, len ); byte[] tmp = new byte[1024*16];
if ( nRead != len ) { for ( ; ; ) {
DbgUtils.logf( "**** warning ****; read only %d of %d bytes.", int nRead = dict.read( tmp, 0, tmp.length );
nRead, len ); 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() ); Assert.assertTrue( -1 == dict.read() );
bytes = bas.toByteArray();
} catch ( java.io.IOException ee ){ } catch ( java.io.IOException ee ){
// DbgUtils.logf( "%s failed to open; likely not built-in", name );
} }
} }