1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-18 07:25:15 -05:00

Merge pull request #112 from eperez/master

Close resources properly
This commit is contained in:
cketti 2012-01-01 15:50:57 -08:00
commit 4b5189973b
5 changed files with 45 additions and 21 deletions

View File

@ -500,14 +500,20 @@ public class Utility {
try { try {
FileInputStream in = new FileInputStream(from); FileInputStream in = new FileInputStream(from);
try {
FileOutputStream out = new FileOutputStream(to); FileOutputStream out = new FileOutputStream(to);
try {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int count = -1; int count = -1;
while ((count = in.read(buffer)) > 0) { while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count); out.write(buffer, 0, count);
} }
} finally {
out.close(); out.close();
in.close(); }
} finally {
try { in.close(); } catch (Throwable ignore) {}
}
from.delete(); from.delete();
return true; return true;
} catch (Exception e) { } catch (Exception e) {

View File

@ -46,8 +46,11 @@ public class BinaryTempFileBody implements Body {
public void writeTo(OutputStream out) throws IOException, MessagingException { public void writeTo(OutputStream out) throws IOException, MessagingException {
InputStream in = getInputStream(); InputStream in = getInputStream();
Base64OutputStream base64Out = new Base64OutputStream(out); Base64OutputStream base64Out = new Base64OutputStream(out);
try {
IOUtils.copy(in, base64Out); IOUtils.copy(in, base64Out);
} finally {
base64Out.close(); base64Out.close();
}
mFile.delete(); mFile.delete();
} }

View File

@ -1092,8 +1092,11 @@ public class MimeUtility {
BinaryTempFileBody tempBody = new BinaryTempFileBody(); BinaryTempFileBody tempBody = new BinaryTempFileBody();
OutputStream out = tempBody.getOutputStream(); OutputStream out = tempBody.getOutputStream();
try {
IOUtils.copy(in, out); IOUtils.copy(in, out);
} finally {
out.close(); out.close();
}
return tempBody; return tempBody;
} }

View File

@ -2357,12 +2357,18 @@ public class LocalStore extends Store implements Serializable {
* so we copy the data into a cached attachment file. * so we copy the data into a cached attachment file.
*/ */
InputStream in = attachment.getBody().getInputStream(); InputStream in = attachment.getBody().getInputStream();
try {
tempAttachmentFile = File.createTempFile("att", null, attachmentDirectory); tempAttachmentFile = File.createTempFile("att", null, attachmentDirectory);
FileOutputStream out = new FileOutputStream(tempAttachmentFile); FileOutputStream out = new FileOutputStream(tempAttachmentFile);
try {
size = IOUtils.copy(in, out); size = IOUtils.copy(in, out);
in.close(); } finally {
out.close(); out.close();
} }
} finally {
try { in.close(); } catch (Throwable ignore) {}
}
}
} }
if (size == -1) { if (size == -1) {
@ -3357,9 +3363,12 @@ public class LocalStore extends Store implements Serializable {
public void writeTo(OutputStream out) throws IOException, MessagingException { public void writeTo(OutputStream out) throws IOException, MessagingException {
InputStream in = getInputStream(); InputStream in = getInputStream();
Base64OutputStream base64Out = new Base64OutputStream(out); Base64OutputStream base64Out = new Base64OutputStream(out);
try {
IOUtils.copy(in, base64Out); IOUtils.copy(in, base64Out);
} finally {
base64Out.close(); base64Out.close();
} }
}
public Uri getContentUri() { public Uri getContentUri() {
return mUri; return mUri;

View File

@ -180,11 +180,14 @@ public class AttachmentProvider extends ContentProvider {
if (thumbnail != null) { if (thumbnail != null) {
thumbnail = Bitmap.createScaledBitmap(thumbnail, width, height, true); thumbnail = Bitmap.createScaledBitmap(thumbnail, width, height, true);
FileOutputStream out = new FileOutputStream(file); FileOutputStream out = new FileOutputStream(file);
try {
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out); thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out);
} finally {
out.close(); out.close();
} }
}
} finally { } finally {
in.close(); try { in.close(); } catch (Throwable ignore) {}
} }
} catch (IOException ioe) { } catch (IOException ioe) {
return null; return null;