The Java JNI Two-dimensional array is passed as a method parameter to the local

Source: Internet
Author: User

Java has the following local definition:

private static native int setfilter (final int fd, final int[][] array_filter);

The corresponding functions in C + + are as follows:

Jniexport Jint Jnicall Java_jnisocketcan_setfilter
(jnienv *env, Jclass obj, Jint fd, Jobjectarray array_filter)
{
struct Can_filter *pfilter = NULL;
Jint filter_num = 0;
Jint column_num = 0;
Jint i;
Jintarray filter_tmp;
Jint mem_length = 0;

Filter_num = Env->getarraylength (Array_filter);
DBG ("Filter_num =%d\n", filter_num);

mem_length = filter_num * sizeof (struct can_filter);
PFilter = (struct can_filter*) malloc (mem_length);
if (NULL = = PFilter) {
Log_err ("Memory is not enough, allocate failed\n");
Return-2;
}

for (i = 0; i < Filter_num; i++) {
Filter_tmp = (Jintarray) (Env->getobjectarrayelement (Array_filter, i));
Column_num = Env->getarraylength (filter_tmp);

if (2! = column_num) {
Log_err ("array_filter column! = 2, we only need can_id and can_mask\n");
return-1;
}

Jint *pcolumndata = env->getintarrayelements (filter_tmp, 0);
pfilter[i].can_id = pcolumndata[0];
Pfilter[i].can_mask = pcolumndata[1];
DBG ("%d:id=0x%x,mask=0x%x\n", I, pfilter[i].can_id, pfilter[i].can_mask);
Env->releaseintarrayelements (filter_tmp, pcolumndata, 0);
}

jint ret = setsockopt (FD, Sol_can_raw, Can_raw_filter, PFilter, mem_length);

Free (pfilter);
return ret;
}

The code that is called in Java is as follows:

Int[][] Can_filter = {
{0X234,0X7FF},
{0X235,0X7FF}
};

Cantest.setfilter (S, can_filter);

The Java JNI Two-dimensional array is passed as a method parameter to the local

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.