From af0cef987da359955023fde97f0f4c7444fd5e76 Mon Sep 17 00:00:00 2001 From: Tobias Thierer Date: Fri, 27 Sep 2019 17:08:49 +0100 Subject: MimeMapImpl.createDefaultInstance() -> DefaultMimeMapFactory.create(). The class no longer implements MimeMap, so the name MimeMapImpl no longer made sense. Test: Treehugger Bug: 136256059 Change-Id: I2cbc70a7769232b704a9bdfde2def832c1e292b8 --- mime/Android.bp | 2 +- .../content/type/DefaultMimeMapFactory.java | 81 ++++++++++++++++++++++ mime/java/android/content/type/MimeMapImpl.java | 78 --------------------- 3 files changed, 82 insertions(+), 79 deletions(-) create mode 100644 mime/java/android/content/type/DefaultMimeMapFactory.java delete mode 100644 mime/java/android/content/type/MimeMapImpl.java (limited to 'mime') diff --git a/mime/Android.bp b/mime/Android.bp index 9303755ba73d..17bad746e039 100644 --- a/mime/Android.bp +++ b/mime/Android.bp @@ -20,7 +20,7 @@ java_library { ], srcs: [ - "java/android/content/type/MimeMapImpl.java", + "java/android/content/type/DefaultMimeMapFactory.java", ], java_resources: [ diff --git a/mime/java/android/content/type/DefaultMimeMapFactory.java b/mime/java/android/content/type/DefaultMimeMapFactory.java new file mode 100644 index 000000000000..545fb3cbb5cd --- /dev/null +++ b/mime/java/android/content/type/DefaultMimeMapFactory.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.content.type; + +import libcore.net.MimeMap; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; + +/** + * Creates the framework default {@link MimeMap}, a bidirectional mapping + * between MIME types and file extensions. + * + * This default mapping is loaded from data files that start with some mappings + * recognized by IANA plus some custom extensions and overrides. + * + * @hide + */ +public class DefaultMimeMapFactory { + + private DefaultMimeMapFactory() { + } + + /** + * Creates and returns a new {@link MimeMap} instance that implements. + * Android's default mapping between MIME types and extensions. + */ + public static MimeMap create() { + return parseFromResources("/mime.types", "/android.mime.types"); + } + + private static final Pattern SPLIT_PATTERN = Pattern.compile("\\s+"); + + static MimeMap parseFromResources(String... resourceNames) { + MimeMap.Builder builder = MimeMap.builder(); + for (String resourceName : resourceNames) { + parseTypes(builder, resourceName); + } + return builder.build(); + } + + private static void parseTypes(MimeMap.Builder builder, String resource) { + try (BufferedReader r = new BufferedReader( + new InputStreamReader(DefaultMimeMapFactory.class.getResourceAsStream(resource)))) { + String line; + while ((line = r.readLine()) != null) { + int commentPos = line.indexOf('#'); + if (commentPos >= 0) { + line = line.substring(0, commentPos); + } + line = line.trim(); + if (line.isEmpty()) { + continue; + } + List specs = Arrays.asList(SPLIT_PATTERN.split(line)); + builder.put(specs.get(0), specs.subList(1, specs.size())); + } + } catch (IOException | RuntimeException e) { + throw new RuntimeException("Failed to parse " + resource, e); + } + } + +} diff --git a/mime/java/android/content/type/MimeMapImpl.java b/mime/java/android/content/type/MimeMapImpl.java deleted file mode 100644 index 367160367cc0..000000000000 --- a/mime/java/android/content/type/MimeMapImpl.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.content.type; - -import libcore.net.MimeMap; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.Arrays; -import java.util.List; -import java.util.regex.Pattern; - -/** - * Default implementation of {@link MimeMap}, a bidirectional mapping between - * MIME types and file extensions. - * - * This default mapping is loaded from data files that start with some mappings - * recognized by IANA plus some custom extensions and overrides. - * - * @hide - */ -public class MimeMapImpl { - - /** - * Creates and returns a new {@link MimeMapImpl} instance that implements. - * Android's default mapping between MIME types and extensions. - */ - public static MimeMap createDefaultInstance() { - return parseFromResources("/mime.types", "/android.mime.types"); - } - - private static final Pattern SPLIT_PATTERN = Pattern.compile("\\s+"); - - static MimeMap parseFromResources(String... resourceNames) { - MimeMap.Builder builder = MimeMap.builder(); - for (String resourceName : resourceNames) { - parseTypes(builder, resourceName); - } - return builder.build(); - } - - private static void parseTypes(MimeMap.Builder builder, String resource) { - try (BufferedReader r = new BufferedReader( - new InputStreamReader(MimeMapImpl.class.getResourceAsStream(resource)))) { - String line; - while ((line = r.readLine()) != null) { - int commentPos = line.indexOf('#'); - if (commentPos >= 0) { - line = line.substring(0, commentPos); - } - line = line.trim(); - if (line.isEmpty()) { - continue; - } - List specs = Arrays.asList(SPLIT_PATTERN.split(line)); - builder.put(specs.get(0), specs.subList(1, specs.size())); - } - } catch (IOException | RuntimeException e) { - throw new RuntimeException("Failed to parse " + resource, e); - } - } - -} -- cgit v1.2.3