From 9128c2d60cc4d4418910f868293fe033bb7ffca6 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 13 Aug 2012 07:45:14 -0700 Subject: [PATCH] factor code into new method copyFileStream --- .../src/org/eehouse/android/xw4/DBUtils.java | 42 ++++++++++++------- .../org/eehouse/android/xw4/DictUtils.java | 21 +--------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index a6a17eb46..c96a0283c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -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() ); } } 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 ee56d11f7..7b63e10cb 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java @@ -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