|
27 | 27 |
|
28 | 28 | import jdk.internal.util.StaticProperty; |
29 | 29 |
|
30 | | -import java.io.*; |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileInputStream; |
| 32 | +import java.io.FileNotFoundException; |
| 33 | +import java.io.FileOutputStream; |
| 34 | +import java.io.InputStream; |
| 35 | +import java.io.IOException; |
31 | 36 | import java.net.FileNameMap; |
32 | | -import java.util.Hashtable; |
33 | 37 | import java.util.Enumeration; |
| 38 | +import java.util.Hashtable; |
34 | 39 | import java.util.Properties; |
35 | 40 | import java.util.StringTokenizer; |
36 | 41 |
|
37 | 42 | public class MimeTable implements FileNameMap { |
| 43 | + /** Hash mark introducing a URI fragment */ |
| 44 | + private static final int HASH_MARK = '#'; |
| 45 | + |
38 | 46 | /** Keyed by content type, returns MimeEntries */ |
39 | | - private Hashtable<String, MimeEntry> entries |
40 | | - = new Hashtable<>(); |
| 47 | + private Hashtable<String, MimeEntry> entries = new Hashtable<>(); |
41 | 48 |
|
42 | 49 | /** Keyed by file extension (with the .), returns MimeEntries */ |
43 | | - private Hashtable<String, MimeEntry> extensionMap |
44 | | - = new Hashtable<>(); |
| 50 | + private Hashtable<String, MimeEntry> extensionMap = new Hashtable<>(); |
45 | 51 |
|
46 | 52 | // Will be reset if in the platform-specific data file |
47 | 53 | @SuppressWarnings("removal") |
@@ -84,9 +90,6 @@ public static MimeTable getDefaultTable() { |
84 | 90 | return DefaultInstanceHolder.defaultInstance; |
85 | 91 | } |
86 | 92 |
|
87 | | - /** |
88 | | - * |
89 | | - */ |
90 | 93 | public static FileNameMap loadTable() { |
91 | 94 | MimeTable mt = getDefaultTable(); |
92 | 95 | return mt; |
@@ -151,30 +154,54 @@ public synchronized MimeEntry find(String type) { |
151 | 154 | } |
152 | 155 |
|
153 | 156 | /** |
154 | | - * Locate a MimeEntry by the file extension that has been associated |
155 | | - * with it. Parses general file names, and URLs. |
| 157 | + * Extracts the file extension and uses it to look up the entry. |
156 | 158 | */ |
157 | | - public MimeEntry findByFileName(String fname) { |
158 | | - String ext = ""; |
159 | | - |
160 | | - int i = fname.lastIndexOf('#'); |
161 | | - |
162 | | - if (i > 0) { |
163 | | - fname = fname.substring(0, i - 1); |
164 | | - } |
165 | | - |
166 | | - i = fname.lastIndexOf('.'); |
| 159 | + private MimeEntry findViaFileExtension(String fname) { |
| 160 | + int i = fname.lastIndexOf('.'); |
167 | 161 | // REMIND: OS specific delimiters appear here |
168 | 162 | i = Math.max(i, fname.lastIndexOf('/')); |
169 | 163 | i = Math.max(i, fname.lastIndexOf('?')); |
170 | 164 |
|
| 165 | + String ext = ""; |
171 | 166 | if (i != -1 && fname.charAt(i) == '.') { |
172 | 167 | ext = fname.substring(i).toLowerCase(); |
173 | 168 | } |
174 | 169 |
|
175 | 170 | return findByExt(ext); |
176 | 171 | } |
177 | 172 |
|
| 173 | + /** |
| 174 | + * Locate a MimeEntry by its associated file extension. |
| 175 | + * Parses general file names, and URLs. |
| 176 | + * |
| 177 | + * @param fname the file name |
| 178 | + * |
| 179 | + * @return the MIME entry associated with the file name or {@code null} |
| 180 | + */ |
| 181 | + public MimeEntry findByFileName(String fname) { |
| 182 | + MimeEntry entry = null; |
| 183 | + |
| 184 | + // If an optional fragment introduced by a hash mark is |
| 185 | + // present, then strip it and use the prefix |
| 186 | + int hashIndex = fname.lastIndexOf(HASH_MARK); |
| 187 | + if (hashIndex > 0) { |
| 188 | + entry = findViaFileExtension(fname.substring(0, hashIndex)); |
| 189 | + if (entry != null) { |
| 190 | + return entry; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + assert entry == null; |
| 195 | + |
| 196 | + // If either no optional fragment was present, or the entry was not |
| 197 | + // found with the fragment stripped, then try again with the full name |
| 198 | + if (entry == null) { |
| 199 | + entry = findViaFileExtension(fname); |
| 200 | + } |
| 201 | + |
| 202 | + return entry; |
| 203 | + } |
| 204 | + |
178 | 205 | /** |
179 | 206 | * Locate a MimeEntry by the file extension that has been associated |
180 | 207 | * with it. |
|
0 commit comments