--- old/src/share/classes/java/net/URLConnection.java 2020-06-26 21:42:19.098054226 +0100 +++ new/src/share/classes/java/net/URLConnection.java 2020-06-26 21:42:19.002053015 +0100 @@ -285,12 +285,7 @@ /** * @since JDK1.1 */ - private static FileNameMap fileNameMap; - - /** - * @since 1.2.2 - */ - private static boolean fileNameMapLoaded = false; + private static volatile FileNameMap fileNameMap; /** * Loads filename map (a mimetable) from a data file. It will @@ -302,18 +297,21 @@ * @since 1.2 * @see #setFileNameMap(java.net.FileNameMap) */ - public static synchronized FileNameMap getFileNameMap() { - if ((fileNameMap == null) && !fileNameMapLoaded) { - fileNameMap = sun.net.www.MimeTable.loadTable(); - fileNameMapLoaded = true; + public static FileNameMap getFileNameMap() { + FileNameMap map = fileNameMap; + + if (map == null) { + fileNameMap = map = new FileNameMap() { + private FileNameMap internalMap = + sun.net.www.MimeTable.loadTable(); + + public String getContentTypeFor(String fileName) { + return internalMap.getContentTypeFor(fileName); + } + }; } - return new FileNameMap() { - private FileNameMap map = fileNameMap; - public String getContentTypeFor(String fileName) { - return map.getContentTypeFor(fileName); - } - }; + return map; } /** --- old/src/share/classes/sun/nio/fs/AbstractFileTypeDetector.java 2020-06-26 21:42:19.483059086 +0100 +++ new/src/share/classes/sun/nio/fs/AbstractFileTypeDetector.java 2020-06-26 21:42:19.389057899 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,8 @@ package sun.nio.fs; +import java.net.FileNameMap; +import java.net.URLConnection; import java.nio.file.Path; import java.nio.file.spi.FileTypeDetector; import java.util.Locale; @@ -50,6 +52,16 @@ if (file == null) throw new NullPointerException("'file' is null"); String result = implProbeContentType(file); + + // Fall back to content types property. + if (result == null) { + Path fileName = file.getFileName(); + if (fileName != null) { + FileNameMap fileNameMap = URLConnection.getFileNameMap(); + result = fileNameMap.getContentTypeFor(fileName.toString()); + } + } + return (result == null) ? null : parse(result); } --- old/test/java/nio/file/Files/probeContentType/Basic.java 2020-06-26 21:42:19.867063931 +0100 +++ new/test/java/nio/file/Files/probeContentType/Basic.java 2020-06-26 21:42:19.772062733 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,21 +22,24 @@ */ /* @test - * @bug 4313887 + * @bug 4313887 8146215 * @summary Unit test for probeContentType method * @library ../.. * @build Basic SimpleFileTypeDetector * @run main/othervm Basic */ -import java.nio.file.*; import java.io.*; +import java.nio.file.*; +import java.util.stream.Stream; /** - * Uses Files.probeContentType to probe html file and custom file type. + * Uses Files.probeContentType to probe html file, custom file type, and minimal + * set of file extension to content type mappings. */ - public class Basic { + private static final boolean IS_UNIX = + ! System.getProperty("os.name").startsWith("Windows"); static Path createHtmlFile() throws IOException { Path file = Files.createTempFile("foo", ".html"); @@ -51,7 +54,49 @@ return Files.createTempFile("red", ".grape"); } + private static void checkMimeTypesFile(Path mimeTypes) { + if (!Files.exists(mimeTypes)) { + System.out.println(mimeTypes + " does not exist"); + } else if (!Files.isReadable(mimeTypes)) { + System.out.println(mimeTypes + " is not readable"); + } else { + System.out.println(mimeTypes + " contents:"); + try (Stream lines = Files.lines(mimeTypes)) { + lines.forEach(System.out::println); + System.out.println(""); + } catch (IOException ioe) { + System.err.printf("Problem reading %s: %s%n", + mimeTypes, ioe.getMessage()); + } + } + } + + private static int checkContentTypes(String expected, String actual) { + assert expected != null; + assert actual != null; + + if (!expected.equals(actual)) { + if (IS_UNIX) { + Path userMimeTypes = + Paths.get(System.getProperty("user.home"), ".mime.types"); + checkMimeTypesFile(userMimeTypes); + + Path etcMimeTypes = Paths.get("/etc/mime.types"); + checkMimeTypesFile(etcMimeTypes); + } + + System.err.println("Expected \"" + expected + + "\" but obtained \"" + + actual + "\""); + + return 1; + } + + return 0; + } + public static void main(String[] args) throws IOException { + int failures = 0; // exercise default file type detector Path file = createHtmlFile(); @@ -60,8 +105,7 @@ if (type == null) { System.err.println("Content type cannot be determined - test skipped"); } else { - if (!type.equals("text/html")) - throw new RuntimeException("Unexpected type: " + type); + failures += checkContentTypes("text/html", type); } } finally { Files.delete(file); @@ -71,13 +115,18 @@ file = createGrapeFile(); try { String type = Files.probeContentType(file); - if (type == null) - throw new RuntimeException("Custom file type detector not installed?"); - if (!type.equals("grape/unknown")) - throw new RuntimeException("Unexpected type: " + type); + if (type == null) { + System.err.println("Custom file type detector not installed?"); + failures++; + } else { + failures += checkContentTypes("grape/unknown", type); + } } finally { Files.delete(file); } + if (failures > 0) { + throw new RuntimeException("Test failed!"); + } } }