+/* javastring_new_from_utf_buffer **********************************************
+
+ Create a new object of type java/lang/String with the text from
+ the specified utf8 buffer.
+
+ IN:
+ buffer.......points to first char in the buffer
+ blength......number of bytes to read from the buffer
+
+ RETURN VALUE:
+ the java.lang.String object, or
+ NULL if an exception has been thrown
+
+*******************************************************************************/
+
+java_objectheader *javastring_new_from_utf_buffer(const char *buffer, u4 blength)
+{
+ const char *utf_ptr; /* current utf character in utf string */
+ u4 utflength; /* length of utf-string if uncompressed */
+ java_objectheader *o;
+ java_lang_String *s; /* result-string */
+ java_chararray *a;
+ u4 i;
+
+ assert(buffer);
+
+ utflength = utf_get_number_of_u2s_for_buffer(buffer,blength);
+
+ o = builtin_new(class_java_lang_String);
+ a = builtin_newarray_char(utflength);
+
+ /* javastring or character-array could not be created */
+
+ if ((o == NULL) || (a == NULL))
+ return NULL;
+
+ /* decompress utf-string */
+
+ utf_ptr = buffer;
+
+ for (i = 0; i < utflength; i++)
+ a->data[i] = utf_nextu2((char **) &utf_ptr);
+
+ /* set fields of the javastring-object */
+
+ s = (java_lang_String *) o;
+
+ s->value = a;
+ s->offset = 0;
+ s->count = utflength;
+
+ return o;
+}
+
+
+/* javastring_safe_new_from_utf8 ***********************************************
+
+ Create a new object of type java/lang/String with the text from
+ the specified UTF-8 string. This function is safe for invalid UTF-8.
+ (Invalid characters will be replaced by U+fffd.)
+
+ IN:
+ text.........the UTF-8 string, zero-terminated.
+
+ RETURN VALUE:
+ the java.lang.String object, or
+ NULL if an exception has been thrown
+
+*******************************************************************************/
+
+java_objectheader *javastring_safe_new_from_utf8(const char *text)
+{
+ java_objectheader *o;
+ java_chararray *a;
+ java_lang_String *s;
+ s4 nbytes;
+ s4 len;
+
+ assert(text);
+
+ /* Get number of bytes. We need this to completely emulate the messy */
+ /* behaviour of the RI. :( */
+
+ nbytes = strlen(text);
+
+ /* calculate number of Java characters */
+
+ len = utf8_safe_number_of_u2s(text, nbytes);
+
+ /* allocate the String object and the char array */
+
+ o = builtin_new(class_java_lang_String);
+ a = builtin_newarray_char(len);
+
+ /* javastring or character-array could not be created? */
+
+ if ((o == NULL) || (a == NULL))
+ return NULL;
+
+ /* decompress UTF-8 string */
+
+ utf8_safe_convert_to_u2s(text, nbytes, a->data);
+
+ /* set fields of the String object */
+
+ s = (java_lang_String *) o;
+
+ s->value = a;
+ s->offset = 0;
+ s->count = len;
+
+ return o;
+}
+
+
+/* javastring_new_from_utf_string **********************************************
+
+ Create a new object of type java/lang/String with the text from
+ the specified zero-terminated utf8 string.
+
+ IN:
+ buffer.......points to first char in the buffer
+ blength......number of bytes to read from the buffer
+
+ RETURN VALUE:
+ the java.lang.String object, or
+ NULL if an exception has been thrown
+
+*******************************************************************************/
+
+java_objectheader *javastring_new_from_utf_string(const char *utfstr)
+{
+ assert(utfstr);
+
+ return javastring_new_from_utf_buffer(utfstr, strlen(utfstr));
+}
+
+