From 4774107cd772da5348073feeb82f3ec69044ab3c Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Thu, 22 Aug 2013 21:24:22 +0200 Subject: refactoring #3159: file copy exception handling * as discussed on github for #3159 --- main/src/cgeo/geocaching/files/LocalStorage.java | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java index e7e8a1c..fc82409 100644 --- a/main/src/cgeo/geocaching/files/LocalStorage.java +++ b/main/src/cgeo/geocaching/files/LocalStorage.java @@ -291,24 +291,25 @@ public final class LocalStorage { InputStream input = null; OutputStream output = null; + boolean copyDone = false; + try { input = new BufferedInputStream(new FileInputStream(source)); output = new BufferedOutputStream(new FileOutputStream(destination)); - } catch (FileNotFoundException e) { - Log.e("LocalStorage.copy: could not open file", e); - IOUtils.closeQuietly(input); - IOUtils.closeQuietly(output); - return false; - } - - boolean copyDone = copy(input, output); - - try { + copyDone = copy(input, output); + // close here already to catch any issue with closing input.close(); output.close(); + } catch (FileNotFoundException e) { + Log.e("LocalStorage.copy: could not copy file", e); + return false; } catch (IOException e) { - Log.e("LocalStorage.copy: could not close file", e); + Log.e("LocalStorage.copy: could not copy file", e); return false; + } finally { + // close here quietly to clean up in all situations + IOUtils.closeQuietly(input); + IOUtils.closeQuietly(output); } return copyDone; -- cgit v1.1