1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.net; 18 19 import java.util.HashMap; 20 import java.util.Map; 21 22 /** 23 * Utilities for dealing with MIME types. 24 * Used to implement java.net.URLConnection and android.webkit.MimeTypeMap. 25 */ 26 public final class MimeUtils { 27 private static final Map<String, String> mimeTypeToExtensionMap = new HashMap<String, String>(); 28 29 private static final Map<String, String> extensionToMimeTypeMap = new HashMap<String, String>(); 30 31 static { 32 // The following table is based on /etc/mime.types data minus 33 // chemical/* MIME types and MIME types that don't map to any 34 // file extensions. We also exclude top-level domain names to 35 // deal with cases like: 36 // 37 // mail.google.com/a/google.com 38 // 39 // and "active" MIME types (due to potential security issues). 40 41 // Note that this list is _not_ in alphabetical order and must not be sorted. 42 // The "most popular" extension must come first, so that it's the one returned 43 // by guessExtensionFromMimeType. 44 45 add("application/andrew-inset", "ez"); 46 add("application/dsptype", "tsp"); 47 add("application/epub+zip", "epub"); 48 add("application/hta", "hta"); 49 add("application/mac-binhex40", "hqx"); 50 add("application/mathematica", "nb"); 51 add("application/msaccess", "mdb"); 52 add("application/oda", "oda"); 53 add("application/ogg", "ogg"); 54 add("application/ogg", "oga"); 55 add("application/pdf", "pdf"); 56 add("application/pgp-keys", "key"); 57 add("application/pgp-signature", "pgp"); 58 add("application/pics-rules", "prf"); 59 add("application/pkix-cert", "cer"); 60 add("application/rar", "rar"); 61 add("application/rdf+xml", "rdf"); 62 add("application/rss+xml", "rss"); 63 add("application/zip", "zip"); 64 add("application/vnd.android.package-archive", "apk"); 65 add("application/vnd.cinderella", "cdy"); 66 add("application/vnd.ms-pki.stl", "stl"); 67 add("application/vnd.oasis.opendocument.database", "odb"); 68 add("application/vnd.oasis.opendocument.formula", "odf"); 69 add("application/vnd.oasis.opendocument.graphics", "odg"); 70 add("application/vnd.oasis.opendocument.graphics-template", "otg"); 71 add("application/vnd.oasis.opendocument.image", "odi"); 72 add("application/vnd.oasis.opendocument.presentation", "odp"); 73 add("application/vnd.oasis.opendocument.presentation-template", "otp"); 74 add("application/vnd.oasis.opendocument.spreadsheet", "ods"); 75 add("application/vnd.oasis.opendocument.spreadsheet-template", "ots"); 76 add("application/vnd.oasis.opendocument.text", "odt"); 77 add("application/vnd.oasis.opendocument.text-master", "odm"); 78 add("application/vnd.oasis.opendocument.text-template", "ott"); 79 add("application/vnd.oasis.opendocument.text-web", "oth"); 80 add("application/vnd.google-earth.kml+xml", "kml"); 81 add("application/vnd.google-earth.kmz", "kmz"); 82 add("application/msword", "doc"); 83 add("application/msword", "dot"); 84 add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx"); 85 add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", "dotx"); 86 add("application/vnd.ms-excel", "xls"); 87 add("application/vnd.ms-excel", "xlt"); 88 add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"); 89 add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", "xltx"); 90 add("application/vnd.ms-powerpoint", "ppt"); 91 add("application/vnd.ms-powerpoint", "pot"); 92 add("application/vnd.ms-powerpoint", "pps"); 93 add("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"); 94 add("application/vnd.openxmlformats-officedocument.presentationml.template", "potx"); 95 add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", "ppsx"); 96 add("application/vnd.rim.cod", "cod"); 97 add("application/vnd.smaf", "mmf"); 98 add("application/vnd.stardivision.calc", "sdc"); 99 add("application/vnd.stardivision.draw", "sda"); 100 add("application/vnd.stardivision.impress", "sdd"); 101 add("application/vnd.stardivision.impress", "sdp"); 102 add("application/vnd.stardivision.math", "smf"); 103 add("application/vnd.stardivision.writer", "sdw"); 104 add("application/vnd.stardivision.writer", "vor"); 105 add("application/vnd.stardivision.writer-global", "sgl"); 106 add("application/vnd.sun.xml.calc", "sxc"); 107 add("application/vnd.sun.xml.calc.template", "stc"); 108 add("application/vnd.sun.xml.draw", "sxd"); 109 add("application/vnd.sun.xml.draw.template", "std"); 110 add("application/vnd.sun.xml.impress", "sxi"); 111 add("application/vnd.sun.xml.impress.template", "sti"); 112 add("application/vnd.sun.xml.math", "sxm"); 113 add("application/vnd.sun.xml.writer", "sxw"); 114 add("application/vnd.sun.xml.writer.global", "sxg"); 115 add("application/vnd.sun.xml.writer.template", "stw"); 116 add("application/vnd.visio", "vsd"); 117 add("application/x-abiword", "abw"); 118 add("application/x-apple-diskimage", "dmg"); 119 add("application/x-bcpio", "bcpio"); 120 add("application/x-bittorrent", "torrent"); 121 add("application/x-cdf", "cdf"); 122 add("application/x-cdlink", "vcd"); 123 add("application/x-chess-pgn", "pgn"); 124 add("application/x-cpio", "cpio"); 125 add("application/x-debian-package", "deb"); 126 add("application/x-debian-package", "udeb"); 127 add("application/x-director", "dcr"); 128 add("application/x-director", "dir"); 129 add("application/x-director", "dxr"); 130 add("application/x-dms", "dms"); 131 add("application/x-doom", "wad"); 132 add("application/x-dvi", "dvi"); 133 add("application/x-font", "pfa"); 134 add("application/x-font", "pfb"); 135 add("application/x-font", "gsf"); 136 add("application/x-font", "pcf"); 137 add("application/x-font", "pcf.Z"); 138 add("application/x-freemind", "mm"); 139 // application/futuresplash isn't IANA, so application/x-futuresplash should come first. 140 add("application/x-futuresplash", "spl"); 141 add("application/futuresplash", "spl"); 142 add("application/x-gnumeric", "gnumeric"); 143 add("application/x-go-sgf", "sgf"); 144 add("application/x-graphing-calculator", "gcf"); 145 add("application/x-gtar", "tgz"); 146 add("application/x-gtar", "gtar"); 147 add("application/x-gtar", "taz"); 148 add("application/x-hdf", "hdf"); 149 add("application/x-hwp", "hwp"); // http://b/18788282. 150 add("application/x-ica", "ica"); 151 add("application/x-internet-signup", "ins"); 152 add("application/x-internet-signup", "isp"); 153 add("application/x-iphone", "iii"); 154 add("application/x-iso9660-image", "iso"); 155 add("application/x-jmol", "jmz"); 156 add("application/x-kchart", "chrt"); 157 add("application/x-killustrator", "kil"); 158 add("application/x-koan", "skp"); 159 add("application/x-koan", "skd"); 160 add("application/x-koan", "skt"); 161 add("application/x-koan", "skm"); 162 add("application/x-kpresenter", "kpr"); 163 add("application/x-kpresenter", "kpt"); 164 add("application/x-kspread", "ksp"); 165 add("application/x-kword", "kwd"); 166 add("application/x-kword", "kwt"); 167 add("application/x-latex", "latex"); 168 add("application/x-lha", "lha"); 169 add("application/x-lzh", "lzh"); 170 add("application/x-lzx", "lzx"); 171 add("application/x-maker", "frm"); 172 add("application/x-maker", "maker"); 173 add("application/x-maker", "frame"); 174 add("application/x-maker", "fb"); 175 add("application/x-maker", "book"); 176 add("application/x-maker", "fbdoc"); 177 add("application/x-mif", "mif"); 178 add("application/x-ms-wmd", "wmd"); 179 add("application/x-ms-wmz", "wmz"); 180 add("application/x-msi", "msi"); 181 add("application/x-ns-proxy-autoconfig", "pac"); 182 add("application/x-nwc", "nwc"); 183 add("application/x-object", "o"); 184 add("application/x-oz-application", "oza"); 185 add("application/x-pem-file", "pem"); 186 add("application/x-pkcs12", "p12"); 187 add("application/x-pkcs12", "pfx"); 188 add("application/x-pkcs7-certreqresp", "p7r"); 189 add("application/x-pkcs7-crl", "crl"); 190 add("application/x-quicktimeplayer", "qtl"); 191 add("application/x-shar", "shar"); 192 add("application/x-shockwave-flash", "swf"); 193 add("application/x-stuffit", "sit"); 194 add("application/x-sv4cpio", "sv4cpio"); 195 add("application/x-sv4crc", "sv4crc"); 196 add("application/x-tar", "tar"); 197 add("application/x-texinfo", "texinfo"); 198 add("application/x-texinfo", "texi"); 199 add("application/x-troff", "t"); 200 add("application/x-troff", "roff"); 201 add("application/x-troff-man", "man"); 202 add("application/x-ustar", "ustar"); 203 add("application/x-wais-source", "src"); 204 add("application/x-wingz", "wz"); 205 add("application/x-webarchive", "webarchive"); 206 add("application/x-webarchive-xml", "webarchivexml"); 207 add("application/x-x509-ca-cert", "crt"); 208 add("application/x-x509-user-cert", "crt"); 209 add("application/x-x509-server-cert", "crt"); 210 add("application/x-xcf", "xcf"); 211 add("application/x-xfig", "fig"); 212 add("application/xhtml+xml", "xhtml"); 213 add("audio/3gpp", "3gpp"); 214 add("audio/aac", "aac"); 215 add("audio/aac-adts", "aac"); 216 add("audio/amr", "amr"); 217 add("audio/amr-wb", "awb"); 218 add("audio/basic", "snd"); 219 add("audio/flac", "flac"); 220 add("application/x-flac", "flac"); 221 add("audio/imelody", "imy"); 222 add("audio/midi", "mid"); 223 add("audio/midi", "midi"); 224 add("audio/midi", "ota"); 225 add("audio/midi", "kar"); 226 add("audio/midi", "rtttl"); 227 add("audio/midi", "xmf"); 228 add("audio/mobile-xmf", "mxmf"); 229 // add ".mp3" first so it will be the default for guessExtensionFromMimeType 230 add("audio/mpeg", "mp3"); 231 add("audio/mpeg", "mpga"); 232 add("audio/mpeg", "mpega"); 233 add("audio/mpeg", "mp2"); 234 add("audio/mpeg", "m4a"); 235 add("audio/mpegurl", "m3u"); 236 add("audio/prs.sid", "sid"); 237 add("audio/x-aiff", "aif"); 238 add("audio/x-aiff", "aiff"); 239 add("audio/x-aiff", "aifc"); 240 add("audio/x-gsm", "gsm"); 241 add("audio/x-matroska", "mka"); 242 add("audio/x-mpegurl", "m3u"); 243 add("audio/x-ms-wma", "wma"); 244 add("audio/x-ms-wax", "wax"); 245 add("audio/x-pn-realaudio", "ra"); 246 add("audio/x-pn-realaudio", "rm"); 247 add("audio/x-pn-realaudio", "ram"); 248 add("audio/x-realaudio", "ra"); 249 add("audio/x-scpls", "pls"); 250 add("audio/x-sd2", "sd2"); 251 add("audio/x-wav", "wav"); 252 // image/bmp isn't IANA, so image/x-ms-bmp should come first. 253 add("image/x-ms-bmp", "bmp"); 254 add("image/bmp", "bmp"); 255 add("image/gif", "gif"); 256 // image/ico isn't IANA, so image/x-icon should come first. 257 add("image/x-icon", "ico"); 258 add("image/ico", "cur"); 259 add("image/ico", "ico"); 260 add("image/ief", "ief"); 261 // add ".jpg" first so it will be the default for guessExtensionFromMimeType 262 add("image/jpeg", "jpg"); 263 add("image/jpeg", "jpeg"); 264 add("image/jpeg", "jpe"); 265 add("image/pcx", "pcx"); 266 add("image/png", "png"); 267 add("image/svg+xml", "svg"); 268 add("image/svg+xml", "svgz"); 269 add("image/tiff", "tiff"); 270 add("image/tiff", "tif"); 271 add("image/vnd.djvu", "djvu"); 272 add("image/vnd.djvu", "djv"); 273 add("image/vnd.wap.wbmp", "wbmp"); 274 add("image/webp", "webp"); 275 add("image/x-adobe-dng", "dng"); 276 add("image/x-canon-cr2", "cr2"); 277 add("image/x-cmu-raster", "ras"); 278 add("image/x-coreldraw", "cdr"); 279 add("image/x-coreldrawpattern", "pat"); 280 add("image/x-coreldrawtemplate", "cdt"); 281 add("image/x-corelphotopaint", "cpt"); 282 add("image/x-fuji-raf", "raf"); 283 add("image/x-jg", "art"); 284 add("image/x-jng", "jng"); 285 add("image/x-nikon-nef", "nef"); 286 add("image/x-nikon-nrw", "nrw"); 287 add("image/x-olympus-orf", "orf"); 288 add("image/x-panasonic-rw2", "rw2"); 289 add("image/x-pentax-pef", "pef"); 290 add("image/x-photoshop", "psd"); 291 add("image/x-portable-anymap", "pnm"); 292 add("image/x-portable-bitmap", "pbm"); 293 add("image/x-portable-graymap", "pgm"); 294 add("image/x-portable-pixmap", "ppm"); 295 add("image/x-samsung-srw", "srw"); 296 add("image/x-sony-arw", "arw"); 297 add("image/x-rgb", "rgb"); 298 add("image/x-xbitmap", "xbm"); 299 add("image/x-xpixmap", "xpm"); 300 add("image/x-xwindowdump", "xwd"); 301 add("model/iges", "igs"); 302 add("model/iges", "iges"); 303 add("model/mesh", "msh"); 304 add("model/mesh", "mesh"); 305 add("model/mesh", "silo"); 306 add("text/calendar", "ics"); 307 add("text/calendar", "icz"); 308 add("text/comma-separated-values", "csv"); 309 add("text/css", "css"); 310 add("text/html", "htm"); 311 add("text/html", "html"); 312 add("text/h323", "323"); 313 add("text/iuls", "uls"); 314 add("text/mathml", "mml"); 315 // add ".txt" first so it will be the default for guessExtensionFromMimeType 316 add("text/plain", "txt"); 317 add("text/plain", "asc"); 318 add("text/plain", "text"); 319 add("text/plain", "diff"); 320 add("text/plain", "po"); // reserve "pot" for vnd.ms-powerpoint 321 add("text/richtext", "rtx"); 322 add("text/rtf", "rtf"); 323 add("text/text", "phps"); 324 add("text/tab-separated-values", "tsv"); 325 add("text/xml", "xml"); 326 add("text/x-bibtex", "bib"); 327 add("text/x-boo", "boo"); 328 add("text/x-c++hdr", "hpp"); 329 add("text/x-c++hdr", "h++"); 330 add("text/x-c++hdr", "hxx"); 331 add("text/x-c++hdr", "hh"); 332 add("text/x-c++src", "cpp"); 333 add("text/x-c++src", "c++"); 334 add("text/x-c++src", "cc"); 335 add("text/x-c++src", "cxx"); 336 add("text/x-chdr", "h"); 337 add("text/x-component", "htc"); 338 add("text/x-csh", "csh"); 339 add("text/x-csrc", "c"); 340 add("text/x-dsrc", "d"); 341 add("text/x-haskell", "hs"); 342 add("text/x-java", "java"); 343 add("text/x-literate-haskell", "lhs"); 344 add("text/x-moc", "moc"); 345 add("text/x-pascal", "p"); 346 add("text/x-pascal", "pas"); 347 add("text/x-pcs-gcd", "gcd"); 348 add("text/x-setext", "etx"); 349 add("text/x-tcl", "tcl"); 350 add("text/x-tex", "tex"); 351 add("text/x-tex", "ltx"); 352 add("text/x-tex", "sty"); 353 add("text/x-tex", "cls"); 354 add("text/x-vcalendar", "vcs"); 355 add("text/x-vcard", "vcf"); 356 add("video/3gpp", "3gpp"); 357 add("video/3gpp", "3gp"); 358 add("video/3gpp2", "3gpp2"); 359 add("video/3gpp2", "3g2"); 360 add("video/avi", "avi"); 361 add("video/dl", "dl"); 362 add("video/dv", "dif"); 363 add("video/dv", "dv"); 364 add("video/fli", "fli"); 365 add("video/m4v", "m4v"); 366 add("video/mp2ts", "ts"); 367 add("video/mpeg", "mpeg"); 368 add("video/mpeg", "mpg"); 369 add("video/mpeg", "mpe"); 370 add("video/mp4", "mp4"); 371 add("video/mpeg", "VOB"); 372 add("video/quicktime", "qt"); 373 add("video/quicktime", "mov"); 374 add("video/vnd.mpegurl", "mxu"); 375 add("video/webm", "webm"); 376 add("video/x-la-asf", "lsf"); 377 add("video/x-la-asf", "lsx"); 378 add("video/x-matroska", "mkv"); 379 add("video/x-mng", "mng"); 380 add("video/x-ms-asf", "asf"); 381 add("video/x-ms-asf", "asx"); 382 add("video/x-ms-wm", "wm"); 383 add("video/x-ms-wmv", "wmv"); 384 add("video/x-ms-wmx", "wmx"); 385 add("video/x-ms-wvx", "wvx"); 386 add("video/x-sgi-movie", "movie"); 387 add("video/x-webex", "wrf"); 388 add("x-conference/x-cooltalk", "ice"); 389 add("x-epoc/x-sisx-app", "sisx"); 390 } 391 add(String mimeType, String extension)392 private static void add(String mimeType, String extension) { 393 // If we have an existing x -> y mapping, we do not want to 394 // override it with another mapping x -> y2. 395 // If a mime type maps to several extensions 396 // the first extension added is considered the most popular 397 // so we do not want to overwrite it later. 398 if (!mimeTypeToExtensionMap.containsKey(mimeType)) { 399 mimeTypeToExtensionMap.put(mimeType, extension); 400 } 401 if (!extensionToMimeTypeMap.containsKey(extension)) { 402 extensionToMimeTypeMap.put(extension, mimeType); 403 } 404 } 405 MimeUtils()406 private MimeUtils() { 407 } 408 409 /** 410 * Returns true if the given MIME type has an entry in the map. 411 * @param mimeType A MIME type (i.e. text/plain) 412 * @return True iff there is a mimeType entry in the map. 413 */ hasMimeType(String mimeType)414 public static boolean hasMimeType(String mimeType) { 415 if (mimeType == null || mimeType.isEmpty()) { 416 return false; 417 } 418 return mimeTypeToExtensionMap.containsKey(mimeType); 419 } 420 421 /** 422 * Returns the MIME type for the given extension. 423 * @param extension A file extension without the leading '.' 424 * @return The MIME type for the given extension or null iff there is none. 425 */ guessMimeTypeFromExtension(String extension)426 public static String guessMimeTypeFromExtension(String extension) { 427 if (extension == null || extension.isEmpty()) { 428 return null; 429 } 430 return extensionToMimeTypeMap.get(extension); 431 } 432 433 /** 434 * Returns true if the given extension has a registered MIME type. 435 * @param extension A file extension without the leading '.' 436 * @return True iff there is an extension entry in the map. 437 */ hasExtension(String extension)438 public static boolean hasExtension(String extension) { 439 if (extension == null || extension.isEmpty()) { 440 return false; 441 } 442 return extensionToMimeTypeMap.containsKey(extension); 443 } 444 445 /** 446 * Returns the registered extension for the given MIME type. Note that some 447 * MIME types map to multiple extensions. This call will return the most 448 * common extension for the given MIME type. 449 * @param mimeType A MIME type (i.e. text/plain) 450 * @return The extension for the given MIME type or null iff there is none. 451 */ guessExtensionFromMimeType(String mimeType)452 public static String guessExtensionFromMimeType(String mimeType) { 453 if (mimeType == null || mimeType.isEmpty()) { 454 return null; 455 } 456 return mimeTypeToExtensionMap.get(mimeType); 457 } 458 } 459