factor code into new method copyFileStream

This commit is contained in:
Eric House 2012-08-13 07:45:14 -07:00
parent 829fd01685
commit 9128c2d60c
2 changed files with 28 additions and 35 deletions

View file

@ -30,9 +30,10 @@ import android.os.Environment;
import android.text.TextUtils; import android.text.TextUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -913,6 +914,30 @@ public class DBUtils {
copyGameDB( context, true ); copyGameDB( context, true );
} }
public static boolean copyFileStream( FileOutputStream fos,
FileInputStream fis )
{
boolean success = false;
FileChannel channelSrc = null;
FileChannel channelDest = null;
try {
channelSrc = fis.getChannel();
channelDest = fos.getChannel();
channelSrc.transferTo( 0, channelSrc.size(), channelDest );
success = true;
} catch( java.io.IOException ioe ) {
DbgUtils.logf( "in saveDB: %s", ioe.toString() );
} finally {
try {
channelSrc.close();
channelDest.close();
} catch( java.io.IOException ioe ) {
DbgUtils.logf( "in saveDB: %s", ioe.toString() );
}
}
return success;
}
private static void copyGameDB( Context context, boolean toSDCard ) private static void copyGameDB( Context context, boolean toSDCard )
{ {
String name = DBHelper.getDBName(); String name = DBHelper.getDBName();
@ -925,23 +950,10 @@ public class DBUtils {
FileInputStream src = new FileInputStream( srcDB ); FileInputStream src = new FileInputStream( srcDB );
FileOutputStream dest = FileOutputStream dest =
new FileOutputStream( toSDCard? sdcardDB : gamesDB ); new FileOutputStream( toSDCard? sdcardDB : gamesDB );
byte[] buffer = new byte[1024]; copyFileStream( dest, src );
for ( ; ; ) {
int nRead = src.read(buffer);
if ( 0 > nRead ) {
break;
}
dest.write( buffer, 0, nRead );
}
dest.flush();
dest.close();
src.close();
} }
} catch( java.io.FileNotFoundException fnfe ) { } catch( java.io.FileNotFoundException fnfe ) {
DbgUtils.logf( "in saveDB: %s", fnfe.toString() ); DbgUtils.logf( "in saveDB: %s", fnfe.toString() );
} catch( java.io.IOException ioe ) {
DbgUtils.logf( "in saveDB: %s", ioe.toString() );
} }
} }

View file

@ -29,7 +29,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import android.content.res.AssetManager; import android.content.res.AssetManager;
@ -224,9 +223,6 @@ public class DictUtils {
Assert.assertFalse( from.equals(to) ); Assert.assertFalse( from.equals(to) );
boolean success = false; boolean success = false;
FileChannel channelIn = null;
FileChannel channelOut = null;
try { try {
FileInputStream fis = DictLoc.INTERNAL == from FileInputStream fis = DictLoc.INTERNAL == from
? context.openFileInput( name ) ? context.openFileInput( name )
@ -236,24 +232,9 @@ public class DictUtils {
? context.openFileOutput( name, Context.MODE_PRIVATE ) ? context.openFileOutput( name, Context.MODE_PRIVATE )
: new FileOutputStream( getDictFile( context, name, to ) ); : new FileOutputStream( getDictFile( context, name, to ) );
channelIn = fis.getChannel(); success = DBUtils.copyFileStream( fos, fis );
channelOut = fos.getChannel();
channelIn.transferTo( 0, channelIn.size(), channelOut );
success = true;
} catch ( java.io.FileNotFoundException fnfe ) { } catch ( java.io.FileNotFoundException fnfe ) {
DbgUtils.logf( "%s", fnfe.toString() ); DbgUtils.logf( "%s", fnfe.toString() );
} catch ( java.io.IOException ioe ) {
DbgUtils.logf( "%s", ioe.toString() );
} finally {
try {
// Order should match assignment order to above in
// case one or both null
channelIn.close();
channelOut.close();
} catch ( Exception e ) {
DbgUtils.logf( "%s", e.toString() );
}
} }
return success; return success;
} // copyDict } // copyDict