src/java.base/unix/classes/sun/nio/fs/MimeTypesFileTypeDetector.java

Print this page
rev 12244 : 8129632: (fs) Files.probeContentType returns null on Mac OS X
Summary: Append a Uniform Type Identifier-based FileType Detector for Mac OS X
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  66 
  67         String ext = getExtension(fn.toString());
  68         if (ext.isEmpty())
  69             return null;  // no extension
  70 
  71         loadMimeTypes();
  72         if (mimeTypeMap == null || mimeTypeMap.isEmpty())
  73             return null;
  74 
  75         // Case-sensitive search
  76         String mimeType;
  77         do {
  78             mimeType = mimeTypeMap.get(ext);
  79             if (mimeType == null)
  80                 ext = getExtension(ext);
  81         } while (mimeType == null && !ext.isEmpty());
  82 
  83         return mimeType;
  84     }
  85 
  86     // Get the extension of a file name.
  87     private static String getExtension(String name) {
  88         String ext = "";
  89         if (name != null && !name.isEmpty()) {
  90             int dot = name.indexOf('.');
  91             if ((dot >= 0) && (dot < name.length() - 1)) {
  92                 ext = name.substring(dot + 1);
  93             }
  94         }
  95         return ext;
  96     }
  97 
  98     /**
  99      * Parse the mime types file, and store the type-extension mappings into
 100      * mimeTypeMap. The mime types file is not loaded until the first probe
 101      * to achieve the lazy initialization. It adopts double-checked locking
 102      * optimization to reduce the locking overhead.
 103      */
 104     private void loadMimeTypes() {
 105         if (!loaded) {
 106             synchronized (this) {
 107                 if (!loaded) {
 108                     List<String> lines = AccessController.doPrivileged(
 109                         new PrivilegedAction<>() {
 110                             @Override
 111                             public List<String> run() {
 112                                 try {
 113                                     return Files.readAllLines(mimeTypesFile,
 114                                                               Charset.defaultCharset());
 115                                 } catch (IOException ignore) {
 116                                     return Collections.emptyList();
 117                                 }


   1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  66 
  67         String ext = getExtension(fn.toString());
  68         if (ext.isEmpty())
  69             return null;  // no extension
  70 
  71         loadMimeTypes();
  72         if (mimeTypeMap == null || mimeTypeMap.isEmpty())
  73             return null;
  74 
  75         // Case-sensitive search
  76         String mimeType;
  77         do {
  78             mimeType = mimeTypeMap.get(ext);
  79             if (mimeType == null)
  80                 ext = getExtension(ext);
  81         } while (mimeType == null && !ext.isEmpty());
  82 
  83         return mimeType;
  84     }
  85 












  86     /**
  87      * Parse the mime types file, and store the type-extension mappings into
  88      * mimeTypeMap. The mime types file is not loaded until the first probe
  89      * to achieve the lazy initialization. It adopts double-checked locking
  90      * optimization to reduce the locking overhead.
  91      */
  92     private void loadMimeTypes() {
  93         if (!loaded) {
  94             synchronized (this) {
  95                 if (!loaded) {
  96                     List<String> lines = AccessController.doPrivileged(
  97                         new PrivilegedAction<>() {
  98                             @Override
  99                             public List<String> run() {
 100                                 try {
 101                                     return Files.readAllLines(mimeTypesFile,
 102                                                               Charset.defaultCharset());
 103                                 } catch (IOException ignore) {
 104                                     return Collections.emptyList();
 105                                 }