Skip to content

Commit

Permalink
8336412: sun.net.www.MimeTable has a few unused methods
Browse files Browse the repository at this point in the history
Reviewed-by: jpai
  • Loading branch information
vy authored and jaikiran committed Jan 2, 2025
1 parent e769b53 commit a77ed30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 182 deletions.
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/URLConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
204 changes: 23 additions & 181 deletions src/java.base/share/classes/sun/net/www/MimeTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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;
Expand All @@ -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) {
Expand All @@ -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<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('/'));
Expand All @@ -155,7 +101,8 @@ private MimeEntry findViaFileExtension(String fname) {
ext = fname.substring(i).toLowerCase(Locale.ROOT);
}

return findByExt(ext);
return extensionMap.get(ext);

}

/**
Expand All @@ -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<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);
}
return findByFileExtension(fname);

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
Expand All @@ -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
Expand Down Expand Up @@ -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 '"'
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -367,61 +266,4 @@ int getActionCode(String action) {
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");
}
*/
}

1 comment on commit a77ed30

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.