From dad86349bea3aec1d48793adc61b446f82311d15 Mon Sep 17 00:00:00 2001 From: John Eckerdal Date: Wed, 2 Jun 2010 10:59:46 +0200 Subject: [PATCH] Add better error handling for savePicture and restorePicture If an Exception occurs when storing the file treat this as an error and always fail to try to prevent corrupted pictures to be stored to the file system. Close files if they were opened, the caller might want to perform other file operations on the file and if it is still open these may fail. Change-Id: Ic68596b5c745bbe413096c22684c388e853a7643 --- core/java/android/webkit/WebView.java | 28 ++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index c5c14d3ee08ac..de450fcf73540 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -1182,18 +1182,29 @@ public class WebView extends AbsoluteLayout return false; } final Picture p = capturePicture(); + + FileOutputStream out = null; + boolean success = false; try { - final FileOutputStream out = new FileOutputStream(dest); + out = new FileOutputStream(dest); p.writeToStream(out); - out.close(); + success = true; } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); + } finally { + if (out != null) { + try { + out.close(); + } catch (Throwable t) { + } + } } - if (dest.length() > 0) { + + if (success && dest.length() > 0) { b.putInt("scrollX", mScrollX); b.putInt("scrollY", mScrollY); b.putFloat("scale", mActualScale); @@ -1217,16 +1228,23 @@ public class WebView extends AbsoluteLayout } if (src.exists()) { Picture p = null; + FileInputStream in = null; try { - final FileInputStream in = new FileInputStream(src); + in = new FileInputStream(src); p = Picture.createFromStream(in); - in.close(); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); + } finally { + if (in != null) { + try { + in.close(); + } catch (Throwable t) { + } + } } if (p != null) { int sx = b.getInt("scrollX", 0);