< prev index next >

src/java.base/share/native/libjava/jni_util.c

Print this page
rev 17266 : 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
Reviewed-by: TBD


 483     result = MALLOC_MIN4(len);
 484     if (result == 0) {
 485         (*env)->ReleaseStringCritical(env, jstr, str);
 486         JNU_ThrowOutOfMemoryError(env, 0);
 487         return 0;
 488     }
 489 
 490     for (i=0; i<len; i++) {
 491         jchar unicode = str[i];
 492         if (unicode <= 0x00ff)
 493             result[i] = (char)unicode;
 494         else
 495             result[i] = '?';
 496     }
 497 
 498     result[len] = 0;
 499     (*env)->ReleaseStringCritical(env, jstr, str);
 500     return result;
 501 }
 502 


















 503 
 504 /* Optimized for char set ISO646-US (us-ascii) */
 505 static jstring
 506 newString646_US(JNIEnv *env, const char *str)
 507 {
 508     int len = (int)strlen(str);
 509     jchar buf[512];
 510     jchar *str1;
 511     jstring result;
 512     int i;
 513 
 514     if (len > 512) {
 515         str1 = (jchar *)malloc(len * sizeof(jchar));
 516         if (str1 == 0) {
 517             JNU_ThrowOutOfMemoryError(env, 0);
 518             return 0;
 519         }
 520     } else
 521         str1 = buf;
 522 


 703                         propname).l;
 704         if (!exc) {
 705             if (enc) {
 706                 const char* encname = (*env)->GetStringUTFChars(env, enc, 0);
 707                 if (encname) {
 708            /*
 709             * On Solaris with nl_langinfo() called in GetJavaProperties():
 710             *
 711             *   locale undefined -> NULL -> hardcoded default
 712             *   "C" locale       -> "" -> hardcoded default     (on 2.6)
 713             *   "C" locale       -> "ISO646-US"                 (on Sol 7/8)
 714             *   "en_US" locale -> "ISO8859-1"
 715             *   "en_GB" locale -> "ISO8859-1"                   (on Sol 7/8)
 716             *   "en_UK" locale -> "ISO8859-1"                   (on 2.6)
 717             */
 718                     if ((strcmp(encname, "8859_1") == 0) ||
 719                         (strcmp(encname, "ISO8859-1") == 0) ||
 720                         (strcmp(encname, "ISO8859_1") == 0) ||
 721                         (strcmp(encname, "ISO-8859-1") == 0))
 722                         fastEncoding = FAST_8859_1;


 723                     else if (strcmp(encname, "ISO646-US") == 0)
 724                         fastEncoding = FAST_646_US;
 725                     else if (strcmp(encname, "Cp1252") == 0 ||
 726                              /* This is a temporary fix until we move */
 727                              /* to wide character versions of all Windows */
 728                              /* calls. */
 729                              strcmp(encname, "utf-16le") == 0)
 730                         fastEncoding = FAST_CP1252;
 731                     else {
 732                         fastEncoding = NO_FAST_ENCODING;
 733                         jnuEncoding = (jstring)(*env)->NewGlobalRef(env, enc);
 734                     }
 735                     (*env)->ReleaseStringUTFChars(env, enc, encname);
 736                 }
 737             }
 738         } else {
 739             (*env)->ExceptionClear(env);
 740         }
 741     } else {
 742         (*env)->ExceptionClear(env);


 775 }
 776 
 777 JNIEXPORT jstring JNICALL
 778 JNU_NewStringPlatform(JNIEnv *env, const char *str)
 779 {
 780     jstring result = NULL;
 781     jbyteArray hab = 0;
 782     int len;
 783 
 784     if (fastEncoding == NO_ENCODING_YET) {
 785         initializeEncoding(env);
 786         JNU_CHECK_EXCEPTION_RETURN(env, NULL);
 787     }
 788 
 789     if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
 790         return newString8859_1(env, str);
 791     if (fastEncoding == FAST_646_US)
 792         return newString646_US(env, str);
 793     if (fastEncoding == FAST_CP1252)
 794         return newStringCp1252(env, str);


 795 
 796     if ((*env)->EnsureLocalCapacity(env, 2) < 0)
 797         return NULL;
 798 
 799     len = (int)strlen(str);
 800     hab = (*env)->NewByteArray(env, len);
 801     if (hab != 0) {
 802         jclass strClazz = JNU_ClassString(env);
 803         CHECK_NULL_RETURN(strClazz, 0);
 804         (*env)->SetByteArrayRegion(env, hab, 0, len, (jbyte *)str);
 805         if (jnuEncodingSupported(env)) {
 806             result = (*env)->NewObject(env, strClazz,
 807                                        String_init_ID, hab, jnuEncoding);
 808         } else {
 809             /*If the encoding specified in sun.jnu.encoding is not endorsed
 810               by "Charset.isSupported" we have to fall back to use String(byte[])
 811               explicitly here without specifying the encoding name, in which the
 812               StringCoding class will pickup the iso-8859-1 as the fallback
 813               converter for us.
 814              */


 833 JNIEXPORT const char * JNICALL
 834 JNU_GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy)
 835 {
 836     char *result = NULL;
 837     jbyteArray hab = 0;
 838 
 839     if (isCopy)
 840         *isCopy = JNI_TRUE;
 841 
 842     if (fastEncoding == NO_ENCODING_YET) {
 843         initializeEncoding(env);
 844         JNU_CHECK_EXCEPTION_RETURN(env, 0);
 845     }
 846 
 847     if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
 848         return getString8859_1Chars(env, jstr);
 849     if (fastEncoding == FAST_646_US)
 850         return getString646_USChars(env, jstr);
 851     if (fastEncoding == FAST_CP1252)
 852         return getStringCp1252Chars(env, jstr);


 853 
 854     if ((*env)->EnsureLocalCapacity(env, 2) < 0)
 855         return 0;
 856 
 857     if (jnuEncodingSupported(env)) {
 858         hab = (*env)->CallObjectMethod(env, jstr, String_getBytes_ID, jnuEncoding);
 859     } else {
 860         jmethodID mid;
 861         jclass strClazz = JNU_ClassString(env);
 862         CHECK_NULL_RETURN(strClazz, 0);
 863         mid = (*env)->GetMethodID(env, strClazz,
 864                                        "getBytes", "()[B");
 865         if (mid != NULL) {
 866             hab = (*env)->CallObjectMethod(env, jstr, mid);
 867         }
 868     }
 869 
 870     if (!(*env)->ExceptionCheck(env)) {
 871         jint len = (*env)->GetArrayLength(env, hab);
 872         result = MALLOC_MIN4(len);




 483     result = MALLOC_MIN4(len);
 484     if (result == 0) {
 485         (*env)->ReleaseStringCritical(env, jstr, str);
 486         JNU_ThrowOutOfMemoryError(env, 0);
 487         return 0;
 488     }
 489 
 490     for (i=0; i<len; i++) {
 491         jchar unicode = str[i];
 492         if (unicode <= 0x00ff)
 493             result[i] = (char)unicode;
 494         else
 495             result[i] = '?';
 496     }
 497 
 498     result[len] = 0;
 499     (*env)->ReleaseStringCritical(env, jstr, str);
 500     return result;
 501 }
 502 
 503 static const char*
 504 getStringUTF8(JNIEnv *env, jstring jstr)
 505 {
 506     jsize len;
 507     jsize unicode_len;
 508     char *result;
 509     len = (*env)->GetStringUTFLength(env, jstr);
 510     unicode_len = (*env)->GetStringLength(env, jstr);
 511     result = malloc(len + 1);
 512     if (result == NULL) {
 513         JNU_ThrowOutOfMemoryError(env, NULL);
 514         return NULL;
 515     }
 516     (*env)->GetStringUTFRegion(env, jstr, 0, unicode_len, result);
 517 
 518     return result;
 519 }
 520 
 521 
 522 /* Optimized for char set ISO646-US (us-ascii) */
 523 static jstring
 524 newString646_US(JNIEnv *env, const char *str)
 525 {
 526     int len = (int)strlen(str);
 527     jchar buf[512];
 528     jchar *str1;
 529     jstring result;
 530     int i;
 531 
 532     if (len > 512) {
 533         str1 = (jchar *)malloc(len * sizeof(jchar));
 534         if (str1 == 0) {
 535             JNU_ThrowOutOfMemoryError(env, 0);
 536             return 0;
 537         }
 538     } else
 539         str1 = buf;
 540 


 721                         propname).l;
 722         if (!exc) {
 723             if (enc) {
 724                 const char* encname = (*env)->GetStringUTFChars(env, enc, 0);
 725                 if (encname) {
 726            /*
 727             * On Solaris with nl_langinfo() called in GetJavaProperties():
 728             *
 729             *   locale undefined -> NULL -> hardcoded default
 730             *   "C" locale       -> "" -> hardcoded default     (on 2.6)
 731             *   "C" locale       -> "ISO646-US"                 (on Sol 7/8)
 732             *   "en_US" locale -> "ISO8859-1"
 733             *   "en_GB" locale -> "ISO8859-1"                   (on Sol 7/8)
 734             *   "en_UK" locale -> "ISO8859-1"                   (on 2.6)
 735             */
 736                     if ((strcmp(encname, "8859_1") == 0) ||
 737                         (strcmp(encname, "ISO8859-1") == 0) ||
 738                         (strcmp(encname, "ISO8859_1") == 0) ||
 739                         (strcmp(encname, "ISO-8859-1") == 0))
 740                         fastEncoding = FAST_8859_1;
 741                     else if (strcmp(encname, "UTF-8") == 0)
 742                         fastEncoding = FAST_UTF_8;
 743                     else if (strcmp(encname, "ISO646-US") == 0)
 744                         fastEncoding = FAST_646_US;
 745                     else if (strcmp(encname, "Cp1252") == 0 ||
 746                              /* This is a temporary fix until we move */
 747                              /* to wide character versions of all Windows */
 748                              /* calls. */
 749                              strcmp(encname, "utf-16le") == 0)
 750                         fastEncoding = FAST_CP1252;
 751                     else {
 752                         fastEncoding = NO_FAST_ENCODING;
 753                         jnuEncoding = (jstring)(*env)->NewGlobalRef(env, enc);
 754                     }
 755                     (*env)->ReleaseStringUTFChars(env, enc, encname);
 756                 }
 757             }
 758         } else {
 759             (*env)->ExceptionClear(env);
 760         }
 761     } else {
 762         (*env)->ExceptionClear(env);


 795 }
 796 
 797 JNIEXPORT jstring JNICALL
 798 JNU_NewStringPlatform(JNIEnv *env, const char *str)
 799 {
 800     jstring result = NULL;
 801     jbyteArray hab = 0;
 802     int len;
 803 
 804     if (fastEncoding == NO_ENCODING_YET) {
 805         initializeEncoding(env);
 806         JNU_CHECK_EXCEPTION_RETURN(env, NULL);
 807     }
 808 
 809     if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
 810         return newString8859_1(env, str);
 811     if (fastEncoding == FAST_646_US)
 812         return newString646_US(env, str);
 813     if (fastEncoding == FAST_CP1252)
 814         return newStringCp1252(env, str);
 815     if (fastEncoding == FAST_UTF_8)
 816         return (*env)->NewStringUTF(env, str);
 817 
 818     if ((*env)->EnsureLocalCapacity(env, 2) < 0)
 819         return NULL;
 820 
 821     len = (int)strlen(str);
 822     hab = (*env)->NewByteArray(env, len);
 823     if (hab != 0) {
 824         jclass strClazz = JNU_ClassString(env);
 825         CHECK_NULL_RETURN(strClazz, 0);
 826         (*env)->SetByteArrayRegion(env, hab, 0, len, (jbyte *)str);
 827         if (jnuEncodingSupported(env)) {
 828             result = (*env)->NewObject(env, strClazz,
 829                                        String_init_ID, hab, jnuEncoding);
 830         } else {
 831             /*If the encoding specified in sun.jnu.encoding is not endorsed
 832               by "Charset.isSupported" we have to fall back to use String(byte[])
 833               explicitly here without specifying the encoding name, in which the
 834               StringCoding class will pickup the iso-8859-1 as the fallback
 835               converter for us.
 836              */


 855 JNIEXPORT const char * JNICALL
 856 JNU_GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy)
 857 {
 858     char *result = NULL;
 859     jbyteArray hab = 0;
 860 
 861     if (isCopy)
 862         *isCopy = JNI_TRUE;
 863 
 864     if (fastEncoding == NO_ENCODING_YET) {
 865         initializeEncoding(env);
 866         JNU_CHECK_EXCEPTION_RETURN(env, 0);
 867     }
 868 
 869     if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
 870         return getString8859_1Chars(env, jstr);
 871     if (fastEncoding == FAST_646_US)
 872         return getString646_USChars(env, jstr);
 873     if (fastEncoding == FAST_CP1252)
 874         return getStringCp1252Chars(env, jstr);
 875     if (fastEncoding == FAST_UTF_8)
 876         return getStringUTF8(env, jstr);
 877 
 878     if ((*env)->EnsureLocalCapacity(env, 2) < 0)
 879         return 0;
 880 
 881     if (jnuEncodingSupported(env)) {
 882         hab = (*env)->CallObjectMethod(env, jstr, String_getBytes_ID, jnuEncoding);
 883     } else {
 884         jmethodID mid;
 885         jclass strClazz = JNU_ClassString(env);
 886         CHECK_NULL_RETURN(strClazz, 0);
 887         mid = (*env)->GetMethodID(env, strClazz,
 888                                        "getBytes", "()[B");
 889         if (mid != NULL) {
 890             hab = (*env)->CallObjectMethod(env, jstr, mid);
 891         }
 892     }
 893 
 894     if (!(*env)->ExceptionCheck(env)) {
 895         jint len = (*env)->GetArrayLength(env, hab);
 896         result = MALLOC_MIN4(len);


< prev index next >