public class Loader extends Object
offsetof() or sizeof() a native
struct, class, or union with its Pointer peer class
and a HashMap initialized by the native libraries inside JNI_OnLoad().| Modifier and Type | Field and Description |
|---|---|
(package private) static File |
cacheDir
User-specified cache directory set and returned by
getCacheDir(). |
(package private) static Map<String,String> |
loadedLibraries
Contains all the native libraries that we have loaded to avoid reloading them.
|
private static Logger |
logger |
(package private) static WeakHashMap<Class<? extends Pointer>,HashMap<String,Integer>> |
memberOffsets
Contains
offsetof() and sizeof() values of native types
of struct, class, and union. |
private static String |
PLATFORM
Value created out of "java.vm.name", "os.name", and "os.arch" system properties.
|
private static Properties |
platformProperties
Default platform properties loaded and returned by
loadProperties(). |
(package private) static File |
tempDir
Temporary directory set and returned by
getTempDir(). |
| Constructor and Description |
|---|
Loader() |
| Modifier and Type | Method and Description |
|---|---|
static File |
cacheResource(Class cls,
String name)
|
static File |
cacheResource(String name)
Extracts a resource using the
ClassLoader of the caller class,
and returns the cached File. |
static File |
cacheResource(URL resourceURL)
Returns
cacheResource(resourceUrl, null) |
static File |
cacheResource(URL resourceURL,
String target)
Extracts a resource, if the size or last modified timestamp differs from what is in cache,
and returns the cached
File. |
static File[] |
cacheResources(Class cls,
String name)
Extracts resources using the
ClassLoader of the specified Class,
and returns the cached File objects. |
static File[] |
cacheResources(String name)
Extracts resources using the
ClassLoader of the caller class,
and returns the cached File objects. |
static String |
createLibraryLink(String filename,
ClassProperties properties,
String libnameversion)
Creates a version-less symbolic link to a library file, if needed.
|
static File |
extractResource(Class cls,
String name,
File directory,
String prefix,
String suffix)
Extracts by name a resource using the
ClassLoader of the specified Class. |
static File |
extractResource(String name,
File directory,
String prefix,
String suffix)
Extracts by name a resource using the
ClassLoader of the caller. |
static File |
extractResource(URL resourceURL,
File directoryOrFile,
String prefix,
String suffix)
Extracts a resource into the specified directory and with the specified
prefix and suffix for the filename.
|
static File[] |
extractResources(Class cls,
String name,
File directory,
String prefix,
String suffix)
Extracts by name resources using the
ClassLoader of the specified Class. |
static File[] |
extractResources(String name,
File directory,
String prefix,
String suffix)
Extracts by name resources using the
ClassLoader of the caller. |
static URL[] |
findLibrary(Class cls,
ClassProperties properties,
String libnameversion,
boolean pathsFirst)
Finds from where the library may be extracted and loaded among the
Class
resources. |
static URL[] |
findResources(Class cls,
String name)
Finds by name resources using the
ClassLoader of the specified Class. |
static File |
getCacheDir()
Creates and returns
System.getProperty("org.bytedeco.javacpp.cachedir") or ~/.javacpp/cache/ when not set. |
static Class |
getCallerClass(int i)
Returns the
Class object that contains a caller's method. |
static Class |
getEnclosingClass(Class cls)
If annotated with properties, returns the argument as "enclosing Class".
|
static String |
getPlatform()
Returns either the value of the "org.bytedeco.javacpp.platform"
system property, or
PLATFORM when the former is not set. |
static File |
getTempDir()
Creates a unique name for
tempDir out of
System.getProperty("java.io.tmpdir") and System.nanoTime(). |
static boolean |
isLoadLibraries()
Returns
System.getProperty("org.bytedeco.javacpp.loadlibraries"). |
static String |
load()
Returns
load(getCallerClass(2), loadProperties(), false). |
static String |
load(boolean pathsFirst)
Loads native libraries associated with the
Class of the caller. |
static String |
load(Class cls)
Returns
load(cls, loadProperties(), false). |
static String |
load(Class cls,
Properties properties,
boolean pathsFirst)
Loads native libraries associated with the given
Class. |
static String |
loadLibrary(URL[] urls,
String libnameversion)
Tries to load the library from the URLs in order, extracting resources as necessary.
|
static Properties |
loadProperties()
Loads the
Properties associated with the default getPlatform(). |
static ClassProperties |
loadProperties(Class[] cls,
Properties properties,
boolean inherit)
For all the classes, loads all properties from each Class annotations for the given platform.
|
static ClassProperties |
loadProperties(Class cls,
Properties properties,
boolean inherit)
Loads all properties from Class annotations for the given platform.
|
static Properties |
loadProperties(String name,
String defaults)
Loads from resources the default
Properties of the specified platform name. |
static int |
offsetof(Class<? extends Pointer> type,
String member)
Gets
offsetof() values from memberOffsets filled by native libraries. |
(package private) static void |
putMemberOffset(Class<? extends Pointer> type,
String member,
int offset)
|
(package private) static Class |
putMemberOffset(String typeName,
String member,
int offset)
|
static int |
sizeof(Class<? extends Pointer> type)
Gets
sizeof() values from memberOffsets filled by native libraries. |
private static final Logger logger
private static final String PLATFORM
getPlatform() as default.private static Properties platformProperties
loadProperties().static File cacheDir
getCacheDir().static File tempDir
getTempDir().static Map<String,String> loadedLibraries
static WeakHashMap<Class<? extends Pointer>,HashMap<String,Integer>> memberOffsets
offsetof() and sizeof() values of native types
of struct, class, and union. A WeakHashMap
is used to prevent the Loader from hanging onto Class objects the user may
be trying to unload.public static String getPlatform()
PLATFORM when the former is not set.System.getProperty("org.bytedeco.javacpp.platform", platform)PLATFORMpublic static Properties loadProperties()
Properties associated with the default getPlatform().loadProperties(getPlatform(), null)loadProperties(String, String)public static Properties loadProperties(String name, String defaults)
Properties of the specified platform name.
The resource must be at "org/bytedeco/javacpp/properties/" + name + ".properties".name - the platform namedefaults - the fallback platform name (null == "generic")public static Class getEnclosingClass(Class cls)
cls - the Class to start the search fromPlatform,
Propertiespublic static ClassProperties loadProperties(Class[] cls, Properties properties, boolean inherit)
public static ClassProperties loadProperties(Class cls, Properties properties, boolean inherit)
Properties.inherit()
annotation recursively via the inherit argument.cls - the Class of which to return Propertiesproperties - the platform Properties to inheritinherit - indicates whether or not to inherit properties from other classespublic static Class getCallerClass(int i)
Class object that contains a caller's method.i - the offset on the call stack of the method of interestnull if not foundpublic static File cacheResource(String name) throws IOException
ClassLoader of the caller class,
and returns the cached File.name - the name of the resource passed to Class.getResource(String)IOExceptioncacheResource(Class, String)public static File cacheResource(Class cls, String name) throws IOException
cls - the Class from which to load resourcesname - the name of the resource passed to Class.getResource(String)IOExceptioncacheResource(URL)public static File[] cacheResources(String name) throws IOException
ClassLoader of the caller class,
and returns the cached File objects.name - of the resources passed to findResources(Class, String)IOExceptioncacheResources(Class, String)public static File[] cacheResources(Class cls, String name) throws IOException
ClassLoader of the specified Class,
and returns the cached File objects.cls - the Class from which to load resourcesname - of the resources passed to findResources(Class, String)IOExceptioncacheResource(URL)public static File cacheResource(URL resourceURL) throws IOException
cacheResource(resourceUrl, null)IOExceptionpublic static File cacheResource(URL resourceURL, String target) throws IOException
File. If target is not null, creates instead a symbolic link
where the resource would have been extracted.resourceURL - the URL of the resource to extract and cachetarget - of the symbolic link to create (must be null to have the resource actually extracted)IOException - if fails to extract resource properlyextractResource(URL, File, String, String),
cacheDirpublic static File extractResource(String name, File directory, String prefix, String suffix) throws IOException
ClassLoader of the caller.name - the name of the resource passed to Class.getResource(String)IOExceptionextractResource(URL, File, String, String)public static File extractResource(Class cls, String name, File directory, String prefix, String suffix) throws IOException
ClassLoader of the specified Class.cls - the Class from which to load resourcesname - the name of the resource passed to Class.getResource(String)IOExceptionextractResource(URL, File, String, String)public static File[] extractResources(String name, File directory, String prefix, String suffix) throws IOException
ClassLoader of the caller.name - of the resources passed to findResources(Class, String)IOExceptionextractResources(Class, String, File, String, String)public static File[] extractResources(Class cls, String name, File directory, String prefix, String suffix) throws IOException
ClassLoader of the specified Class.cls - the Class from which to load resourcesname - of the resources passed to findResources(Class, String)IOExceptionextractResource(URL, File, String, String)public static File extractResource(URL resourceURL, File directoryOrFile, String prefix, String suffix) throws IOException
null,
the original filename is used, so directoryOrFile must not be null.resourceURL - the URL of the resource to extractdirectoryOrFile - the output directory or file (null == System.getProperty("java.io.tmpdir"))prefix - the prefix of the temporary filename to usesuffix - the suffix of the temporary filename to useIOException - if fails to extract resource properlypublic static URL[] findResources(Class cls, String name) throws IOException
ClassLoader of the specified Class.
Names not prefixed with '/' are considered relative to the Class.cls - the Class from whose ClassLoader to load resourcesname - of the resources passed to ClassLoader.getResources(String)IOExceptionpublic static File getCacheDir() throws IOException
System.getProperty("org.bytedeco.javacpp.cachedir") or ~/.javacpp/cache/ when not set.IOExceptionpublic static File getTempDir()
tempDir out of
System.getProperty("java.io.tmpdir") and System.nanoTime().tempDirpublic static boolean isLoadLibraries()
System.getProperty("org.bytedeco.javacpp.loadlibraries").
Flag set by the Builder to tell us not to try to load anything.public static String load()
load(getCallerClass(2), loadProperties(), false).public static String load(boolean pathsFirst)
Class of the caller.pathsFirst - search the paths first before bundled resourcesload(getCallerClass(2), loadProperties(), pathsFirst) getCallerClass(int),
load(Class, Properties, boolean)public static String load(Class cls, Properties properties, boolean pathsFirst)
Class.cls - the Class to get native library information fromproperties - the platform Properties to inheritpathsFirst - search the paths first before bundled resourcesif (!isLoadLibraries() || cls == null) { return null; })NoClassDefFoundError - on Class initialization failureUnsatisfiedLinkError - on native library loading failurefindLibrary(Class, ClassProperties, String, boolean),
loadLibrary(URL[], String)public static URL[] findLibrary(Class cls, ClassProperties properties, String libnameversion, boolean pathsFirst)
Class
resources. But in case that fails, and depending on the value of pathsFirst,
either as a fallback or in priority over bundled resources, also searches the paths
found in the "platform.preloadpath" and "platform.linkpath" class properties as well as
the "java.library.path" system property, in that order.cls - the Class whose package name and ClassLoader are used to extract from resourcesproperties - contains the directories to scan for if we fail to extract the library from resourceslibnameversion - the name of the library + "@" + optional version tag
+ "#" + a second optional name used at extraction (or empty to prevent it)pathsFirst - search the paths first before bundled resourcespublic static String loadLibrary(URL[] urls, String libnameversion)
System.loadLibrary(String).urls - the URLs to try loading the library fromlibnameversion - the name of the library + "@" + optional version tag
+ "#" + a second optional name used at extractionif (!isLoadLibraries) { return null; })UnsatisfiedLinkError - on failurepublic static String createLibraryLink(String filename, ClassProperties properties, String libnameversion)
filename - of the probably versioned libraryproperties - of the class associated with the librarylibnameversion - the library name and version as with loadLibrary(URL[], String)static Class putMemberOffset(String typeName, String member, int offset) throws ClassNotFoundException
offsetof() and sizeof() values in memberOffsets.
Tries to load the Class object for typeName using the ClassLoader of the Loader.typeName - the name of the peer Class acting as interface to the native typemember - the name of the native member variable (can be null to retrieve the Class object only)offset - the value of offsetof() (or sizeof() when member.equals("sizeof"))Class.forName(typeName, false)ClassNotFoundException - on Class initialization failurestatic void putMemberOffset(Class<? extends Pointer> type, String member, int offset)
type - the peer Class acting as interface to the native typemember - the name of the native member variableoffset - the value of offsetof() (or sizeof() when member.equals("sizeof"))public static int offsetof(Class<? extends Pointer> type, String member)
offsetof() values from memberOffsets filled by native libraries.type - the peer Class acting as interface to the native typemember - the name of the native member variablememberOffsets.get(type).get(member)public static int sizeof(Class<? extends Pointer> type)
sizeof() values from memberOffsets filled by native libraries.type - the peer Class acting as interface to the native typememberOffsets.get(type).get("sizeof")Copyright © 2016. All rights reserved.