Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.content.type; |
| 18 | |
| 19 | import libcore.net.MimeMap; |
| 20 | |
| 21 | import java.io.BufferedReader; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStreamReader; |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 24 | import java.util.Arrays; |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 25 | import java.util.List; |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 26 | import java.util.regex.Pattern; |
| 27 | |
| 28 | /** |
| 29 | * Default implementation of {@link MimeMap}, a bidirectional mapping between |
| 30 | * MIME types and file extensions. |
| 31 | * |
| 32 | * This default mapping is loaded from data files that start with some mappings |
| 33 | * recognized by IANA plus some custom extensions and overrides. |
| 34 | * |
| 35 | * @hide |
| 36 | */ |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 37 | public class MimeMapImpl { |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Creates and returns a new {@link MimeMapImpl} instance that implements. |
| 41 | * Android's default mapping between MIME types and extensions. |
| 42 | */ |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 43 | public static MimeMap createDefaultInstance() { |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 44 | return parseFromResources("/mime.types", "/android.mime.types"); |
| 45 | } |
| 46 | |
| 47 | private static final Pattern SPLIT_PATTERN = Pattern.compile("\\s+"); |
| 48 | |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 49 | static MimeMap parseFromResources(String... resourceNames) { |
| 50 | MimeMap.Builder builder = MimeMap.builder(); |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 51 | for (String resourceName : resourceNames) { |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 52 | parseTypes(builder, resourceName); |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 53 | } |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 54 | return builder.build(); |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 57 | private static void parseTypes(MimeMap.Builder builder, String resource) { |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 58 | try (BufferedReader r = new BufferedReader( |
| 59 | new InputStreamReader(MimeMapImpl.class.getResourceAsStream(resource)))) { |
| 60 | String line; |
| 61 | while ((line = r.readLine()) != null) { |
| 62 | int commentPos = line.indexOf('#'); |
| 63 | if (commentPos >= 0) { |
| 64 | line = line.substring(0, commentPos); |
| 65 | } |
| 66 | line = line.trim(); |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 67 | if (line.isEmpty()) { |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 68 | continue; |
| 69 | } |
Tobias Thierer | fd9657d | 2019-08-20 15:23:34 +0100 | [diff] [blame^] | 70 | List<String> specs = Arrays.asList(SPLIT_PATTERN.split(line)); |
| 71 | builder.put(specs.get(0), specs.subList(1, specs.size())); |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 72 | } |
| 73 | } catch (IOException | RuntimeException e) { |
| 74 | throw new RuntimeException("Failed to parse " + resource, e); |
| 75 | } |
| 76 | } |
| 77 | |
Tobias Thierer | 878c77b | 2019-08-18 15:19:45 +0100 | [diff] [blame] | 78 | } |