mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-03 23:04:08 +01:00
factor code into new method copyFileStream
This commit is contained in:
parent
829fd01685
commit
9128c2d60c
2 changed files with 28 additions and 35 deletions
|
@ -30,9 +30,10 @@ import android.os.Environment;
|
|||
import android.text.TextUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
@ -913,6 +914,30 @@ public class DBUtils {
|
|||
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 )
|
||||
{
|
||||
String name = DBHelper.getDBName();
|
||||
|
@ -925,23 +950,10 @@ public class DBUtils {
|
|||
FileInputStream src = new FileInputStream( srcDB );
|
||||
FileOutputStream dest =
|
||||
new FileOutputStream( toSDCard? sdcardDB : gamesDB );
|
||||
byte[] buffer = new byte[1024];
|
||||
for ( ; ; ) {
|
||||
int nRead = src.read(buffer);
|
||||
if ( 0 > nRead ) {
|
||||
break;
|
||||
}
|
||||
dest.write( buffer, 0, nRead );
|
||||
}
|
||||
|
||||
dest.flush();
|
||||
dest.close();
|
||||
src.close();
|
||||
copyFileStream( dest, src );
|
||||
}
|
||||
} catch( java.io.FileNotFoundException fnfe ) {
|
||||
DbgUtils.logf( "in saveDB: %s", fnfe.toString() );
|
||||
} catch( java.io.IOException ioe ) {
|
||||
DbgUtils.logf( "in saveDB: %s", ioe.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,6 @@ import java.io.File;
|
|||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import android.content.res.AssetManager;
|
||||
|
@ -224,9 +223,6 @@ public class DictUtils {
|
|||
Assert.assertFalse( from.equals(to) );
|
||||
boolean success = false;
|
||||
|
||||
FileChannel channelIn = null;
|
||||
FileChannel channelOut = null;
|
||||
|
||||
try {
|
||||
FileInputStream fis = DictLoc.INTERNAL == from
|
||||
? context.openFileInput( name )
|
||||
|
@ -236,24 +232,9 @@ public class DictUtils {
|
|||
? context.openFileOutput( name, Context.MODE_PRIVATE )
|
||||
: new FileOutputStream( getDictFile( context, name, to ) );
|
||||
|
||||
channelIn = fis.getChannel();
|
||||
channelOut = fos.getChannel();
|
||||
channelIn.transferTo( 0, channelIn.size(), channelOut );
|
||||
success = true;
|
||||
|
||||
success = DBUtils.copyFileStream( fos, fis );
|
||||
} catch ( java.io.FileNotFoundException fnfe ) {
|
||||
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;
|
||||
} // copyDict
|
||||
|
|
Loading…
Reference in a new issue