Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1855)

Unified Diff: net/android/java/src/org/chromium/net/NetStringUtil.java

Issue 266053002: Implement alternative string conversion functions in net/ on Android, (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove GN changes Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/DEPS ('k') | net/android/net_jni_registrar.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/android/java/src/org/chromium/net/NetStringUtil.java
diff --git a/net/android/java/src/org/chromium/net/NetStringUtil.java b/net/android/java/src/org/chromium/net/NetStringUtil.java
new file mode 100644
index 0000000000000000000000000000000000000000..4ab1bd9f5bd8f7eb98edaffb119fa814f8202d6e
--- /dev/null
+++ b/net/android/java/src/org/chromium/net/NetStringUtil.java
@@ -0,0 +1,89 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.net;
+
+import org.chromium.base.CalledByNative;
+import org.chromium.base.JNINamespace;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CodingErrorAction;
+import java.text.Normalizer;
+
+/**
+ * Utility functions for converting strings between formats when not built with
+ * icu.
+ */
+@JNINamespace("net::android")
+public class NetStringUtil {
+ /**
+ * Attempts to convert text in a given character set to a Unicode string.
+ * Returns null on failure.
+ * @param text ByteBuffer containing the character array to convert.
+ * @param charset Character set it's in encoded in.
+ * @return: Unicode string on success, null on failure.
+ */
+ @CalledByNative
+ private static String convertToUnicode(
+ ByteBuffer text,
+ String charset_name) {
+ try {
+ Charset charset = Charset.forName(charset_name);
+ CharsetDecoder decoder = charset.newDecoder();
+ // On invalid characters, this will throw an exception.
+ return decoder.decode(text).toString();
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ /**
+ * Attempts to convert text in a given character set to a Unicode string,
+ * and normalize it. Returns null on failure.
+ * @param text ByteBuffer containing the character array to convert.
+ * @param charset Character set it's in encoded in.
+ * @return: Unicode string on success, null on failure.
+ */
+ @CalledByNative
+ private static String convertToUnicodeAndNormalize(
+ ByteBuffer text,
+ String charset_name) {
+ String unicodeString = convertToUnicode(text, charset_name);
+ if (unicodeString == null)
+ return unicodeString;
+ return Normalizer.normalize(unicodeString, Normalizer.Form.NFC);
+ }
+
+ /**
+ * Convert text in a given character set to a Unicode string. Any invalid
+ * characters are replaced with U+FFFD. Returns null if the character set
+ * is not recognized.
+ * @param text ByteBuffer containing the character array to convert.
+ * @param charset Character set it's in encoded in.
+ * @return: Unicode string on success, null on failure.
+ */
+ @CalledByNative
+ private static String convertToUnicodeWithSubstitutions(
+ ByteBuffer text,
+ String charset_name) {
+ try {
+ Charset charset = Charset.forName(charset_name);
+
+ // TODO(mmenke): Investigate if Charset.decode() can be used
+ // instead. The question is whether it uses the proper replace
+ // character. JDK CharsetDecoder docs say U+FFFD is the default,
+ // but Charset.decode() docs say it uses the "charset's default
+ // replacement byte array".
+ CharsetDecoder decoder = charset.newDecoder();
+ decoder.onMalformedInput(CodingErrorAction.REPLACE);
+ decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+ decoder.replaceWith("\uFFFD");
+ return decoder.decode(text).toString();
+ } catch (Exception e) {
+ return null;
+ }
+ }
+}
« no previous file with comments | « net/DEPS ('k') | net/android/net_jni_registrar.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698