|
1 /* |
|
2 * Copyright 2008-2009 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package sun.nio.fs; |
|
27 |
|
28 import java.nio.file.FileRef; |
|
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 |
|
50 public GnomeFileTypeDetector() { |
|
51 gioAvailable = initializeGio(); |
|
52 if (gioAvailable) { |
|
53 gnomeVfsAvailable = false; |
|
54 } else { |
|
55 gnomeVfsAvailable = initializeGnomeVfs(); |
|
56 } |
|
57 } |
|
58 |
|
59 @Override |
|
60 public String implProbeContentType(FileRef 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 byte[] type = probeUsingGio(buffer.address()); |
|
71 return (type == null) ? null : new String(type); |
|
72 } else { |
|
73 byte[] type = probeUsingGnomeVfs(buffer.address()); |
|
74 if (type == null) |
|
75 return null; |
|
76 String s = new String(type); |
|
77 return s.equals(GNOME_VFS_MIME_TYPE_UNKNOWN) ? null : s; |
|
78 } |
|
79 |
|
80 } finally { |
|
81 buffer.release(); |
|
82 } |
|
83 |
|
84 } |
|
85 |
|
86 // GIO |
|
87 private static native boolean initializeGio(); |
|
88 private static native byte[] probeUsingGio(long pathAddress); |
|
89 |
|
90 // GNOME VFS |
|
91 private static native boolean initializeGnomeVfs(); |
|
92 private static native byte[] probeUsingGnomeVfs(long pathAddress); |
|
93 |
|
94 static { |
|
95 AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
96 public Void run() { |
|
97 System.loadLibrary("nio"); |
|
98 return null; |
|
99 }}); |
|
100 } |
|
101 } |