Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/nio/fs/MacOSXNativeDispatcher.c
32288 views
/*1* Copyright (c) 2008, 2012, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "jlong.h"2930#include <stdlib.h>31#include <string.h>3233#include <CoreFoundation/CoreFoundation.h>3435JNIEXPORT jcharArray JNICALL36Java_sun_nio_fs_MacOSXNativeDispatcher_normalizepath(JNIEnv* env, jclass this,37jcharArray path,38jint form)39{40jcharArray result = NULL;41char *chars;42CFMutableStringRef csref = CFStringCreateMutable(NULL, 0);43if (csref == NULL) {44JNU_ThrowOutOfMemoryError(env, "native heap");45return NULL;46}47chars = (char*)(*env)->GetPrimitiveArrayCritical(env, path, 0);48if (chars != NULL) {49char chars_buf[(PATH_MAX + 1) * 2]; // utf16 + zero padding50jsize len = (*env)->GetArrayLength(env, path);51CFStringAppendCharacters(csref, (const UniChar*)chars, len);52(*env)->ReleasePrimitiveArrayCritical(env, path, chars, 0);53CFStringNormalize(csref, form);54len = CFStringGetLength(csref);55if (len < PATH_MAX) {56if (CFStringGetCString(csref, chars_buf, sizeof(chars_buf), kCFStringEncodingUTF16)) {57result = (*env)->NewCharArray(env, len);58if (result != NULL) {59(*env)->SetCharArrayRegion(env, result, 0, len, (jchar*)&chars_buf);60}61}62} else {63int ulen = (len + 1) * 2;64chars = malloc(ulen);65if (chars == NULL) {66JNU_ThrowOutOfMemoryError(env, "native heap");67} else {68if (CFStringGetCString(csref, chars, ulen, kCFStringEncodingUTF16)) {69result = (*env)->NewCharArray(env, len);70if (result != NULL) {71(*env)->SetCharArrayRegion(env, result, 0, len, (jchar*)chars);72}73}74free(chars);75}76}77}78CFRelease(csref);79return result;80}818283