jdk/test/java/lang/String/nativeEncoding/libstringPlatformChars.c
changeset 45572 07f412070bd9
equal deleted inserted replaced
45571:a905332da8c6 45572:07f412070bd9
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 #include <stdlib.h>
       
    25 #include <string.h>
       
    26 
       
    27 #include "jni.h"
       
    28 #include "jni_util.h"
       
    29 
       
    30 JNIEXPORT jbyteArray JNICALL
       
    31 Java_StringPlatformChars_getBytes(JNIEnv *env, jclass unused, jstring value)
       
    32 {
       
    33     const char* str;
       
    34     int len;
       
    35     jbyteArray bytes = NULL;
       
    36 
       
    37     str = JNU_GetStringPlatformChars(env, value, NULL);
       
    38     if (str == NULL) {
       
    39         return NULL;
       
    40     }
       
    41     len = (int)strlen(str);
       
    42     bytes = (*env)->NewByteArray(env, len);
       
    43     if (bytes != 0) {
       
    44         jclass strClazz = JNU_ClassString(env);
       
    45         if (strClazz == NULL) {
       
    46             return NULL;
       
    47         }
       
    48         (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte *)str);
       
    49 
       
    50         return bytes;
       
    51     }
       
    52     return NULL;
       
    53 }
       
    54 
       
    55 JNIEXPORT jstring JNICALL
       
    56 Java_StringPlatformChars_newString(JNIEnv *env, jclass unused, jbyteArray bytes)
       
    57 {
       
    58     char* str;
       
    59     int len = (*env)->GetArrayLength(env, bytes);
       
    60     int i;
       
    61     jbyte* jbytes;
       
    62 
       
    63     str = (char*)malloc(len + 1);
       
    64     jbytes = (*env)->GetPrimitiveArrayCritical(env, bytes, NULL);
       
    65     if (jbytes == NULL) {
       
    66         return NULL;
       
    67     }
       
    68     for (i = 0; i < len; i++) {
       
    69         str[i] = (char)jbytes[i];
       
    70     }
       
    71     str[len] = '\0';
       
    72     (*env)->ReleasePrimitiveArrayCritical(env, bytes, (void*)jbytes, 0);
       
    73 
       
    74     return JNU_NewStringPlatform(env, str);
       
    75 }
       
    76