Java - JNI - 将UCS-2字符转换为jchar(即Java的char)

时间:2015-10-15 08:40:07

标签: java c++ arrays casting java-native-interface

我想将一系列“宽”字符从本机C ++代码传输到Java代码。为此,我有一个原生实现的Java方法:

public native char[] getRawText();

在C ++中,我有一个连续的uchar16_t缓冲区(实际上是2个字节宽的无符号整数)。我想将该缓冲区的内容复制到要返回的Java char数组中。

执行以下操作是否安全:

// Buffer of uchar16_t characters (UCS-2 encoded unicode).
const uchar16_t * const wsRawText = /* Get it from native code */
// Length of that buffer
const uint32_t nLength = /* Same */

// Allocate the Java char array to be returned.
jcharArray pCharArray = pEnv->NewCharArray( nLength );
// Set the array elements, casting const uchar16_t * to const jchar *
pEnv->SetCharArrayRegion( pCharArray, 0, nLength, wsRawText );

return pCharArray;

将const uchar16_t *转换为const jchar *?

是否可以

char数组大部分时间长约2000个字符,所以我想避免无用的副本。

我省略了异常检查以保持代码简单。

谢谢。

1 个答案:

答案 0 :(得分:0)

jchar在jhi.h中定义为:

{
timeoff[] /* Array of timeoffs 
           * i2to -Index to Timeoff */
generaltime[] /* Array of generaltime, initially it contains only one entry
               * at the end of execution generaltime contains all the availability times
               * i2gt - Index to Generaltime */
do /* For each entry in timeoff
{
    do /* for each entry in generaltime
    {
        if((timeoff[i2to].start <= generaltime[i2gt].start) && (timeoff[i2to].end >= generaltime[i2gt].end))
        {
            /* Current timoff is in Inside start touching, Inside, Inside End touching and Exact case. 
             * So, this can not be part of available time. clear current generaltime 
            generaltime[i2gt].start = generaltime[i2gt].end = 0;
        }
        else if((timeoff[i2to].start >= generaltime[i2gt].end) || (timeoff[i2to] <= generaltime[i2gt].start))
        {
            /* Current timeoff is in After, Start Touching, End Touching and Before case
             * Leave the generaltime as it and it will make to acutalTime
        }
        else
        {
            /* All remaining cases */

            if(timeoff_start <= generaltime_start)
            {
                /* Start inside & Enclosing Start Touching case
                 * Cut overlapped timeoff from general_time
                generaltime[i2gt].start = timeoff[i2to].end 
            }
            else 
            {
                if(timeoff_end < generaltime_end)
                {
                    /* Enclosing case
                     * Split the general time in to two

                    /* First entry has generaltime[i2gt].start to timeoff[i2to].start
                    generaltime[i2gt].end = timeoff[i2to].start 

                    /* Create new entry in the generaltime by pushing out remaining entries to */
                    Push entries from i2gt+1 to numgt to i2gt+2 to numgt+1
                    generaltime[i2gt+1].start = timeoff[i2to].end 
                    generaltime[i2gt+1].end = generaltime[i2gt].end
                    numgt++;
                }
                else
                {
                    /* End Inside and enclosing end touching case
                     * Cut overlapped timeoff from general_time
                     generaltime[i2gt].end = timeoff[i2to].start
                }
            }
        }
    } while(more entries in generaltime)
} while (more entries in timeoff)

/* Generaltime contains all actual availability time
Display all entries in generaltime
}

如果您尝试将字符串返回给java,请查看JNI函数NewStringUTF(),它接受一个C字符串并返回一个java字符串

NewStringUTF