Skip to content

Commit dad8634

Browse files
John EckerdalJohan Redestig
authored andcommitted
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
1 parent d6c1919 commit dad8634

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

core/java/android/webkit/WebView.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,18 +1182,29 @@ public boolean savePicture(Bundle b, File dest) {
11821182
return false;
11831183
}
11841184
final Picture p = capturePicture();
1185+
1186+
FileOutputStream out = null;
1187+
boolean success = false;
11851188
try {
1186-
final FileOutputStream out = new FileOutputStream(dest);
1189+
out = new FileOutputStream(dest);
11871190
p.writeToStream(out);
1188-
out.close();
1191+
success = true;
11891192
} catch (FileNotFoundException e){
11901193
e.printStackTrace();
11911194
} catch (IOException e) {
11921195
e.printStackTrace();
11931196
} catch (RuntimeException e) {
11941197
e.printStackTrace();
1198+
} finally {
1199+
if (out != null) {
1200+
try {
1201+
out.close();
1202+
} catch (Throwable t) {
1203+
}
1204+
}
11951205
}
1196-
if (dest.length() > 0) {
1206+
1207+
if (success && dest.length() > 0) {
11971208
b.putInt("scrollX", mScrollX);
11981209
b.putInt("scrollY", mScrollY);
11991210
b.putFloat("scale", mActualScale);
@@ -1217,16 +1228,23 @@ public boolean restorePicture(Bundle b, File src) {
12171228
}
12181229
if (src.exists()) {
12191230
Picture p = null;
1231+
FileInputStream in = null;
12201232
try {
1221-
final FileInputStream in = new FileInputStream(src);
1233+
in = new FileInputStream(src);
12221234
p = Picture.createFromStream(in);
1223-
in.close();
12241235
} catch (FileNotFoundException e){
12251236
e.printStackTrace();
12261237
} catch (RuntimeException e) {
12271238
e.printStackTrace();
12281239
} catch (IOException e) {
12291240
e.printStackTrace();
1241+
} finally {
1242+
if (in != null) {
1243+
try {
1244+
in.close();
1245+
} catch (Throwable t) {
1246+
}
1247+
}
12301248
}
12311249
if (p != null) {
12321250
int sx = b.getInt("scrollX", 0);

0 commit comments

Comments
 (0)