src/java.base/share/native/libjava/jni_util.c
changeset 49121 10f447530d32
parent 47216 71c04702a3d5
child 49123 d8057fc6fffc
equal deleted inserted replaced
49120:c04d813140dc 49121:10f447530d32
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   772     }
   772     }
   773 
   773 
   774     return newSizedStringJava(env, str, len);
   774     return newSizedStringJava(env, str, len);
   775 }
   775 }
   776 
   776 
   777 /* Initialize the fast encoding.  If the "sun.jnu.encoding" property
   777 /* Initialize the fast encoding from the encoding name. */
   778  * has not yet been set, we leave fastEncoding == NO_ENCODING_YET.
       
   779  */
       
   780 void
   778 void
   781 initializeEncoding(JNIEnv *env)
   779 InitializeEncoding(JNIEnv *env, const char *encname)
   782 {
   780 {
   783     jstring propname = 0;
       
   784     jstring enc = 0;
       
   785     jclass strClazz = NULL;
   781     jclass strClazz = NULL;
   786 
   782 
   787     if ((*env)->EnsureLocalCapacity(env, 3) < 0)
   783     if ((*env)->EnsureLocalCapacity(env, 3) < 0)
   788         return;
   784         return;
   789 
   785 
   790     strClazz = JNU_ClassString(env);
   786     strClazz = JNU_ClassString(env);
   791     CHECK_NULL(strClazz);
   787     CHECK_NULL(strClazz);
   792 
   788 
   793     propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
   789     if (encname) {
   794     if (propname) {
   790         /*
   795         jboolean exc;
   791          * On Solaris with nl_langinfo() called in GetJavaProperties():
   796         enc = JNU_CallStaticMethodByName
   792          *
   797                        (env,
   793          *   locale undefined -> NULL -> hardcoded default
   798                         &exc,
   794          *   "C" locale       -> "" -> hardcoded default     (on 2.6)
   799                         "java/lang/System",
   795          *   "C" locale       -> "ISO646-US"                 (on Sol 7/8)
   800                         "getProperty",
   796          *   "en_US" locale -> "ISO8859-1"
   801                         "(Ljava/lang/String;)Ljava/lang/String;",
   797          *   "en_GB" locale -> "ISO8859-1"                   (on Sol 7/8)
   802                         propname).l;
   798          *   "en_UK" locale -> "ISO8859-1"                   (on 2.6)
   803         if (!exc) {
   799          */
   804             if (enc) {
   800         if ((strcmp(encname, "8859_1") == 0) ||
   805                 const char* encname = (*env)->GetStringUTFChars(env, enc, 0);
   801             (strcmp(encname, "ISO8859-1") == 0) ||
   806                 if (encname) {
   802             (strcmp(encname, "ISO8859_1") == 0) ||
   807            /*
   803             (strcmp(encname, "ISO-8859-1") == 0)) {
   808             * On Solaris with nl_langinfo() called in GetJavaProperties():
   804             fastEncoding = FAST_8859_1;
   809             *
   805         } else if (strcmp(encname, "UTF-8") == 0) {
   810             *   locale undefined -> NULL -> hardcoded default
   806             fastEncoding = FAST_UTF_8;
   811             *   "C" locale       -> "" -> hardcoded default     (on 2.6)
   807             jstring enc = (*env)->NewStringUTF(env, encname);
   812             *   "C" locale       -> "ISO646-US"                 (on Sol 7/8)
   808             if (enc == NULL)
   813             *   "en_US" locale -> "ISO8859-1"
   809                 return;
   814             *   "en_GB" locale -> "ISO8859-1"                   (on Sol 7/8)
   810             jnuEncoding = (jstring)(*env)->NewGlobalRef(env, enc);
   815             *   "en_UK" locale -> "ISO8859-1"                   (on 2.6)
   811             (*env)->DeleteLocalRef(env, enc);
   816             */
   812         } else if (strcmp(encname, "ISO646-US") == 0) {
   817                     if ((strcmp(encname, "8859_1") == 0) ||
   813             fastEncoding = FAST_646_US;
   818                         (strcmp(encname, "ISO8859-1") == 0) ||
   814         } else if (strcmp(encname, "Cp1252") == 0 ||
   819                         (strcmp(encname, "ISO8859_1") == 0) ||
   815             /* This is a temporary fix until we move */
   820                         (strcmp(encname, "ISO-8859-1") == 0)) {
   816             /* to wide character versions of all Windows */
   821                         fastEncoding = FAST_8859_1;
   817             /* calls. */
   822                     } else if (strcmp(encname, "UTF-8") == 0) {
   818             strcmp(encname, "utf-16le") == 0) {
   823                         fastEncoding = FAST_UTF_8;
   819             fastEncoding = FAST_CP1252;
   824                         jnuEncoding = (jstring)(*env)->NewGlobalRef(env, enc);
       
   825                     } else if (strcmp(encname, "ISO646-US") == 0) {
       
   826                         fastEncoding = FAST_646_US;
       
   827                     } else if (strcmp(encname, "Cp1252") == 0 ||
       
   828                              /* This is a temporary fix until we move */
       
   829                              /* to wide character versions of all Windows */
       
   830                              /* calls. */
       
   831                              strcmp(encname, "utf-16le") == 0) {
       
   832                         fastEncoding = FAST_CP1252;
       
   833                     } else {
       
   834                         fastEncoding = NO_FAST_ENCODING;
       
   835                         jnuEncoding = (jstring)(*env)->NewGlobalRef(env, enc);
       
   836                     }
       
   837                     (*env)->ReleaseStringUTFChars(env, enc, encname);
       
   838                 }
       
   839             }
       
   840         } else {
   820         } else {
   841             (*env)->ExceptionClear(env);
   821             fastEncoding = NO_FAST_ENCODING;
       
   822             jstring enc = (*env)->NewStringUTF(env, encname);
       
   823             if (enc == NULL)
       
   824                 return;
       
   825             jnuEncoding = (jstring)(*env)->NewGlobalRef(env, enc);
       
   826             (*env)->DeleteLocalRef(env, enc);
   842         }
   827         }
   843     } else {
   828     } else {
   844         (*env)->ExceptionClear(env);
   829         JNU_ThrowInternalError(env, "platform encoding undefined");
   845     }
   830         return;
   846     (*env)->DeleteLocalRef(env, propname);
   831     }
   847     (*env)->DeleteLocalRef(env, enc);
       
   848 
   832 
   849     /* Initialize method-id cache */
   833     /* Initialize method-id cache */
   850     String_getBytes_ID = (*env)->GetMethodID(env, strClazz,
   834     String_getBytes_ID = (*env)->GetMethodID(env, strClazz,
   851                                              "getBytes", "(Ljava/lang/String;)[B");
   835                                              "getBytes", "(Ljava/lang/String;)[B");
   852     CHECK_NULL(String_getBytes_ID);
   836     CHECK_NULL(String_getBytes_ID);
   863 }
   847 }
   864 
   848 
   865 JNIEXPORT jstring JNICALL
   849 JNIEXPORT jstring JNICALL
   866 JNU_NewStringPlatform(JNIEnv *env, const char *str)
   850 JNU_NewStringPlatform(JNIEnv *env, const char *str)
   867 {
   851 {
   868     if (fastEncoding == NO_ENCODING_YET) {
   852     if (fastEncoding == FAST_UTF_8)
   869         initializeEncoding(env);
   853         return newStringUTF8(env, str);
   870         JNU_CHECK_EXCEPTION_RETURN(env, NULL);
   854     if (fastEncoding == FAST_8859_1)
   871     }
       
   872 
       
   873     if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
       
   874         return newString8859_1(env, str);
   855         return newString8859_1(env, str);
   875     if (fastEncoding == FAST_646_US)
   856     if (fastEncoding == FAST_646_US)
   876         return newString646_US(env, str);
   857         return newString646_US(env, str);
   877     if (fastEncoding == FAST_CP1252)
   858     if (fastEncoding == FAST_CP1252)
   878         return newStringCp1252(env, str);
   859         return newStringCp1252(env, str);
   879     if (fastEncoding == FAST_UTF_8)
   860     if (fastEncoding == NO_ENCODING_YET) {
   880         return newStringUTF8(env, str);
   861         JNU_ThrowInternalError(env, "platform encoding not initialized");
       
   862         return NULL;
       
   863     }
   881     return newStringJava(env, str);
   864     return newStringJava(env, str);
   882 }
   865 }
   883 
   866 
   884 JNIEXPORT const char *
   867 JNIEXPORT const char *
   885 GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy)
   868 GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy)
   983 {
   966 {
   984 
   967 
   985     if (isCopy)
   968     if (isCopy)
   986         *isCopy = JNI_TRUE;
   969         *isCopy = JNI_TRUE;
   987 
   970 
   988     if (fastEncoding == NO_ENCODING_YET) {
   971     if (fastEncoding == FAST_UTF_8)
   989         initializeEncoding(env);
   972         return getStringUTF8(env, jstr);
   990         JNU_CHECK_EXCEPTION_RETURN(env, 0);
   973     if (fastEncoding == FAST_8859_1)
   991     }
       
   992 
       
   993     if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
       
   994         return getString8859_1Chars(env, jstr);
   974         return getString8859_1Chars(env, jstr);
   995     if (fastEncoding == FAST_646_US)
   975     if (fastEncoding == FAST_646_US)
   996         return getString646_USChars(env, jstr);
   976         return getString646_USChars(env, jstr);
   997     if (fastEncoding == FAST_CP1252)
   977     if (fastEncoding == FAST_CP1252)
   998         return getStringCp1252Chars(env, jstr);
   978         return getStringCp1252Chars(env, jstr);
   999     if (fastEncoding == FAST_UTF_8)
   979     if (fastEncoding == NO_ENCODING_YET) {
  1000         return getStringUTF8(env, jstr);
   980         JNU_ThrowInternalError(env, "platform encoding not initialized");
  1001     else
   981         return 0;
       
   982     } else
  1002         return getStringBytes(env, jstr);
   983         return getStringBytes(env, jstr);
  1003 }
   984 }
  1004 
   985 
  1005 JNIEXPORT void JNICALL
   986 JNIEXPORT void JNICALL
  1006 JNU_ReleaseStringPlatformChars(JNIEnv *env, jstring jstr, const char *str)
   987 JNU_ReleaseStringPlatformChars(JNIEnv *env, jstring jstr, const char *str)