Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Attention to details and usage of JNA type mapping in Java

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "attention to details and usage of JNA type mapping of Java". In daily operation, I believe many people have doubts about the details and usage of JNA type mapping of Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "attention details and usage of Java JNA type mapping". Next, please follow the editor to study!

String

The first is the mapping of String. The String in JAVA actually corresponds to two native types: const char* and const wchar_t*. By default, String is converted to char*.

Char is the data type of ANSI type, while wchar_t is the data type of Unicode characters, also known as wide characters.

If the unicode characters of JAVA is to be converted into an char array, then some encoding needs to be done, and if jna.encoding is set, it will be encoded using the set encoding. By default, the encoding method is "UTF8".

If it is WString, then Unicode values can be copied directly into WString without any coding.

Let's start with a simple example:

Char* returnStringArgument (char* arg) {return arg;} wchar_t* returnWStringArgument (wchar_t* arg) {return arg;}

The above native code can be mapped to:

String returnStringArgument (String s); WString returnWStringArgument (WString s)

Let's look at a different example, if the definition of the native method is like this:

Int getString (char* buffer, int bufsize); int getUnicodeString (wchar_t* buffer, int bufsize)

We define two methods, whose parameters are char* and wchar_t*.

Let's take a look at how to define the mapping of methods in JAVA:

/ / Mapping A:int getString (byte [] buf, int bufsize); / / Mapping B:int getUnicodeString (char [] buf, int bufsize)

Here are the specific uses:

Byte [] buf = new byte [256]; int len = getString (buf, buf.length); String normalCString = Native.toString (buf); String embeddedNULs = new String (buf, 0, len)

Some students may ask, since String in JAVA can be converted to char*, why do you need to use the byte array here?

This is because the getString method needs to modify the contents of the incoming char array, but because String is immutable, you can't use String directly here, we need to use the byte array.

Then we use Native.toString (byte []) to convert the byte array into a JAVA string.

Take another look at the return value:

/ / Example A: Returns a C string directlyconst char* getString (); / / Example B: Returns a wide character C string directlyconst wchar_t* getString ()

In general, if the native method returns string directly, we can use String for mapping:

/ / Mapping AString getString (); / / Mapping BWString getString ()

If native code allocates memory space for String, then we'd better use Pointer in JNA as the return value so that we can free up the occupied space at some point in the future, as shown below:

Pointer getString (); Buffers,Memory, array and Pointer

When do I need to use Buffers and Memory?

In general, if the array of the underlying data is passed to the function as an argument, you can use the array of the underlying class directly in JAVA instead. But if the native method needs to access the array after the method returns (keeping the pointer to the array), it is not appropriate to use the array of the underlying class, in which case we need to use ByteBuffers or Memory.

We know that the array in JAVA has a length, but for the native method, the returned array is actually a pointer to the array, and we don't know the length of the returned array, so if the native method returns an array pointer, it is not appropriate to use an array for mapping in the JAVA code. In this case, Pointer.

Pointer represents a pointer. Take a look at the example of Pointer, starting with the native code:

Void* returnPointerArgument (void* arg) {return arg;} void* returnPointerArrayElement (void* args [], int which) {return args [which];}

Next is the mapping of JAVA:

Pointer returnPointerArgument (Pointer p); Pointer returnPointerArrayElement (Pointer [] args, int which)

In addition to basic Pointer, you can also customize typed Pointer, that is, PointerType. You only need to inherit PointerType, as shown below:

Public static class TestPointerType extends PointerType {public TestPointerType () {} public TestPointerType (Pointer p) {super (p);}} TestPointerType returnPointerArrayElement (TestPointerType [] args, int which)

Take another look at the string array:

Char* returnStringArrayElement (char* args [], int which) {return args [which];} wchar_t* returnWideStringArrayElement (wchar_t* args [], int which) {return args [which];}

The corresponding JAVA mapping is as follows:

String returnStringArrayElement (String [] args, int which); WString returnWideStringArrayElement (WString [] args, int which)

For Buffer, there are many types of buffer available in JAVA NIO, such as ByteBuffer,ShortBuffer,IntBuffer,LongBuffer,FloatBuffer and DoubleBuffer. Here, taking ByteBuffer as an example, let's take a look at the specific use.

First, take a look at the native code:

Int32_t fillInt8Buffer (int8_t * buf, int len, char value) {int i; for (iTuno Tipi < len;i++) {buf [I] = value;} return len;}

The buff is populated here, and it is obvious that we need to use this buffer later, so it is not appropriate to use an array here, we can choose to use ByteBuffer:

Int fillInt8Buffer (ByteBuffer buf, int len, byte value)

Then take a look at how to use it:

TestLibrary lib = Native.load ("testlib", TestLibrary.class); ByteBuffer buf = ByteBuffer.allocate (1024) .order (ByteOrder.nativeOrder ()); final byte MAGIC = (byte) 0xEd; lib.fillInt8Buffer (buf, 1024, MAGIC); for (int iTunes +) {assertEquals ("Bad value at index" + I, MAGIC, buf.get (I));} variable parameters

For both native and JAVA themselves, variable parameters are supported, for example, in the native method:

Int32_t addVarArgs (const char * fmt,...) {va_list ap; int32_t sum = 0; va_start (ap, fmt); while (* fmt) {switch (* fmt++) {case'dudes: sum + = va_arg (ap, int32_t); break; case'lcards: sum + = (int) va_arg (ap, int64_t); break Case's int' when passed through: / / short (promoted to 'int' when passed through'...') Case 'int' when passed through: / / byte/char (promoted to 'int' when passed through'...') Sum + = (int) va_arg (ap, int); break; case'fags: / / float (promoted to 'double' when passed through'...') Case'gathers: / / double sum + = (int) va_arg (ap, double); break; default: break;}} va_end (ap); return sum;}

The corresponding JAVA method is mapped as follows:

Public int addVarArgs (String fmt, Number... Args)

The corresponding calling code is as follows:

Int arg1 = 1nint arg2 = 2bot assertEquals ("32-bit integer varargs not added correctly", arg1 + arg2, lib.addVarArgs ("dd", arg1, arg2)); at this point, the study on "attention to details and usage of JNA type mapping of Java" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 240

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report