From 53bc417f8f77a9f92786457281d02431ef614ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 15 Sep 2014 10:19:55 +0200 Subject: New decrypt activity design (WIP), saner UTF8 decoding (replacing non-decodable characters) --- .../sufficientlysecure/keychain/util/Utf8Util.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Utf8Util.java (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Utf8Util.java') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Utf8Util.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Utf8Util.java new file mode 100644 index 000000000..bed3e28ed --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Utf8Util.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.util; + +import org.sufficientlysecure.keychain.Constants; + +import java.nio.ByteBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CodingErrorAction; + +public class Utf8Util { + + public static boolean isValidUTF8(byte[] input) { + CharsetDecoder cs = Charset.forName("UTF-8").newDecoder(); + + try { + cs.decode(ByteBuffer.wrap(input)); + return true; + } catch (CharacterCodingException e) { + return false; + } + } + + public static String fromUTF8ByteArrayReplaceBadEncoding(byte[] input) { + final CharsetDecoder charsetDecoder = Charset.forName("UTF-8").newDecoder(); + charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE); + charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + + try { + return charsetDecoder.decode(ByteBuffer.wrap(input)).toString(); + } catch (CharacterCodingException e) { + Log.e(Constants.TAG, "Decoding failed!", e); + return charsetDecoder.replacement(); + } + } +} -- cgit v1.2.3