< prev index next >

src/hotspot/share/classfile/javaClasses.cpp

Print this page
rev 50331 : 8198285: More consistent Access API for arraycopy
rev 50332 : [mq]: JDK-8203232-2.patch
rev 50333 : [mq]: JDK-8198285-3.patch


 293 
 294 oop java_lang_String::create_oop_from_unicode(jchar* unicode, int length, TRAPS) {
 295   Handle h_obj = create_from_unicode(unicode, length, CHECK_0);
 296   return h_obj();
 297 }
 298 
 299 Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) {
 300   if (utf8_str == NULL) {
 301     return Handle();
 302   }
 303   bool has_multibyte, is_latin1;
 304   int length = UTF8::unicode_length(utf8_str, is_latin1, has_multibyte);
 305   if (!CompactStrings) {
 306     has_multibyte = true;
 307     is_latin1 = false;
 308   }
 309 
 310   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 311   if (length > 0) {
 312     if (!has_multibyte) {
 313       ArrayAccess<>::arraycopy_from_native<jbyte>(reinterpret_cast<const jbyte*>(utf8_str), value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length);
 314     } else if (is_latin1) {
 315       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 316     } else {
 317       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 318     }
 319   }
 320 
 321 #ifdef ASSERT
 322   // This check is too strict because the input string is not necessarily valid UTF8.
 323   // For example, it may be created with arbitrary content via jni_NewStringUTF.
 324   /*
 325   {
 326     ResourceMark rm;
 327     const char* expected = utf8_str;
 328     char* actual = as_utf8_string(h_obj());
 329     if (strcmp(expected, actual) != 0) {
 330       tty->print_cr("String conversion failure: %s --> %s", expected, actual);
 331       ShouldNotReachHere();
 332     }
 333   }


 339 
 340 oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) {
 341   Handle h_obj = create_from_str(utf8_str, CHECK_0);
 342   return h_obj();
 343 }
 344 
 345 Handle java_lang_String::create_from_symbol(Symbol* symbol, TRAPS) {
 346   const char* utf8_str = (char*)symbol->bytes();
 347   int utf8_len = symbol->utf8_length();
 348 
 349   bool has_multibyte, is_latin1;
 350   int length = UTF8::unicode_length(utf8_str, utf8_len, is_latin1, has_multibyte);
 351   if (!CompactStrings) {
 352     has_multibyte = true;
 353     is_latin1 = false;
 354   }
 355 
 356   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 357   if (length > 0) {
 358     if (!has_multibyte) {
 359       ArrayAccess<>::arraycopy_from_native<jbyte>(reinterpret_cast<const jbyte*>(utf8_str), value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length);
 360     } else if (is_latin1) {
 361       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 362     } else {
 363       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 364     }
 365   }
 366 
 367 #ifdef ASSERT
 368   {
 369     ResourceMark rm;
 370     const char* expected = symbol->as_utf8();
 371     char* actual = as_utf8_string(h_obj());
 372     if (strncmp(expected, actual, utf8_len) != 0) {
 373       tty->print_cr("Symbol conversion failure: %s --> %s", expected, actual);
 374       ShouldNotReachHere();
 375     }
 376   }
 377 #endif
 378 
 379   return h_obj;




 293 
 294 oop java_lang_String::create_oop_from_unicode(jchar* unicode, int length, TRAPS) {
 295   Handle h_obj = create_from_unicode(unicode, length, CHECK_0);
 296   return h_obj();
 297 }
 298 
 299 Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) {
 300   if (utf8_str == NULL) {
 301     return Handle();
 302   }
 303   bool has_multibyte, is_latin1;
 304   int length = UTF8::unicode_length(utf8_str, is_latin1, has_multibyte);
 305   if (!CompactStrings) {
 306     has_multibyte = true;
 307     is_latin1 = false;
 308   }
 309 
 310   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 311   if (length > 0) {
 312     if (!has_multibyte) {
 313       ArrayAccess<>::arraycopy_from_native<>(reinterpret_cast<const jbyte*>(utf8_str), value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length);
 314     } else if (is_latin1) {
 315       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 316     } else {
 317       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 318     }
 319   }
 320 
 321 #ifdef ASSERT
 322   // This check is too strict because the input string is not necessarily valid UTF8.
 323   // For example, it may be created with arbitrary content via jni_NewStringUTF.
 324   /*
 325   {
 326     ResourceMark rm;
 327     const char* expected = utf8_str;
 328     char* actual = as_utf8_string(h_obj());
 329     if (strcmp(expected, actual) != 0) {
 330       tty->print_cr("String conversion failure: %s --> %s", expected, actual);
 331       ShouldNotReachHere();
 332     }
 333   }


 339 
 340 oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) {
 341   Handle h_obj = create_from_str(utf8_str, CHECK_0);
 342   return h_obj();
 343 }
 344 
 345 Handle java_lang_String::create_from_symbol(Symbol* symbol, TRAPS) {
 346   const char* utf8_str = (char*)symbol->bytes();
 347   int utf8_len = symbol->utf8_length();
 348 
 349   bool has_multibyte, is_latin1;
 350   int length = UTF8::unicode_length(utf8_str, utf8_len, is_latin1, has_multibyte);
 351   if (!CompactStrings) {
 352     has_multibyte = true;
 353     is_latin1 = false;
 354   }
 355 
 356   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 357   if (length > 0) {
 358     if (!has_multibyte) {
 359       ArrayAccess<>::arraycopy_from_native<>(reinterpret_cast<const jbyte*>(utf8_str), value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length);
 360     } else if (is_latin1) {
 361       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 362     } else {
 363       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 364     }
 365   }
 366 
 367 #ifdef ASSERT
 368   {
 369     ResourceMark rm;
 370     const char* expected = symbol->as_utf8();
 371     char* actual = as_utf8_string(h_obj());
 372     if (strncmp(expected, actual, utf8_len) != 0) {
 373       tty->print_cr("Symbol conversion failure: %s --> %s", expected, actual);
 374       ShouldNotReachHere();
 375     }
 376   }
 377 #endif
 378 
 379   return h_obj;


< prev index next >