|
JNA API> 3.0 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.sun.jna.Native
public final class Native
Provides generation of invocation plumbing for a defined native library interface. Also provides various utilities for native operations.
getTypeMapper(java.lang.Class)
and getStructureAlignment(java.lang.Class)
are provided
to avoid having to explicitly pass these parameters to Structure
s,
which would require every Structure
which requires custom mapping
or alignment to define a constructor and pass parameters to the superclass.
To avoid lots of boilerplate, the base Structure
constructor
figures out these properties based on its enclosing interface.
System.loadLibrary(java.lang.String)
. If not found, the appropriate library
will be extracted from the class path into a temporary directory and
loaded from there. If your system has additional security constraints
regarding execution or load of files (SELinux, for example), you should
probably install the native library in an accessible location and configure
your system accordingly, rather than relying on JNA to extract the library
from its own jar file.
Library
Nested Class Summary | |
---|---|
static class |
Native.W32Cleanup
For internal use only. |
Field Summary | |
---|---|
static int |
LONG_SIZE
Size of a native long type, in bytes. |
static int |
POINTER_SIZE
The size of a native pointer ( void* ) on the current
platform, in bytes. |
static int |
WCHAR_SIZE
Size of a native wchar_t type, in bytes. |
Method Summary | |
---|---|
(package private) static Class |
findLibraryClass(Class cls)
Find the library interface corresponding to the given class. |
static Pointer |
getByteBufferPointer(ByteBuffer b)
Deprecated. Use getDirectBufferPointer(java.nio.Buffer) instead. |
(package private) static byte[] |
getBytes(String s)
Return a byte array corresponding to the given String. |
static long |
getComponentID(Component c)
Utility method to get the native window ID for a heavyweight Java Component as a long value. |
static Pointer |
getComponentPointer(Component c)
Utility method to get the native window pointer for a heavyweight Java Component as a Pointer value. |
static Pointer |
getDirectBufferPointer(Buffer b)
Convert a direct Buffer into a Pointer . |
static int |
getLastError()
Retrieve the last error set by the OS. |
static Map |
getLibraryOptions(Class type)
Return the preferred native library configuration options for the given class. |
static int |
getNativeSize(Class cls)
Returns the native size for a given Java class. |
static boolean |
getPreserveLastError()
Indicates whether the system last error result is preserved after every invocation. |
static int |
getStructureAlignment(Class cls)
Return the preferred structure alignment for the given native interface. |
static TypeMapper |
getTypeMapper(Class cls)
Return the preferred TypeMapper for the given native interface. |
static String |
getWebStartLibraryPath(String libName)
If running web start, determine the location of a given native library. |
static long |
getWindowID(Window w)
Utility method to get the native window ID for a Java Window
as a long value. |
static Pointer |
getWindowPointer(Window w)
Utility method to get the native window pointer for a Java Window as a Pointer value. |
static boolean |
isProtected()
Returns whether protection is enabled. |
static boolean |
isSupportedNativeType(Class cls)
Indicate whether the given class is supported as a native argument type. |
static Object |
loadLibrary(String name,
Class interfaceClass)
Load a library interface from the given shared library, providing the explicit interface class. |
static Object |
loadLibrary(String name,
Class interfaceClass,
Map libOptions)
Load a library interface from the given shared library, providing the explicit interface class and a map of options for the library. |
static void |
main(String[] args)
Prints JNA library details to the console. |
static void |
setLastError(int code)
Set the OS last error code. |
static void |
setPreserveLastError(boolean enable)
Set whether the system last error result is captured after every native invocation. |
static void |
setProtected(boolean enable)
Set whether native memory accesses are protected from invalid accesses. |
static Library |
synchronizedLibrary(Library library)
Returns a synchronized (thread-safe) library backed by the specified library. |
static String |
toString(byte[] buf)
Obtain a Java String from the given native byte array. |
static String |
toString(char[] buf)
Obtain a Java String from the given native wchar_t array. |
(package private) static void |
updateLastError(int e)
Update the last error value (called from native code). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int POINTER_SIZE
void*
) on the current
platform, in bytes.
public static final int LONG_SIZE
long
type, in bytes.
public static final int WCHAR_SIZE
wchar_t
type, in bytes.
Method Detail |
---|
public static void setProtected(boolean enable)
jna.protected
has a value of "true"
when the JNA library is first loaded.If not supported by the underlying platform, this setting will have no effect.
public static boolean isProtected()
setProtected(true)
to determine
if this platform supports protecting memory accesses.
public static void setPreserveLastError(boolean enable)
true
.
public static boolean getPreserveLastError()
public static long getWindowID(Window w) throws HeadlessException
Window
as a long
value.
This method is primarily for X11-based systems, which use an opaque
XID
(usually long int
) to identify windows.
HeadlessException
- if the current VM is running headlesspublic static long getComponentID(Component c) throws HeadlessException
Component
as a long
value.
This method is primarily for X11-based systems, which use an opaque
XID
(usually long int
) to identify windows.
HeadlessException
- if the current VM is running headlesspublic static Pointer getWindowPointer(Window w) throws HeadlessException
Window
as a Pointer
value. This method is primarily for
w32, which uses the HANDLE
type (actually
void *
) to identify windows.
HeadlessException
- if the current VM is running headlesspublic static Pointer getComponentPointer(Component c) throws HeadlessException
Component
as a Pointer
value. This method is primarily
for w32, which uses the HANDLE
type (actually
void *
) to identify windows.
HeadlessException
- if the current VM is running headlesspublic static Pointer getByteBufferPointer(ByteBuffer b)
getDirectBufferPointer(java.nio.Buffer)
instead.
Buffer
into a Pointer
.
IllegalArgumentException
- if the buffer is not direct.public static Pointer getDirectBufferPointer(Buffer b)
Buffer
into a Pointer
.
IllegalArgumentException
- if the buffer is not direct.public static String toString(byte[] buf)
jna.encoding
is set, its value will
override the platform default encoding (if supported).
public static String toString(char[] buf)
public static Object loadLibrary(String name, Class interfaceClass)
public static Object loadLibrary(String name, Class interfaceClass, Map libOptions)
name
- interfaceClass
- libOptions
- Map of library optionsstatic Class findLibraryClass(Class cls)
Library
.
public static Map getLibraryOptions(Class type)
Library
public static TypeMapper getTypeMapper(Class cls)
TypeMapper
for the given native interface.
Library.OPTION_TYPE_MAPPER
public static int getStructureAlignment(Class cls)
Library.OPTION_STRUCTURE_ALIGNMENT
static byte[] getBytes(String s)
jna.encoding
is set, its value will override
the default platform encoding (if supported).
public static int getLastError()
GetLastError()
on Windows, and errno
on
most other platforms. The value is preserved per-thread, but whether
the original value is per-thread depends on the underlying OS. The
result is undefined If getPreserveLastError()
is
false
.
public static void setLastError(int code)
static void updateLastError(int e)
public static Library synchronizedLibrary(Library library)
NativeLibrary
. Note that the
native library may still be sensitive to being called from different
threads.
library
- the library to be "wrapped" in a synchronized library.
public static String getWebStartLibraryPath(String libName)
jna.library.path
so that JNA can load libraries identified
by the <nativelib> tag in the JNLP configuration file. Returns
null
if the Web Start native library cache location can not
be determined.
Use System.getProperty("javawebstart.version")
to detect
whether your code is running under Web Start.
IllegalArgumentException
- if the library can't be found by the
Web Start class loader, which usually means it wasn't included as
a <nativelib>
resource in the JNLP file.public static int getNativeSize(Class cls)
struct
pointers unless they implement
Structure.ByValue
.
public static boolean isSupportedNativeType(Class cls)
public static void main(String[] args)
|
JNA API> 3.0 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |