Summary of "original" JNI usage

Source: Internet
Author: User

Recent projects in JNI, as a result of the first use, have looked up some information and are summarized as follows:

Resources:

1, http://docs.oracle.com/javase/6/docs/technotes/guides/jni/index.html

2, http://blog.csdn.net/fightspirite/article/details/6859829

3, http://blog.csdn.net/super005/article/details/6387575

4, http://www.cr173.com/html/17867_1.html

5, http://www.cnblogs.com/codc-5117/archive/2012/09/06/2672815.html

6, http://www.cnblogs.com/codc-5117/archive/2012/09/06/2672833.html

7, http://blog.chinaunix.net/uid-23065002-id-3077584.html

8, http://blog.csdn.net/zgyulongfei/article/details/7409441

Summary one: About JNIEnv and JAVAVM

Jnienv* and javavm* are frequently used in native programs. While C and C + + code use the two pointers jnienv* and javavm* are different, most of the online code uses C + +, basically can not find a detailed description of C and C + + on this issue.

In C:

Use jnienv* env to do this (*env), Method name (env, parameter list)

Use the javavm* vm like this (*VM), method name (VM, parameter list)

In C + +:

Use jnienv* env to do this env-> method name (parameter list)

Use javavm* VM to vm-> method name (parameter list)

The difference between the above is that Env and VM must be addressed indirectly in C (the resulting content is still a pointer), and the Env or VM is passed in as the first parameter when calling the method. C + + invokes its members directly with Env and VM pointers.

Summary two: "char *" and "Java string" conversion to each other

1. Jstring Convert to C-style string

char* test = (char*) (*env)->getstringutfchars (env,jstring,null);

When you are finished, you should call:

(*env)->releasestringutfchars (env,jstring, test);

Frees resources.

2. C-style string conversion to jstring

Char charstr[50];

Jstring Jstr;

JSTR = env->newstringutf (CHARSTR);

3. A section of char* buffer obtained in C is passed to Java

In Jni, new a byte array, and then use the

(*env)->setbytearrayregion (env,bytearray, 0, Len, buffer)

The operation copies the buffer into the array.

This approach is mainly for the presence of "" "in buffer, if read in the form of a C-style string, you will lose the character after" \ ".

4. Array manipulation

The list of related functions for array operations is as follows:

JNI function function

Getarraylength returns the number of elements in an array

Newobjectarray creating an array of raw data types of a specified length

Getobjectarrayelement returns an element of an object array

Setobjectarrayelement setting elements of an object array

Getobjectarrayregion Copy the contents of the original data type array into a pre-allocated memory cache

Setobjectarrayregion setting the value of an array in the cache

Releaseobjectarrayregion releasing Getobjectarrayregion allocated memory

For array operations of basic data types such as Int,char, replace the related object name with the corresponding base data type name as the correlation function.

The method selection for array manipulation is based on the needs of the consumer, and if the user needs to copy and manipulate the array in memory then generally use the getobjectarrayregion and setobjectarrayregion functions. Otherwise, the setobjectarrayelement and getobjectarrayelement functions are generally used.

Write a simple example to share:

Http://pan.baidu.com/s/11MThi

Summary three: Commonly used string type processing functions:

(1) const char* getstringutfchars (jstring string,jboolean* iscopy)

Returns a pointer to the string UTF encoding, or null if the character array cannot be created. This pointer is valid until the Releasestringutfchar () function is called. Parameters:

String Java String Object

Iscopy If a copy is made, point to the jboolean filled with jni_true, otherwise point to Jboolean populated with Jni_false.

(2) void Releasestringutfchars (jstring str, const char* chars)

Notifies the virtual machine that local code no longer needs to access the Java string through chars.

Parameters:

String Java String Object

Chars the pointer returned by Getstringchars

(3) jstring Newstringutf (const char *UTF)

Returns a new Java string and copies the UTF contents into a new string, or null if the string object cannot be created. This is usually used when the inverse value type is string.

Parameters:

UTF-UTF-encoded string pointers, for numeric parameters, can be used directly in C + +

Parameters:

Array Object

Iscopy If a copy is made, point to the jboolean filled with jni_true, otherwise point to Jboolean populated with Jni_false.

Example: Jboolean * getbooleanarrayelements (jbooleanarray array, Jboolean *iscopy)

(4) void releasexxxarrayelements (Xxxarray array,xxx *elems, Jint mode)

Notifies the virtual machine that no more pointers are needed from getxxxarrayelements.

Parameters:

Array Object

Elems pointers to array elements that are no longer needed

Mode 0 = release of the elems buffer after updating an array element

Jni_commit= does not release the Elems buffer after updating an array element

jni_abort= do not update array elements to release elems buffers

Example: void Releasebooleanarrayelements (Jbooleanarray array,jboolean *elems, Jint mode)

(5) Xxxarray Newxxxarray (Jsize len)

Produces a new array, usually used when the type of the inverse is an array.

Parameters:

The number of elements in the Len array.

Example: Jbooleanarray newbooleanarray (Jsize len)

Summary nine: JNI memory leaks

One:

In C + +, the object of new, if not returned to Java, must be released, or memory leaks. including Newstringutf,newobject and so on.

If you return Java, you do not have to release,java yourself.

...

Jstring jstr = Env->newstringutf ((*p). sess_id);

...

Env->deletelocalref (JSTR); /* must be called, otherwise leaked */

...

Jobject jobj = Env->newobject (clazz,midinit); /* There is a return, no tube */

return jobj;

Two:

Jstring jstr = (jstring) env->callobjectmethod (Authenrequest, mid_authenreq_getsdid_s);

Env->getstringutfregion (Jstr,0,env->getstringlength (JSTR), authenreq.sd_id);

Env->getstringlength (JSTR) will fail when JSTR is null, causing the JVM to crash

Three:

During JNI programming, the lifetime of the JNI Local Reference is properly controlled.

If you need to create too many Local Reference, you will need to invoke the JNI function (such as DELETELOCALREF ()) after the operation of the referenced Java object is completed, and the JNI local Reference from local Ref Table to avoid potential memory leaks.

Summary of "original" JNI usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.