From 5175ff9df4d0efbc2b453f3ed682b85a78ee8ad8 Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 4 Feb 2015 21:17:54 +0100 Subject: [PATCH] Extract method --- .../k9/ui/messageview/DownloadImageTask.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/DownloadImageTask.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/DownloadImageTask.java index 68eeab3f0..ea15fa617 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/DownloadImageTask.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/DownloadImageTask.java @@ -3,6 +3,7 @@ package com.fsck.k9.ui.messageview; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; @@ -101,19 +102,7 @@ class DownloadImageTask extends AsyncTask { filename = getFileNameWithExtension(filename, mimeType); - String sanitized = FileHelper.sanitizeFilename(filename); - - File directory = new File(K9.getAttachmentDefaultPath()); - File file = FileHelper.createUniqueFile(directory, sanitized); - FileOutputStream out = new FileOutputStream(file); - try { - IOUtils.copy(in, out); - out.flush(); - } finally { - out.close(); - } - - return file.getName(); + return writeFileToStorage(filename, in); } finally { if (in != null) { @@ -143,6 +132,23 @@ class DownloadImageTask extends AsyncTask { return filename + "." + extension; } + private String writeFileToStorage(String filename, InputStream in) throws IOException { + String sanitized = FileHelper.sanitizeFilename(filename); + + File directory = new File(K9.getAttachmentDefaultPath()); + File file = FileHelper.createUniqueFile(directory, sanitized); + + FileOutputStream out = new FileOutputStream(file); + try { + IOUtils.copy(in, out); + out.flush(); + } finally { + out.close(); + } + + return file.getName(); + } + @Override protected void onPostExecute(String filename) { String text;