Home | History | Annotate | Download | only in spi
      1 /*
      2  * Copyright (c) 2007, 2011, 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
     23  * questions.
     24  */
     25 
     26 package java.nio.file.spi;
     27 
     28 import java.nio.file.Path;
     29 import java.io.IOException;
     30 
     31 /**
     32  * A file type detector for probing a file to guess its file type.
     33  *
     34  * <p> A file type detector is a concrete implementation of this class, has a
     35  * zero-argument constructor, and implements the abstract methods specified
     36  * below.
     37  *
     38  * <p> The means by which a file type detector determines the file type is
     39  * highly implementation specific. A simple implementation might examine the
     40  * <em>file extension</em> (a convention used in some platforms) and map it to
     41  * a file type. In other cases, the file type may be stored as a file <a
     42  * href="../attribute/package-summary.html"> attribute</a> or the bytes in a
     43  * file may be examined to guess its file type.
     44  *
     45  * @see java.nio.file.Files#probeContentType(Path)
     46  *
     47  * @since 1.7
     48  */
     49 
     50 public abstract class FileTypeDetector {
     51 
     52     private static Void checkPermission() {
     53         SecurityManager sm = System.getSecurityManager();
     54         if (sm != null)
     55             sm.checkPermission(new RuntimePermission("fileTypeDetector"));
     56         return null;
     57     }
     58     private FileTypeDetector(Void ignore) { }
     59 
     60     /**
     61      * Initializes a new instance of this class.
     62      *
     63      * @throws  SecurityException
     64      *          If a security manager has been installed and it denies
     65      *          {@link RuntimePermission}<tt>("fileTypeDetector")</tt>
     66      */
     67     protected FileTypeDetector() {
     68         this(checkPermission());
     69     }
     70 
     71     /**
     72      * Probes the given file to guess its content type.
     73      *
     74      * <p> The means by which this method determines the file type is highly
     75      * implementation specific. It may simply examine the file name, it may use
     76      * a file <a href="../attribute/package-summary.html">attribute</a>,
     77      * or it may examines bytes in the file.
     78      *
     79      * <p> The probe result is the string form of the value of a
     80      * Multipurpose Internet Mail Extension (MIME) content type as
     81      * defined by <a href="http://www.ietf.org/rfc/rfc2045.txt"><i>RFC&nbsp;2045:
     82      * Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet
     83      * Message Bodies</i></a>. The string must be parsable according to the
     84      * grammar in the RFC 2045.
     85      *
     86      * @param   path
     87      *          the path to the file to probe
     88      *
     89      * @return  The content type or {@code null} if the file type is not
     90      *          recognized
     91      *
     92      * @throws  IOException
     93      *          An I/O error occurs
     94      * @throws  SecurityException
     95      *          If the implementation requires to access the file, and a
     96      *          security manager is installed, and it denies an unspecified
     97      *          permission required by a file system provider implementation.
     98      *          If the file reference is associated with the default file system
     99      *          provider then the {@link SecurityManager#checkRead(String)} method
    100      *          is invoked to check read access to the file.
    101      *
    102      * @see java.nio.file.Files#probeContentType
    103      */
    104     public abstract String probeContentType(Path path)
    105         throws IOException;
    106 }
    107