Path: blob/master/test/jdk/java/lang/String/nativeEncoding/libstringPlatformChars.c
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdlib.h>24#include <string.h>2526#include "jni.h"27#include "jni_util.h"2829JNIEXPORT jbyteArray JNICALL30Java_StringPlatformChars_getBytes(JNIEnv *env, jclass unused, jstring value)31{32const char* str;33int len;34jbyteArray bytes = NULL;3536str = JNU_GetStringPlatformChars(env, value, NULL);37if (str == NULL) {38return NULL;39}40len = (int)strlen(str);41bytes = (*env)->NewByteArray(env, len);42if (bytes != 0) {43jclass strClazz = JNU_ClassString(env);44if (strClazz == NULL) {45return NULL;46}47(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte *)str);4849return bytes;50}51return NULL;52}5354JNIEXPORT jstring JNICALL55Java_StringPlatformChars_newString(JNIEnv *env, jclass unused, jbyteArray bytes)56{57char* str;58int len = (*env)->GetArrayLength(env, bytes);59int i;60jbyte* jbytes;6162str = (char*)malloc(len + 1);63jbytes = (*env)->GetPrimitiveArrayCritical(env, bytes, NULL);64if (jbytes == NULL) {65return NULL;66}67for (i = 0; i < len; i++) {68str[i] = (char)jbytes[i];69}70str[len] = '\0';71(*env)->ReleasePrimitiveArrayCritical(env, bytes, (void*)jbytes, 0);7273return JNU_NewStringPlatform(env, str);74}75767778