mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 06:14:49 +02:00
8336412: sun.net.www.MimeTable has a few unused methods
Reviewed-by: jpai
This commit is contained in:
parent
e769b53614
commit
a77ed30fcc
2 changed files with 24 additions and 182 deletions
|
@ -306,7 +306,7 @@ public abstract class URLConnection {
|
|||
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);
|
||||
|
|
|
@ -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.Hashtable;
|
|||
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<String, MimeEntry> entries = new Hashtable<>();
|
||||
|
||||
/** Keyed by file extension (with the .), returns MimeEntries */
|
||||
private final Hashtable<String, MimeEntry> 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 class MimeTable implements FileNameMap {
|
|||
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 class MimeTable implements FileNameMap {
|
|||
}
|
||||
}
|
||||
|
||||
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<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.
|
||||
*/
|
||||
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 @@ public class MimeTable implements FileNameMap {
|
|||
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}
|
||||
*/
|
||||
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 findByFileExtension(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<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() {
|
||||
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 class MimeTable implements FileNameMap {
|
|||
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 @@ public class MimeTable implements FileNameMap {
|
|||
// 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 @@ public class MimeTable implements FileNameMap {
|
|||
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 @@ public class MimeTable implements FileNameMap {
|
|||
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 @@ public class MimeTable implements FileNameMap {
|
|||
// 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 @@ public class MimeTable implements FileNameMap {
|
|||
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");
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue