Merge "Minimize default MIME map and optimize loading." am: d1bafa9ce1

am: 20615e9565

Change-Id: Iefe597a7413c013aa2971d9b500057738dfa56c8
This commit is contained in:
Tobias Thierer
2019-10-09 09:27:28 -07:00
committed by android-build-merger
3 changed files with 52 additions and 47 deletions

View File

@@ -60,7 +60,7 @@ java_genrule {
tools: [ tools: [
"soong_zip", "soong_zip",
], ],
srcs: [":mime.types"], srcs: [":mime.types.minimized"],
out: ["mimemap-res.jar"], out: ["mimemap-res.jar"],
cmd: "mkdir $(genDir)/res/ && cp $(in) $(genDir)/res/ && $(location soong_zip) -C $(genDir) -o $(out) -D $(genDir)/res/", cmd: "mkdir $(genDir)/res/ && cp $(in) $(genDir)/res/ && $(location soong_zip) -C $(genDir) -o $(out) -D $(genDir)/res/",
} }
@@ -73,42 +73,49 @@ java_genrule {
tools: [ tools: [
"soong_zip", "soong_zip",
], ],
srcs: [":mime.types"], srcs: [":mime.types.minimized"],
out: ["mimemap-testing-res.jar"], out: ["mimemap-testing-res.jar"],
cmd: "mkdir $(genDir)/testres/ && cp $(in) $(genDir)/testres/ && $(location soong_zip) -C $(genDir) -o $(out) -D $(genDir)/testres/", cmd: "mkdir $(genDir)/testres/ && cp $(in) $(genDir)/testres/ && $(location soong_zip) -C $(genDir) -o $(out) -D $(genDir)/testres/",
} }
// Combination of all *mime.types resources. // Combination of all *mime.types.minimized resources.
filegroup { filegroup {
name: "mime.types", name: "mime.types.minimized",
visibility: [ visibility: [
"//visibility:private", "//visibility:private",
], ],
srcs: [ srcs: [
":debian.mime.types", ":debian.mime.types.minimized",
":android.mime.types", ":android.mime.types.minimized",
":vendor.mime.types", ":vendor.mime.types.minimized",
], ],
} }
filegroup { java_genrule {
name: "android.mime.types", name: "android.mime.types.minimized",
visibility: [ visibility: [
"//visibility:private", "//visibility:private",
], ],
path: "java-res/", out: ["android.mime.types"],
srcs: [ srcs: [
"java-res/android.mime.types", "java-res/android.mime.types",
], ],
// strip comments normalize whitepace drop empty lines
cmd: "awk '{gsub(/#.*$$/,\"\"); $$1=$$1; print;}' $(in) | grep ' ' > $(out)",
} }
filegroup { // Unlike the other *mime.types files, vendor.mime.types gets '?' prepended to
name: "vendor.mime.types", // every field so that its mappings will never overwrite earlier mappings by
// the other resource files. http://b/141842825
java_genrule {
name: "vendor.mime.types.minimized",
visibility: [ visibility: [
"//visibility:private", "//visibility:private",
], ],
path: "java-res/", out: ["vendor.mime.types"],
srcs: [ srcs: [
"java-res/vendor.mime.types", "java-res/vendor.mime.types",
], ],
// strip comments normalize whitepace drop empty lines prepend ? to fields that are missing it
cmd: "awk '{gsub(/#.*$$/,\"\"); $$1=$$1; print;}' $(in) | grep ' ' | awk '{for(i=1;i<=NF;i++) { sub(/^\\??/, \"?\", $$i); }; print}' > $(out)",
} }

View File

@@ -39,3 +39,6 @@
# #
# Add your custom mappings below this line (with no "#" at the start of the line): # Add your custom mappings below this line (with no "#" at the start of the line):
test/mimeA extA extB extX
?test/mimeC ?extX ?extY ?extZ
test/mimeB extC ?extD extF

View File

@@ -23,11 +23,9 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern;
/** /**
* Creates the framework default {@link MimeMap}, a bidirectional mapping * Creates the framework default {@link MimeMap}, a bidirectional mapping
@@ -53,8 +51,6 @@ public class DefaultMimeMapFactory {
return create(resourceName -> c.getResourceAsStream("/res/" + resourceName)); return create(resourceName -> c.getResourceAsStream("/res/" + resourceName));
} }
private static final Pattern SPLIT_PATTERN = Pattern.compile("\\s+");
/** /**
* Creates a {@link MimeMap} instance whose resources are loaded from the * Creates a {@link MimeMap} instance whose resources are loaded from the
* InputStreams looked up in {@code resourceSupplier}. * InputStreams looked up in {@code resourceSupplier}.
@@ -63,33 +59,43 @@ public class DefaultMimeMapFactory {
*/ */
public static MimeMap create(Function<String, InputStream> resourceSupplier) { public static MimeMap create(Function<String, InputStream> resourceSupplier) {
MimeMap.Builder builder = MimeMap.builder(); MimeMap.Builder builder = MimeMap.builder();
parseTypes(builder, true, resourceSupplier, "mime.types"); // The files loaded here must be in minimized format with lines of the
parseTypes(builder, true, resourceSupplier, "android.mime.types"); // form "mime/type ext1 ext2 ext3", i.e. no comments, no empty lines, no
parseTypes(builder, false, resourceSupplier, "vendor.mime.types"); // leading/trailing whitespace and with a single space between entries on
// each line. See http://b/142267887
//
// Note: the order here matters - later entries can overwrite earlier ones
// (except that vendor.mime.types entries are prefixed with '?' which makes
// them never overwrite).
parseTypes(builder, resourceSupplier, "debian.mime.types");
parseTypes(builder, resourceSupplier, "android.mime.types");
parseTypes(builder, resourceSupplier, "vendor.mime.types");
return builder.build(); return builder.build();
} }
private static void parseTypes(MimeMap.Builder builder, boolean allowOverwrite, private static void parseTypes(MimeMap.Builder builder,
Function<String, InputStream> resourceSupplier, String resourceName) { Function<String, InputStream> resourceSupplier, String resourceName) {
try (InputStream inputStream = Objects.requireNonNull(resourceSupplier.apply(resourceName)); try (InputStream inputStream = Objects.requireNonNull(resourceSupplier.apply(resourceName));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line; String line;
List<String> specs = new ArrayList<>(10); // re-use for each line
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
int commentPos = line.indexOf('#'); specs.clear();
if (commentPos >= 0) { // Lines are of the form "mimeSpec extSpec extSpec[...]" with a single space
line = line.substring(0, commentPos); // separating them and no leading/trailing spaces and no empty lines.
int startIdx = 0;
do {
int endIdx = line.indexOf(' ', startIdx);
if (endIdx < 0) {
endIdx = line.length();
} }
line = line.trim(); String spec = line.substring(startIdx, endIdx);
if (line.isEmpty()) { if (spec.isEmpty()) {
continue; throw new IllegalArgumentException("Malformed line: " + line);
}
List<String> specs = Arrays.asList(SPLIT_PATTERN.split(line));
if (!allowOverwrite) {
// Pretend that the mimeType and each file extension listed in the line
// carries a "?" prefix, which means that it can add new mappings but
// not modify existing mappings (putIfAbsent() semantics).
specs = ensurePrefix("?", specs);
} }
specs.add(spec);
startIdx = endIdx + 1; // skip over the space
} while (startIdx < line.length());
builder.put(specs.get(0), specs.subList(1, specs.size())); builder.put(specs.get(0), specs.subList(1, specs.size()));
} }
} catch (IOException | RuntimeException e) { } catch (IOException | RuntimeException e) {
@@ -97,15 +103,4 @@ public class DefaultMimeMapFactory {
} }
} }
private static List<String> ensurePrefix(String prefix, List<String> strings) {
List<String> result = new ArrayList<>(strings.size());
for (String s : strings) {
if (!s.startsWith(prefix)) {
s = prefix + s;
}
result.add(s);
}
return result;
}
} }