8336412: sun.net.www.MimeTable has a few unused methods

Reviewed-by: jpai
This commit is contained in:
Volkan Yazıcı 2025-01-02 09:19:58 +00:00 committed by Jaikiran Pai
parent e769b53614
commit a77ed30fcc
2 changed files with 24 additions and 182 deletions

View file

@ -306,7 +306,7 @@ public abstract class URLConnection {
if (map == null) { if (map == null) {
fileNameMap = map = new FileNameMap() { fileNameMap = map = new FileNameMap() {
private final FileNameMap internalMap = private final FileNameMap internalMap =
sun.net.www.MimeTable.loadTable(); sun.net.www.MimeTable.getDefaultTable();
public String getContentTypeFor(String fileName) { public String getContentTypeFor(String fileName) {
return internalMap.getContentTypeFor(fileName); return internalMap.getContentTypeFor(fileName);

View file

@ -25,12 +25,9 @@
package sun.net.www; package sun.net.www;
import jdk.internal.util.StaticProperty;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.net.FileNameMap; import java.net.FileNameMap;
@ -40,30 +37,22 @@ import java.util.Hashtable;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
public class MimeTable implements FileNameMap { public final class MimeTable implements FileNameMap {
/** Hash mark introducing a URI fragment */ /** Hash mark introducing a URI fragment */
private static final int HASH_MARK = '#'; private static final int HASH_MARK = '#';
/** Keyed by content type, returns MimeEntries */
private final Hashtable<String, MimeEntry> entries = new Hashtable<>();
/** Keyed by file extension (with the .), returns MimeEntries */ /** Keyed by file extension (with the .), returns MimeEntries */
private final Hashtable<String, MimeEntry> extensionMap = new Hashtable<>(); private final Hashtable<String, MimeEntry> extensionMap = new Hashtable<>();
// Will be reset if in the platform-specific data file private MimeTable() {
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() {
load(); load();
} }
private static class DefaultInstanceHolder { private static final class DefaultInstanceHolder {
static final MimeTable defaultInstance = getDefaultInstance(); private static final MimeTable defaultInstance = getDefaultInstance();
static MimeTable getDefaultInstance() { private static MimeTable getDefaultInstance() {
final MimeTable instance = new MimeTable(); final MimeTable instance = new MimeTable();
URLConnection.setFileNameMap(instance); URLConnection.setFileNameMap(instance);
return instance; return instance;
@ -78,15 +67,6 @@ public class MimeTable implements FileNameMap {
return DefaultInstanceHolder.defaultInstance; 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) { public synchronized String getContentTypeFor(String fileName) {
MimeEntry entry = findByFileName(fileName); MimeEntry entry = findByFileName(fileName);
if (entry != null) { if (entry != null) {
@ -96,55 +76,21 @@ public class MimeTable implements FileNameMap {
} }
} }
public synchronized void add(MimeEntry m) { private void add(MimeEntry m) {
entries.put(m.getType(), m); String[] exts = m.getExtensions();
String exts[] = m.getExtensions();
if (exts == null) { if (exts == null) {
return; return;
} }
for (int i = 0; i < exts.length; i++) { for (String ext : exts) {
extensionMap.put(exts[i], m); 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<MimeEntry> 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. * 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('.'); int i = fname.lastIndexOf('.');
// REMIND: OS specific delimiters appear here // REMIND: OS specific delimiters appear here
i = Math.max(i, fname.lastIndexOf('/')); i = Math.max(i, fname.lastIndexOf('/'));
@ -155,7 +101,8 @@ public class MimeTable implements FileNameMap {
ext = fname.substring(i).toLowerCase(Locale.ROOT); ext = fname.substring(i).toLowerCase(Locale.ROOT);
} }
return findByExt(ext); return extensionMap.get(ext);
} }
/** /**
@ -166,74 +113,27 @@ public class MimeTable implements FileNameMap {
* *
* @return the MIME entry associated with the file name or {@code null} * @return the MIME entry associated with the file name or {@code null}
*/ */
public MimeEntry findByFileName(String fname) { private MimeEntry findByFileName(String fname) {
MimeEntry entry = null;
// If an optional fragment introduced by a hash mark is // If an optional fragment introduced by a hash mark is
// present, then strip it and use the prefix // present, then strip it and use the prefix
int hashIndex = fname.lastIndexOf(HASH_MARK); int hashIndex = fname.lastIndexOf(HASH_MARK);
if (hashIndex > 0) { if (hashIndex > 0) {
entry = findViaFileExtension(fname.substring(0, hashIndex)); MimeEntry entry = findByFileExtension(fname.substring(0, hashIndex));
if (entry != null) { if (entry != null) {
return entry; return entry;
} }
} }
assert entry == null;
// If either no optional fragment was present, or the entry was not // If either no optional fragment was present, or the entry was not
// found with the fragment stripped, then try again with the full name // found with the fragment stripped, then try again with the full name
if (entry == null) { return findByFileExtension(fname);
entry = findViaFileExtension(fname);
} }
return entry; private synchronized void load() {
}
/**
* 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<MimeEntry> 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);
}
String getTempFileTemplate() {
return tempFileTemplate;
}
public synchronized Enumeration<MimeEntry> 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() {
Properties entries = new Properties(); Properties entries = new Properties();
File file = null; File file;
InputStream in; InputStream in;
// First try to load the user-specific table, if it exists // First try to load the user-specific table, if it exists
@ -260,12 +160,11 @@ public class MimeTable implements FileNameMap {
parse(entries); parse(entries);
} }
void parse(Properties entries) { private void parse(Properties entries) {
// first, strip out the platform-specific temp file template // first, strip out the platform-specific temp file template
String tempFileTemplate = (String)entries.get("temp.file.template"); String tempFileTemplate = (String)entries.get("temp.file.template");
if (tempFileTemplate != null) { if (tempFileTemplate != null) {
entries.remove("temp.file.template"); entries.remove("temp.file.template");
MimeTable.tempFileTemplate = tempFileTemplate;
} }
// now, parse the mime-type spec's // now, parse the mime-type spec's
@ -304,7 +203,7 @@ public class MimeTable implements FileNameMap {
// associated with. // associated with.
// //
void parse(String type, String attrs) { private void parse(String type, String attrs) {
MimeEntry newEntry = new MimeEntry(type); MimeEntry newEntry = new MimeEntry(type);
// REMIND handle embedded ';' and '|' and literal '"' // REMIND handle embedded ';' and '|' and literal '"'
@ -317,7 +216,7 @@ public class MimeTable implements FileNameMap {
add(newEntry); add(newEntry);
} }
void parse(String pair, MimeEntry entry) { private static void parse(String pair, MimeEntry entry) {
// REMIND add exception handling... // REMIND add exception handling...
String name = null; String name = null;
String value = null; String value = null;
@ -337,7 +236,7 @@ public class MimeTable implements FileNameMap {
fill(entry, name, value); 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)) { if ("description".equalsIgnoreCase(name)) {
entry.setDescription(value); entry.setDescription(value);
} }
@ -357,7 +256,7 @@ public class MimeTable implements FileNameMap {
// else illegal name exception // else illegal name exception
} }
int getActionCode(String action) { private static int getActionCode(String action) {
for (int i = 0; i < MimeEntry.actionKeywords.length; i++) { for (int i = 0; i < MimeEntry.actionKeywords.length; i++) {
if (action.equalsIgnoreCase(MimeEntry.actionKeywords[i])) { if (action.equalsIgnoreCase(MimeEntry.actionKeywords[i])) {
return i; return i;
@ -367,61 +266,4 @@ public class MimeTable implements FileNameMap {
return MimeEntry.UNKNOWN; return MimeEntry.UNKNOWN;
} }
public Properties getAsProperties() {
Properties properties = new Properties();
Enumeration<MimeEntry> 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");
}
*/
} }