From f6bc1dcca36cd6e5860a413e41ea7c9896c29137 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 20 Jun 2015 03:34:21 +0200 Subject: extract readTextFromUri into FileHelper --- .../keychain/ui/DecryptListFragment.java | 35 ++------------------ .../keychain/util/FileHelper.java | 37 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 32 deletions(-) (limited to 'OpenKeychain/src/main') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java index 65bcd022d..1199ebc27 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java @@ -18,11 +18,8 @@ package org.sufficientlysecure.keychain.ui; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -346,40 +343,14 @@ public class DecryptListFragment intent.putExtra(DisplayTextActivity.EXTRA_METADATA, result); intent.setDataAndType(outputUri, "text/plain"); + String plaintext; try { - - byte[] decryptedMessage; - { - InputStream in = activity.getContentResolver().openInputStream(outputUri); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buf = new byte[256]; - int read; - while ( (read = in.read(buf)) > 0) { - out.write(buf, 0, read); - } - in.close(); - out.close(); - decryptedMessage = out.toByteArray(); - } - - String plaintext; - if (result.getCharset() != null) { - try { - plaintext = new String(decryptedMessage, result.getCharset()); - } catch (UnsupportedEncodingException e) { - // if we can't decode properly, just fall back to utf-8 - plaintext = new String(decryptedMessage); - } - } else { - plaintext = new String(decryptedMessage); - } - - intent.putExtra(Intent.EXTRA_TEXT, plaintext); - + plaintext = FileHelper.readTextFromUri(activity, outputUri, result.getCharset()); } catch (IOException e) { Notify.create(activity, R.string.error_preparing_data, Style.ERROR).show(); return null; } + intent.putExtra(Intent.EXTRA_TEXT, plaintext); return intent; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java index 677acb1b8..558ab9a85 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java @@ -41,7 +41,11 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; import org.sufficientlysecure.keychain.ui.dialog.FileDialogFragment; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.text.DecimalFormat; public class FileHelper { @@ -234,6 +238,39 @@ public class FileHelper { return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; } + public static String readTextFromUri(Context context, Uri outputUri, String charset) + throws IOException { + + byte[] decryptedMessage; + { + InputStream in = context.getContentResolver().openInputStream(outputUri); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buf = new byte[256]; + int read; + while ( (read = in.read(buf)) > 0) { + out.write(buf, 0, read); + } + in.close(); + out.close(); + decryptedMessage = out.toByteArray(); + } + + String plaintext; + if (charset != null) { + try { + plaintext = new String(decryptedMessage, charset); + } catch (UnsupportedEncodingException e) { + // if we can't decode properly, just fall back to utf-8 + plaintext = new String(decryptedMessage); + } + } else { + plaintext = new String(decryptedMessage); + } + + return plaintext; + + } + public static interface FileDialogCallback { public void onFileSelected(File file, boolean checked); } -- cgit v1.2.3