jstring String_createJavaString(text* t) { jstring result = 0; if(t != 0) { char* utf8; char* src = VARDATA(t); int srcLen = VARSIZE(t) - VARHDRSZ; if(srcLen == 0) return 0; /* Would be nice if a direct conversion to UTF16 was provided. */ utf8 = (char*)pg_do_encoding_conversion((unsigned char*)src, srcLen, GetDatabaseEncoding(), PG_UTF8); result = JNI_newStringUTF(utf8); /* pg_do_encoding_conversion will return the source argument * when no conversion is required. We don't want to accidentally * free that pointer. */ if(utf8 != src) pfree(utf8); } return result; }
jstring String_createJavaStringFromNTS(const char* cp) { jstring result = 0; if(cp != 0) { /* Would be nice if a direct conversion to UTF16 was provided. */ char* utf8 = (char*)pg_do_encoding_conversion((unsigned char*)cp, strlen(cp), GetDatabaseEncoding(), PG_UTF8); result = JNI_newStringUTF(utf8); /* pg_do_encoding_conversion will return the source argument * when no conversion is required. We don't want to accidentally * free that pointer. */ if(utf8 != cp) pfree(utf8); } return result; }
static void String_initialize_codec() { jmethodID string_intern = PgObject_getJavaMethod(s_String_class, "intern", "()Ljava/lang/String;"); jstring empty = JNI_newStringUTF( ""); jclass scharset_class = PgObject_getJavaClass("java/nio/charset/StandardCharsets"); jfieldID scharset_UTF_8 = PgObject_getStaticJavaField(scharset_class, "UTF_8", "Ljava/nio/charset/Charset;"); jobject u8cs = JNI_getStaticObjectField(scharset_class, scharset_UTF_8); jclass charset_class = JNI_getObjectClass(u8cs); jmethodID charset_newDecoder = PgObject_getJavaMethod(charset_class, "newDecoder", "()Ljava/nio/charset/CharsetDecoder;"); jmethodID charset_newEncoder = PgObject_getJavaMethod(charset_class, "newEncoder", "()Ljava/nio/charset/CharsetEncoder;"); jclass decoder_class = PgObject_getJavaClass("java/nio/charset/CharsetDecoder"); jclass encoder_class = PgObject_getJavaClass("java/nio/charset/CharsetEncoder"); jmethodID encoder_abpc = PgObject_getJavaMethod(encoder_class, "averageBytesPerChar", "()F"); jclass result_class = PgObject_getJavaClass("java/nio/charset/CoderResult"); jfieldID overflow = PgObject_getStaticJavaField(result_class, "OVERFLOW", "Ljava/nio/charset/CoderResult;"); jfieldID underflow = PgObject_getStaticJavaField(result_class, "UNDERFLOW", "Ljava/nio/charset/CoderResult;"); jclass buffer_class = PgObject_getJavaClass("java/nio/Buffer"); s_CharsetDecoder_instance = JNI_newGlobalRef(JNI_callObjectMethod(u8cs, charset_newDecoder)); s_CharsetEncoder_instance = JNI_newGlobalRef(JNI_callObjectMethod(u8cs, charset_newEncoder)); s_CharsetDecoder_decode = PgObject_getJavaMethod(decoder_class, "decode", "(Ljava/nio/ByteBuffer;)Ljava/nio/CharBuffer;"); s_CharsetEncoder_encode = PgObject_getJavaMethod(encoder_class, "encode", "(Ljava/nio/CharBuffer;Ljava/nio/ByteBuffer;Z)" "Ljava/nio/charset/CoderResult;"); s_CharsetEncoder_averageBytesPerChar = JNI_callFloatMethod(s_CharsetEncoder_instance, encoder_abpc); s_CoderResult_OVERFLOW = JNI_newGlobalRef( JNI_getStaticObjectField(result_class, overflow)); s_CoderResult_UNDERFLOW = JNI_newGlobalRef( JNI_getStaticObjectField(result_class, underflow)); s_CoderResult_throwException = PgObject_getJavaMethod(result_class, "throwException", "()V"); s_CharBuffer_class = (jclass)JNI_newGlobalRef( PgObject_getJavaClass("java/nio/CharBuffer")); s_CharBuffer_wrap = PgObject_getStaticJavaMethod(s_CharBuffer_class, "wrap", "(Ljava/lang/CharSequence;)Ljava/nio/CharBuffer;"); s_Buffer_position = PgObject_getJavaMethod(buffer_class, "position", "()I"); s_Buffer_remaining = PgObject_getJavaMethod(buffer_class, "remaining", "()I"); s_the_empty_string = JNI_newGlobalRef( JNI_callObjectMethod(empty, string_intern)); s_server_encoding = GetDatabaseEncoding(); s_two_step_conversion = PG_UTF8 != s_server_encoding; uninitialized = false; }