@@ -639,21 +639,6 @@ static jmethodID String_getBytes_ID; /* String.getBytes(enc) */
639
639
static jfieldID String_coder_ID ; /* String.coder */
640
640
static jfieldID String_value_ID ; /* String.value */
641
641
642
- static jboolean isJNUEncodingSupported = JNI_FALSE ;
643
- static jboolean jnuEncodingSupported (JNIEnv * env ) {
644
- jboolean exe ;
645
- if (isJNUEncodingSupported == JNI_TRUE ) {
646
- return JNI_TRUE ;
647
- }
648
- isJNUEncodingSupported = (jboolean ) JNU_CallStaticMethodByName (
649
- env , & exe ,
650
- "java/nio/charset/Charset" ,
651
- "isSupported" ,
652
- "(Ljava/lang/String;)Z" ,
653
- jnuEncoding ).z ;
654
- return isJNUEncodingSupported ;
655
- }
656
-
657
642
/* Create a new string by converting str to a heap-allocated byte array and
658
643
* calling the appropriate String constructor.
659
644
*/
@@ -671,22 +656,8 @@ newSizedStringJava(JNIEnv *env, const char *str, const int len)
671
656
jclass strClazz = JNU_ClassString (env );
672
657
CHECK_NULL_RETURN (strClazz , 0 );
673
658
(* env )-> SetByteArrayRegion (env , bytes , 0 , len , (jbyte * )str );
674
- if (jnuEncodingSupported (env )) {
675
- result = (* env )-> NewObject (env , strClazz ,
676
- String_init_ID , bytes , jnuEncoding );
677
- } else {
678
- /*If the encoding specified in sun.jnu.encoding is not endorsed
679
- by "Charset.isSupported" we have to fall back to use String(byte[])
680
- explicitly here without specifying the encoding name, in which the
681
- StringCoding class will pickup the iso-8859-1 as the fallback
682
- converter for us.
683
- */
684
- jmethodID mid = (* env )-> GetMethodID (env , strClazz ,
685
- "<init>" , "([B)V" );
686
- if (mid != NULL ) {
687
- result = (* env )-> NewObject (env , strClazz , mid , bytes );
688
- }
689
- }
659
+ result = (* env )-> NewObject (env , strClazz ,
660
+ String_init_ID , bytes , jnuEncoding );
690
661
(* env )-> DeleteLocalRef (env , bytes );
691
662
return result ;
692
663
}
@@ -766,11 +737,30 @@ InitializeEncoding(JNIEnv *env, const char *encname)
766
737
strcmp (encname , "utf-16le" ) == 0 ) {
767
738
fastEncoding = FAST_CP1252 ;
768
739
} else {
740
+ jboolean exe ;
769
741
jstring enc = (* env )-> NewStringUTF (env , encname );
770
742
if (enc == NULL )
771
743
return ;
772
- fastEncoding = NO_FAST_ENCODING ;
773
- jnuEncoding = (jstring )(* env )-> NewGlobalRef (env , enc );
744
+
745
+ if ((jboolean ) JNU_CallStaticMethodByName (
746
+ env , & exe ,
747
+ "java/nio/charset/Charset" ,
748
+ "isSupported" ,
749
+ "(Ljava/lang/String;)Z" ,
750
+ enc ).z == JNI_TRUE ) {
751
+ fastEncoding = NO_FAST_ENCODING ;
752
+ jnuEncoding = (jstring )(* env )-> NewGlobalRef (env , enc );
753
+ } else {
754
+ // jnuEncoding falls back to UTF-8
755
+ jstring utf8 = (* env )-> NewStringUTF (env , "UTF-8" );
756
+ if (utf8 == NULL ) {
757
+ (* env )-> DeleteLocalRef (env , enc );
758
+ return ;
759
+ }
760
+ fastEncoding = FAST_UTF_8 ;
761
+ jnuEncoding = (jstring )(* env )-> NewGlobalRef (env , utf8 );
762
+ (* env )-> DeleteLocalRef (env , utf8 );
763
+ }
774
764
(* env )-> DeleteLocalRef (env , enc );
775
765
}
776
766
} else {
@@ -822,32 +812,22 @@ static const char* getStringBytes(JNIEnv *env, jstring jstr) {
822
812
if ((* env )-> EnsureLocalCapacity (env , 2 ) < 0 )
823
813
return 0 ;
824
814
825
- if (jnuEncodingSupported (env )) {
826
- hab = (* env )-> CallObjectMethod (env , jstr , String_getBytes_ID , jnuEncoding );
827
- } else {
828
- jmethodID mid ;
829
- jclass strClazz = JNU_ClassString (env );
830
- CHECK_NULL_RETURN (strClazz , 0 );
831
- mid = (* env )-> GetMethodID (env , strClazz ,
832
- "getBytes" , "()[B" );
833
- if (mid != NULL ) {
834
- hab = (* env )-> CallObjectMethod (env , jstr , mid );
815
+ hab = (* env )-> CallObjectMethod (env , jstr , String_getBytes_ID , jnuEncoding );
816
+ if (hab != 0 ) {
817
+ if (!(* env )-> ExceptionCheck (env )) {
818
+ jint len = (* env )-> GetArrayLength (env , hab );
819
+ result = MALLOC_MIN4 (len );
820
+ if (result == 0 ) {
821
+ JNU_ThrowOutOfMemoryError (env , 0 );
822
+ (* env )-> DeleteLocalRef (env , hab );
823
+ return 0 ;
824
+ }
825
+ (* env )-> GetByteArrayRegion (env , hab , 0 , len , (jbyte * )result );
826
+ result [len ] = 0 ; /* NULL-terminate */
835
827
}
836
- }
837
828
838
- if (!(* env )-> ExceptionCheck (env )) {
839
- jint len = (* env )-> GetArrayLength (env , hab );
840
- result = MALLOC_MIN4 (len );
841
- if (result == 0 ) {
842
- JNU_ThrowOutOfMemoryError (env , 0 );
843
- (* env )-> DeleteLocalRef (env , hab );
844
- return 0 ;
845
- }
846
- (* env )-> GetByteArrayRegion (env , hab , 0 , len , (jbyte * )result );
847
- result [len ] = 0 ; /* NULL-terminate */
829
+ (* env )-> DeleteLocalRef (env , hab );
848
830
}
849
-
850
- (* env )-> DeleteLocalRef (env , hab );
851
831
return result ;
852
832
}
853
833
0 commit comments