1 /*
2  * Copyright (c) 2008, 2013, 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 sun.nio.fs;
27 
28 import java.nio.file.Path;
29 import java.io.IOException;
30 import java.security.AccessController;
31 import java.security.PrivilegedAction;
32 
33 /**
34  * File type detector that uses the GNOME I/O library or the deprecated
35  * GNOME VFS to guess the MIME type of a file.
36  */
37 
38 public class GnomeFileTypeDetector
39     extends AbstractFileTypeDetector
40 {
41     private static final String GNOME_VFS_MIME_TYPE_UNKNOWN =
42         "application/octet-stream";
43 
44     // true if GIO available
45     private final boolean gioAvailable;
46 
47     // true if GNOME VFS available and GIO is not available
48     private final boolean gnomeVfsAvailable;
49 
GnomeFileTypeDetector()50     public GnomeFileTypeDetector() {
51         gioAvailable = initializeGio();
52         if (gioAvailable) {
53             gnomeVfsAvailable = false;
54         } else {
55             gnomeVfsAvailable = initializeGnomeVfs();
56         }
57     }
58 
59     @Override
implProbeContentType(Path obj)60     public String implProbeContentType(Path obj) throws IOException {
61         if (!gioAvailable && !gnomeVfsAvailable)
62             return null;
63         if (!(obj instanceof UnixPath))
64             return null;
65 
66         UnixPath path = (UnixPath)obj;
67         NativeBuffer buffer = NativeBuffers.asNativeBuffer(path.getByteArrayForSysCalls());
68         try {
69             if (gioAvailable) {
70                 // GIO may access file so need permission check
71                 path.checkRead();
72                 byte[] type = probeUsingGio(buffer.address());
73                 return (type == null) ? null : Util.toString(type);
74             } else {
75                 byte[] type = probeUsingGnomeVfs(buffer.address());
76                 if (type == null)
77                     return null;
78                 String s = Util.toString(type);
79                 return s.equals(GNOME_VFS_MIME_TYPE_UNKNOWN) ? null : s;
80             }
81         } finally {
82             buffer.release();
83         }
84 
85     }
86 
87     // GIO
initializeGio()88     private static native boolean initializeGio();
probeUsingGio(long pathAddress)89     private static synchronized native byte[] probeUsingGio(long pathAddress);
90 
91     // GNOME VFS
initializeGnomeVfs()92     private static native boolean initializeGnomeVfs();
probeUsingGnomeVfs(long pathAddress)93     private static native byte[] probeUsingGnomeVfs(long pathAddress);
94 
95     static {
AccessController.doPrivileged(new PrivilegedAction<Void>() { public Void run() { System.loadLibrary(R); return null; }})96         AccessController.doPrivileged(new PrivilegedAction<Void>() {
97             public Void run() {
98                 System.loadLibrary("nio");
99                 return null;
100         }});
101     }
102 }
103