2525
2626package sun .net .www ;
2727
28- import jdk .internal .util .StaticProperty ;
29-
3028import java .io .File ;
3129import java .io .FileInputStream ;
3230import java .io .FileNotFoundException ;
33- import java .io .FileOutputStream ;
3431import java .io .InputStream ;
3532import java .io .IOException ;
3633import java .net .FileNameMap ;
4037import java .util .Properties ;
4138import java .util .StringTokenizer ;
4239
43- public class MimeTable implements FileNameMap {
40+ public final class MimeTable implements FileNameMap {
41+
4442 /** Hash mark introducing a URI fragment */
4543 private static final int HASH_MARK = '#' ;
4644
47- /** Keyed by content type, returns MimeEntries */
48- private final Hashtable <String , MimeEntry > entries = new Hashtable <>();
49-
5045 /** Keyed by file extension (with the .), returns MimeEntries */
5146 private final Hashtable <String , MimeEntry > extensionMap = new Hashtable <>();
5247
53- // Will be reset if in the platform-specific data file
54- private static String tempFileTemplate =
55- System .getProperty ("content.types.temp.file.template" , "/tmp/%s" );
56-
57- private static final String filePreamble = "sun.net.www MIME content-types table" ;
58-
59- MimeTable () {
48+ private MimeTable () {
6049 load ();
6150 }
6251
63- private static class DefaultInstanceHolder {
64- static final MimeTable defaultInstance = getDefaultInstance ();
52+ private static final class DefaultInstanceHolder {
53+ private static final MimeTable defaultInstance = getDefaultInstance ();
6554
66- static MimeTable getDefaultInstance () {
55+ private static MimeTable getDefaultInstance () {
6756 final MimeTable instance = new MimeTable ();
6857 URLConnection .setFileNameMap (instance );
6958 return instance ;
@@ -78,15 +67,6 @@ public static MimeTable getDefaultTable() {
7867 return DefaultInstanceHolder .defaultInstance ;
7968 }
8069
81- public static FileNameMap loadTable () {
82- MimeTable mt = getDefaultTable ();
83- return mt ;
84- }
85-
86- public synchronized int getSize () {
87- return entries .size ();
88- }
89-
9070 public synchronized String getContentTypeFor (String fileName ) {
9171 MimeEntry entry = findByFileName (fileName );
9272 if (entry != null ) {
@@ -96,55 +76,21 @@ public synchronized String getContentTypeFor(String fileName) {
9676 }
9777 }
9878
99- public synchronized void add (MimeEntry m ) {
100- entries .put (m .getType (), m );
101-
102- String exts [] = m .getExtensions ();
79+ private void add (MimeEntry m ) {
80+ String [] exts = m .getExtensions ();
10381 if (exts == null ) {
10482 return ;
10583 }
10684
107- for (int i = 0 ; i < exts . length ; i ++ ) {
108- extensionMap .put (exts [ i ] , m );
85+ for (String ext : exts ) {
86+ extensionMap .put (ext , m );
10987 }
11088 }
11189
112- public synchronized MimeEntry remove (String type ) {
113- MimeEntry entry = entries .get (type );
114- return remove (entry );
115- }
116-
117- public synchronized MimeEntry remove (MimeEntry entry ) {
118- String [] extensionKeys = entry .getExtensions ();
119- if (extensionKeys != null ) {
120- for (int i = 0 ; i < extensionKeys .length ; i ++) {
121- extensionMap .remove (extensionKeys [i ]);
122- }
123- }
124-
125- return entries .remove (entry .getType ());
126- }
127-
128- public synchronized MimeEntry find (String type ) {
129- MimeEntry entry = entries .get (type );
130- if (entry == null ) {
131- // try a wildcard lookup
132- Enumeration <MimeEntry > e = entries .elements ();
133- while (e .hasMoreElements ()) {
134- MimeEntry wild = e .nextElement ();
135- if (wild .matches (type )) {
136- return wild ;
137- }
138- }
139- }
140-
141- return entry ;
142- }
143-
14490 /**
14591 * Extracts the file extension and uses it to look up the entry.
14692 */
147- private MimeEntry findViaFileExtension (String fname ) {
93+ private MimeEntry findByFileExtension (String fname ) {
14894 int i = fname .lastIndexOf ('.' );
14995 // REMIND: OS specific delimiters appear here
15096 i = Math .max (i , fname .lastIndexOf ('/' ));
@@ -155,7 +101,8 @@ private MimeEntry findViaFileExtension(String fname) {
155101 ext = fname .substring (i ).toLowerCase (Locale .ROOT );
156102 }
157103
158- return findByExt (ext );
104+ return extensionMap .get (ext );
105+
159106 }
160107
161108 /**
@@ -166,74 +113,27 @@ private MimeEntry findViaFileExtension(String fname) {
166113 *
167114 * @return the MIME entry associated with the file name or {@code null}
168115 */
169- public MimeEntry findByFileName (String fname ) {
170- MimeEntry entry = null ;
116+ private MimeEntry findByFileName (String fname ) {
171117
172118 // If an optional fragment introduced by a hash mark is
173119 // present, then strip it and use the prefix
174120 int hashIndex = fname .lastIndexOf (HASH_MARK );
175121 if (hashIndex > 0 ) {
176- entry = findViaFileExtension (fname .substring (0 , hashIndex ));
122+ MimeEntry entry = findByFileExtension (fname .substring (0 , hashIndex ));
177123 if (entry != null ) {
178124 return entry ;
179125 }
180126 }
181127
182- assert entry == null ;
183-
184128 // If either no optional fragment was present, or the entry was not
185129 // found with the fragment stripped, then try again with the full name
186- if (entry == null ) {
187- entry = findViaFileExtension (fname );
188- }
189-
190- return entry ;
191- }
192-
193- /**
194- * Locate a MimeEntry by the file extension that has been associated
195- * with it.
196- */
197- public synchronized MimeEntry findByExt (String fileExtension ) {
198- return extensionMap .get (fileExtension );
199- }
200-
201- public synchronized MimeEntry findByDescription (String description ) {
202- Enumeration <MimeEntry > e = elements ();
203- while (e .hasMoreElements ()) {
204- MimeEntry entry = e .nextElement ();
205- if (description .equals (entry .getDescription ())) {
206- return entry ;
207- }
208- }
209-
210- // We failed, now try treating description as type
211- return find (description );
212- }
130+ return findByFileExtension (fname );
213131
214- String getTempFileTemplate () {
215- return tempFileTemplate ;
216132 }
217133
218- public synchronized Enumeration <MimeEntry > elements () {
219- return entries .elements ();
220- }
221-
222- // For backward compatibility -- mailcap format files
223- // This is not currently used, but may in the future when we add ability
224- // to read BOTH the properties format and the mailcap format.
225- protected static String [] mailcapLocations =
226- new String []{
227- System .getProperty ("user.mailcap" ),
228- StaticProperty .userHome () + "/.mailcap" ,
229- "/etc/mailcap" ,
230- "/usr/etc/mailcap" ,
231- "/usr/local/etc/mailcap"
232- };
233-
234- public synchronized void load () {
134+ private synchronized void load () {
235135 Properties entries = new Properties ();
236- File file = null ;
136+ File file ;
237137 InputStream in ;
238138
239139 // First try to load the user-specific table, if it exists
@@ -260,12 +160,11 @@ public synchronized void load() {
260160 parse (entries );
261161 }
262162
263- void parse (Properties entries ) {
163+ private void parse (Properties entries ) {
264164 // first, strip out the platform-specific temp file template
265165 String tempFileTemplate = (String )entries .get ("temp.file.template" );
266166 if (tempFileTemplate != null ) {
267167 entries .remove ("temp.file.template" );
268- MimeTable .tempFileTemplate = tempFileTemplate ;
269168 }
270169
271170 // now, parse the mime-type spec's
@@ -304,7 +203,7 @@ void parse(Properties entries) {
304203 // associated with.
305204 //
306205
307- void parse (String type , String attrs ) {
206+ private void parse (String type , String attrs ) {
308207 MimeEntry newEntry = new MimeEntry (type );
309208
310209 // REMIND handle embedded ';' and '|' and literal '"'
@@ -317,7 +216,7 @@ void parse(String type, String attrs) {
317216 add (newEntry );
318217 }
319218
320- void parse (String pair , MimeEntry entry ) {
219+ private static void parse (String pair , MimeEntry entry ) {
321220 // REMIND add exception handling...
322221 String name = null ;
323222 String value = null ;
@@ -337,7 +236,7 @@ void parse(String pair, MimeEntry entry) {
337236 fill (entry , name , value );
338237 }
339238
340- void fill (MimeEntry entry , String name , String value ) {
239+ private static void fill (MimeEntry entry , String name , String value ) {
341240 if ("description" .equalsIgnoreCase (name )) {
342241 entry .setDescription (value );
343242 }
@@ -357,7 +256,7 @@ else if ("file_extensions".equalsIgnoreCase(name)) {
357256 // else illegal name exception
358257 }
359258
360- int getActionCode (String action ) {
259+ private static int getActionCode (String action ) {
361260 for (int i = 0 ; i < MimeEntry .actionKeywords .length ; i ++) {
362261 if (action .equalsIgnoreCase (MimeEntry .actionKeywords [i ])) {
363262 return i ;
@@ -367,61 +266,4 @@ int getActionCode(String action) {
367266 return MimeEntry .UNKNOWN ;
368267 }
369268
370- public Properties getAsProperties () {
371- Properties properties = new Properties ();
372- Enumeration <MimeEntry > e = elements ();
373- while (e .hasMoreElements ()) {
374- MimeEntry entry = e .nextElement ();
375- properties .put (entry .getType (), entry .toProperty ());
376- }
377-
378- return properties ;
379- }
380-
381- protected boolean saveAsProperties (File file ) {
382- try (FileOutputStream os = new FileOutputStream (file )) {
383- Properties properties = getAsProperties ();
384- properties .put ("temp.file.template" , tempFileTemplate );
385- String tag ;
386- String user = StaticProperty .userName ();
387- if (user != null ) {
388- tag = "; customized for " + user ;
389- properties .store (os , filePreamble + tag );
390- }
391- else {
392- properties .store (os , filePreamble );
393- }
394- }
395- catch (IOException e ) {
396- e .printStackTrace ();
397- return false ;
398- }
399-
400- return true ;
401- }
402- /*
403- * Debugging utilities
404- *
405- public void list(PrintStream out) {
406- Enumeration keys = entries.keys();
407- while (keys.hasMoreElements()) {
408- String key = (String)keys.nextElement();
409- MimeEntry entry = (MimeEntry)entries.get(key);
410- out.println(key + ": " + entry);
411- }
412- }
413-
414- public static void main(String[] args) {
415- MimeTable testTable = MimeTable.getDefaultTable();
416-
417- Enumeration e = testTable.elements();
418- while (e.hasMoreElements()) {
419- MimeEntry entry = (MimeEntry)e.nextElement();
420- System.out.println(entry);
421- }
422-
423- testTable.save(File.separator + "tmp" +
424- File.separator + "mime_table.save");
425- }
426- */
427269}
0 commit comments