Merge changes I92be8a46,I7a3b449f

* changes:
  MimeTypes: Change Locale for lowercase from US to ROOT.
  Extract helper methods canonicalize() and allowedInMap().
diff --git a/luni/src/main/java/libcore/net/MimeUtils.java b/luni/src/main/java/libcore/net/MimeUtils.java
index cc37295..8cd0147 100644
--- a/luni/src/main/java/libcore/net/MimeUtils.java
+++ b/luni/src/main/java/libcore/net/MimeUtils.java
@@ -35,10 +35,15 @@
 
     private static final Pattern splitPattern = Pattern.compile("\\s+");
 
-    // Note: These maps only contain lowercase keys/values, regarded as the canonical form.
-    // This is the case for both extensions and MIME types. The mime.types data file contains
-    // examples of mixed-case MIME types, but some applications use the lowercase version of
-    // these same types. RFC 2045 section 2 states MIME types are case insensitive.
+    /**
+     * Note: These maps only contain lowercase keys/values, regarded as the
+     * {@link #canonicalize(String) canonical form}.
+     *
+     * <p>This is the case for both extensions and MIME types. The mime.types
+     * data file contains examples of mixed-case MIME types, but some applications
+     * use the lowercase version of these same types. RFC 2045 section 2 states
+     * that MIME types are case insensitive.
+     */
     private static final Map<String, String> mimeTypeToExtensionMap = new HashMap<>();
     private static final Map<String, String> extensionToMimeTypeMap = new HashMap<>();
 
@@ -62,9 +67,17 @@
                 }
 
                 final String[] split = splitPattern.split(line);
-                final String mimeType = split[0].toLowerCase(Locale.US);
+                final String mimeType = canonicalize(split[0]);
+                if (!allowedInMap(mimeType)) {
+                    throw new IllegalArgumentException(
+                            "Invalid mimeType " + mimeType + " in: " + line);
+                }
                 for (int i = 1; i < split.length; i++) {
-                    String extension = split[i].toLowerCase(Locale.US);
+                    String extension = canonicalize(split[i]);
+                    if (!allowedInMap(extension)) {
+                        throw new IllegalArgumentException(
+                                "Invalid extension " + extension + " in: " + line);
+                    }
 
                     // Normally the first MIME type definition wins, and the
                     // last extension definition wins. However, a file can
@@ -96,6 +109,21 @@
     }
 
     /**
+     * Returns the canonical (lowercase) form of the given extension or MIME type.
+     */
+    private static String canonicalize(String s) {
+        return s.toLowerCase(Locale.ROOT);
+    }
+
+    /**
+     * Checks whether the given extension or MIME type might be valid and
+     * therefore may appear in the mimeType <-> extension maps.
+     */
+    private static boolean allowedInMap(String s) {
+        return s != null && !s.isEmpty();
+    }
+
+    /**
      * Returns true if the given case insensitive MIME type has an entry in the map.
      * @param mimeType A MIME type (i.e. text/plain)
      * @return True if a extension has been registered for
@@ -115,10 +143,10 @@
     @UnsupportedAppUsage
     @libcore.api.CorePlatformApi
     public static String guessMimeTypeFromExtension(String extension) {
-        if (extension == null || extension.isEmpty()) {
+        if (!allowedInMap(extension)) {
             return null;
         }
-        extension = extension.toLowerCase(Locale.US);
+        extension = canonicalize(extension);
         return extensionToMimeTypeMap.get(extension);
     }
 
@@ -144,10 +172,10 @@
     @UnsupportedAppUsage
     @libcore.api.CorePlatformApi
     public static String guessExtensionFromMimeType(String mimeType) {
-        if (mimeType == null || mimeType.isEmpty()) {
+        if (!allowedInMap(mimeType)) {
             return null;
         }
-        mimeType = mimeType.toLowerCase(Locale.US);
+        mimeType = canonicalize(mimeType);
         return mimeTypeToExtensionMap.get(mimeType);
     }
 }
diff --git a/luni/src/test/java/libcore/libcore/net/MimeUtilsTest.java b/luni/src/test/java/libcore/libcore/net/MimeUtilsTest.java
index 7d863e5..cf1e233 100644
--- a/luni/src/test/java/libcore/libcore/net/MimeUtilsTest.java
+++ b/luni/src/test/java/libcore/libcore/net/MimeUtilsTest.java
@@ -153,6 +153,31 @@
         assertEquals("apk", MimeUtils.guessExtensionFromMimeType(mimeType.toLowerCase(Locale.US)));
     }
 
+    public void test_invalid_empty() {
+        checkInvalidExtension("");
+        checkInvalidMimeType("");
+    }
+
+    public void test_invalid_null() {
+        checkInvalidExtension(null);
+        checkInvalidMimeType(null);
+    }
+
+    public void test_invalid() {
+        checkInvalidMimeType("invalid mime type");
+        checkInvalidExtension("invalid extension");
+    }
+
+    private static void checkInvalidExtension(String s) {
+        assertFalse(MimeUtils.hasExtension(s));
+        assertNull(MimeUtils.guessMimeTypeFromExtension(s));
+    }
+
+    private static void checkInvalidMimeType(String s) {
+        assertFalse(MimeUtils.hasMimeType(s));
+        assertNull(MimeUtils.guessExtensionFromMimeType(s));
+    }
+
     private static void assertMimeTypeFromExtension(String mimeType, String extension) {
         final String actual = MimeUtils.guessMimeTypeFromExtension(extension);
         if (!Objects.equals(mimeType, actual)) {