From a77ed30fcc3360cd16a11b1899f52f7e871df1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Thu, 2 Jan 2025 09:19:58 +0000 Subject: [PATCH] 8336412: sun.net.www.MimeTable has a few unused methods Reviewed-by: jpai --- .../share/classes/java/net/URLConnection.java | 2 +- .../share/classes/sun/net/www/MimeTable.java | 204 ++---------------- 2 files changed, 24 insertions(+), 182 deletions(-) diff --git a/src/java.base/share/classes/java/net/URLConnection.java b/src/java.base/share/classes/java/net/URLConnection.java index 69f719425615a..585ab4fa15aca 100644 --- a/src/java.base/share/classes/java/net/URLConnection.java +++ b/src/java.base/share/classes/java/net/URLConnection.java @@ -306,7 +306,7 @@ public static FileNameMap getFileNameMap() { if (map == null) { fileNameMap = map = new FileNameMap() { private final FileNameMap internalMap = - sun.net.www.MimeTable.loadTable(); + sun.net.www.MimeTable.getDefaultTable(); public String getContentTypeFor(String fileName) { return internalMap.getContentTypeFor(fileName); diff --git a/src/java.base/share/classes/sun/net/www/MimeTable.java b/src/java.base/share/classes/sun/net/www/MimeTable.java index eea633e94fa2c..5b7602416718c 100644 --- a/src/java.base/share/classes/sun/net/www/MimeTable.java +++ b/src/java.base/share/classes/sun/net/www/MimeTable.java @@ -25,12 +25,9 @@ package sun.net.www; -import jdk.internal.util.StaticProperty; - import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.InputStream; import java.io.IOException; import java.net.FileNameMap; @@ -40,30 +37,22 @@ import java.util.Properties; import java.util.StringTokenizer; -public class MimeTable implements FileNameMap { +public final class MimeTable implements FileNameMap { + /** Hash mark introducing a URI fragment */ private static final int HASH_MARK = '#'; - /** Keyed by content type, returns MimeEntries */ - private final Hashtable entries = new Hashtable<>(); - /** Keyed by file extension (with the .), returns MimeEntries */ private final Hashtable extensionMap = new Hashtable<>(); - // Will be reset if in the platform-specific data file - private static String tempFileTemplate = - System.getProperty("content.types.temp.file.template", "/tmp/%s"); - - private static final String filePreamble = "sun.net.www MIME content-types table"; - - MimeTable() { + private MimeTable() { load(); } - private static class DefaultInstanceHolder { - static final MimeTable defaultInstance = getDefaultInstance(); + private static final class DefaultInstanceHolder { + private static final MimeTable defaultInstance = getDefaultInstance(); - static MimeTable getDefaultInstance() { + private static MimeTable getDefaultInstance() { final MimeTable instance = new MimeTable(); URLConnection.setFileNameMap(instance); return instance; @@ -78,15 +67,6 @@ public static MimeTable getDefaultTable() { return DefaultInstanceHolder.defaultInstance; } - public static FileNameMap loadTable() { - MimeTable mt = getDefaultTable(); - return mt; - } - - public synchronized int getSize() { - return entries.size(); - } - public synchronized String getContentTypeFor(String fileName) { MimeEntry entry = findByFileName(fileName); if (entry != null) { @@ -96,55 +76,21 @@ public synchronized String getContentTypeFor(String fileName) { } } - public synchronized void add(MimeEntry m) { - entries.put(m.getType(), m); - - String exts[] = m.getExtensions(); + private void add(MimeEntry m) { + String[] exts = m.getExtensions(); if (exts == null) { return; } - for (int i = 0; i < exts.length; i++) { - extensionMap.put(exts[i], m); + for (String ext : exts) { + extensionMap.put(ext, m); } } - public synchronized MimeEntry remove(String type) { - MimeEntry entry = entries.get(type); - return remove(entry); - } - - public synchronized MimeEntry remove(MimeEntry entry) { - String[] extensionKeys = entry.getExtensions(); - if (extensionKeys != null) { - for (int i = 0; i < extensionKeys.length; i++) { - extensionMap.remove(extensionKeys[i]); - } - } - - return entries.remove(entry.getType()); - } - - public synchronized MimeEntry find(String type) { - MimeEntry entry = entries.get(type); - if (entry == null) { - // try a wildcard lookup - Enumeration e = entries.elements(); - while (e.hasMoreElements()) { - MimeEntry wild = e.nextElement(); - if (wild.matches(type)) { - return wild; - } - } - } - - return entry; - } - /** * Extracts the file extension and uses it to look up the entry. */ - private MimeEntry findViaFileExtension(String fname) { + private MimeEntry findByFileExtension(String fname) { int i = fname.lastIndexOf('.'); // REMIND: OS specific delimiters appear here i = Math.max(i, fname.lastIndexOf('/')); @@ -155,7 +101,8 @@ private MimeEntry findViaFileExtension(String fname) { ext = fname.substring(i).toLowerCase(Locale.ROOT); } - return findByExt(ext); + return extensionMap.get(ext); + } /** @@ -166,74 +113,27 @@ private MimeEntry findViaFileExtension(String fname) { * * @return the MIME entry associated with the file name or {@code null} */ - public MimeEntry findByFileName(String fname) { - MimeEntry entry = null; + private MimeEntry findByFileName(String fname) { // If an optional fragment introduced by a hash mark is // present, then strip it and use the prefix int hashIndex = fname.lastIndexOf(HASH_MARK); if (hashIndex > 0) { - entry = findViaFileExtension(fname.substring(0, hashIndex)); + MimeEntry entry = findByFileExtension(fname.substring(0, hashIndex)); if (entry != null) { return entry; } } - assert entry == null; - // If either no optional fragment was present, or the entry was not // found with the fragment stripped, then try again with the full name - if (entry == null) { - entry = findViaFileExtension(fname); - } - - return entry; - } - - /** - * Locate a MimeEntry by the file extension that has been associated - * with it. - */ - public synchronized MimeEntry findByExt(String fileExtension) { - return extensionMap.get(fileExtension); - } - - public synchronized MimeEntry findByDescription(String description) { - Enumeration e = elements(); - while (e.hasMoreElements()) { - MimeEntry entry = e.nextElement(); - if (description.equals(entry.getDescription())) { - return entry; - } - } - - // We failed, now try treating description as type - return find(description); - } + return findByFileExtension(fname); - String getTempFileTemplate() { - return tempFileTemplate; } - public synchronized Enumeration elements() { - return entries.elements(); - } - - // For backward compatibility -- mailcap format files - // This is not currently used, but may in the future when we add ability - // to read BOTH the properties format and the mailcap format. - protected static String[] mailcapLocations = - new String[]{ - System.getProperty("user.mailcap"), - StaticProperty.userHome() + "/.mailcap", - "/etc/mailcap", - "/usr/etc/mailcap", - "/usr/local/etc/mailcap" - }; - - public synchronized void load() { + private synchronized void load() { Properties entries = new Properties(); - File file = null; + File file; InputStream in; // First try to load the user-specific table, if it exists @@ -260,12 +160,11 @@ public synchronized void load() { parse(entries); } - void parse(Properties entries) { + private void parse(Properties entries) { // first, strip out the platform-specific temp file template String tempFileTemplate = (String)entries.get("temp.file.template"); if (tempFileTemplate != null) { entries.remove("temp.file.template"); - MimeTable.tempFileTemplate = tempFileTemplate; } // now, parse the mime-type spec's @@ -304,7 +203,7 @@ void parse(Properties entries) { // associated with. // - void parse(String type, String attrs) { + private void parse(String type, String attrs) { MimeEntry newEntry = new MimeEntry(type); // REMIND handle embedded ';' and '|' and literal '"' @@ -317,7 +216,7 @@ void parse(String type, String attrs) { add(newEntry); } - void parse(String pair, MimeEntry entry) { + private static void parse(String pair, MimeEntry entry) { // REMIND add exception handling... String name = null; String value = null; @@ -337,7 +236,7 @@ void parse(String pair, MimeEntry entry) { fill(entry, name, value); } - void fill(MimeEntry entry, String name, String value) { + private static void fill(MimeEntry entry, String name, String value) { if ("description".equalsIgnoreCase(name)) { entry.setDescription(value); } @@ -357,7 +256,7 @@ else if ("file_extensions".equalsIgnoreCase(name)) { // else illegal name exception } - int getActionCode(String action) { + private static int getActionCode(String action) { for (int i = 0; i < MimeEntry.actionKeywords.length; i++) { if (action.equalsIgnoreCase(MimeEntry.actionKeywords[i])) { return i; @@ -367,61 +266,4 @@ int getActionCode(String action) { return MimeEntry.UNKNOWN; } - public Properties getAsProperties() { - Properties properties = new Properties(); - Enumeration e = elements(); - while (e.hasMoreElements()) { - MimeEntry entry = e.nextElement(); - properties.put(entry.getType(), entry.toProperty()); - } - - return properties; - } - - protected boolean saveAsProperties(File file) { - try (FileOutputStream os = new FileOutputStream(file)) { - Properties properties = getAsProperties(); - properties.put("temp.file.template", tempFileTemplate); - String tag; - String user = StaticProperty.userName(); - if (user != null) { - tag = "; customized for " + user; - properties.store(os, filePreamble + tag); - } - else { - properties.store(os, filePreamble); - } - } - catch (IOException e) { - e.printStackTrace(); - return false; - } - - return true; - } - /* - * Debugging utilities - * - public void list(PrintStream out) { - Enumeration keys = entries.keys(); - while (keys.hasMoreElements()) { - String key = (String)keys.nextElement(); - MimeEntry entry = (MimeEntry)entries.get(key); - out.println(key + ": " + entry); - } - } - - public static void main(String[] args) { - MimeTable testTable = MimeTable.getDefaultTable(); - - Enumeration e = testTable.elements(); - while (e.hasMoreElements()) { - MimeEntry entry = (MimeEntry)e.nextElement(); - System.out.println(entry); - } - - testTable.save(File.separator + "tmp" + - File.separator + "mime_table.save"); - } - */ }