From daeedc2222668827487a2c92470bccc57f7edf83 Mon Sep 17 00:00:00 2001 From: edpeur Date: Sat, 31 Dec 2011 17:38:41 +0000 Subject: [PATCH] Close resources properly --- src/com/fsck/k9/helper/Utility.java | 20 ++++++++++------ .../k9/mail/internet/BinaryTempFileBody.java | 7 ++++-- .../fsck/k9/mail/internet/MimeUtility.java | 7 ++++-- src/com/fsck/k9/mail/store/LocalStore.java | 23 +++++++++++++------ .../fsck/k9/provider/AttachmentProvider.java | 9 +++++--- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/com/fsck/k9/helper/Utility.java b/src/com/fsck/k9/helper/Utility.java index 1c87e31fa..910989ef2 100644 --- a/src/com/fsck/k9/helper/Utility.java +++ b/src/com/fsck/k9/helper/Utility.java @@ -500,14 +500,20 @@ public class Utility { try { FileInputStream in = new FileInputStream(from); - FileOutputStream out = new FileOutputStream(to); - byte[] buffer = new byte[1024]; - int count = -1; - while ((count = in.read(buffer)) > 0) { - out.write(buffer, 0, count); + try { + FileOutputStream out = new FileOutputStream(to); + try { + byte[] buffer = new byte[1024]; + int count = -1; + while ((count = in.read(buffer)) > 0) { + out.write(buffer, 0, count); + } + } finally { + out.close(); + } + } finally { + try { in.close(); } catch (Throwable ignore) {} } - out.close(); - in.close(); from.delete(); return true; } catch (Exception e) { diff --git a/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java b/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java index 4ee161cbc..154776ca1 100644 --- a/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java +++ b/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java @@ -46,8 +46,11 @@ public class BinaryTempFileBody implements Body { public void writeTo(OutputStream out) throws IOException, MessagingException { InputStream in = getInputStream(); Base64OutputStream base64Out = new Base64OutputStream(out); - IOUtils.copy(in, base64Out); - base64Out.close(); + try { + IOUtils.copy(in, base64Out); + } finally { + base64Out.close(); + } mFile.delete(); } diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index 33ec8f7f5..d8ad792b2 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -1092,8 +1092,11 @@ public class MimeUtility { BinaryTempFileBody tempBody = new BinaryTempFileBody(); OutputStream out = tempBody.getOutputStream(); - IOUtils.copy(in, out); - out.close(); + try { + IOUtils.copy(in, out); + } finally { + out.close(); + } return tempBody; } diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index e7b0def3b..13b4c816e 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -2357,11 +2357,17 @@ public class LocalStore extends Store implements Serializable { * so we copy the data into a cached attachment file. */ InputStream in = attachment.getBody().getInputStream(); - tempAttachmentFile = File.createTempFile("att", null, attachmentDirectory); - FileOutputStream out = new FileOutputStream(tempAttachmentFile); - size = IOUtils.copy(in, out); - in.close(); - out.close(); + try { + tempAttachmentFile = File.createTempFile("att", null, attachmentDirectory); + FileOutputStream out = new FileOutputStream(tempAttachmentFile); + try { + size = IOUtils.copy(in, out); + } finally { + out.close(); + } + } finally { + try { in.close(); } catch (Throwable ignore) {} + } } } @@ -3357,8 +3363,11 @@ public class LocalStore extends Store implements Serializable { public void writeTo(OutputStream out) throws IOException, MessagingException { InputStream in = getInputStream(); Base64OutputStream base64Out = new Base64OutputStream(out); - IOUtils.copy(in, base64Out); - base64Out.close(); + try { + IOUtils.copy(in, base64Out); + } finally { + base64Out.close(); + } } public Uri getContentUri() { diff --git a/src/com/fsck/k9/provider/AttachmentProvider.java b/src/com/fsck/k9/provider/AttachmentProvider.java index 3b5360a21..542beab00 100644 --- a/src/com/fsck/k9/provider/AttachmentProvider.java +++ b/src/com/fsck/k9/provider/AttachmentProvider.java @@ -180,11 +180,14 @@ public class AttachmentProvider extends ContentProvider { if (thumbnail != null) { thumbnail = Bitmap.createScaledBitmap(thumbnail, width, height, true); FileOutputStream out = new FileOutputStream(file); - thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out); - out.close(); + try { + thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out); + } finally { + out.close(); + } } } finally { - in.close(); + try { in.close(); } catch (Throwable ignore) {} } } catch (IOException ioe) { return null;