aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2015-06-20 03:34:21 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2015-06-20 03:34:21 +0200
commitf6bc1dcca36cd6e5860a413e41ea7c9896c29137 (patch)
tree4c2ffdd7723314ac5725617defe4c82fc79319d5 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util
parent25beeaceb59f9d536d2d9408fba47f9fc3700627 (diff)
downloadopen-keychain-f6bc1dcca36cd6e5860a413e41ea7c9896c29137.tar.gz
open-keychain-f6bc1dcca36cd6e5860a413e41ea7c9896c29137.tar.bz2
open-keychain-f6bc1dcca36cd6e5860a413e41ea7c9896c29137.zip
extract readTextFromUri into FileHelper
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java37
1 files changed, 37 insertions, 0 deletions
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);
}