/** * Create Java String object from the specified pcsl_string. * * @param pcsl_str pointer to the pcsl_string instance * @param java_str pointer to the Java String instance * @return status of the operation */ pcsl_string_status midp_jstring_from_pcsl_string(KNIDECLARGS const pcsl_string * pcsl_str, jstring java_str) { if (pcsl_str == NULL) { KNI_ReleaseHandle(java_str); return PCSL_STRING_EINVAL; } else { const jsize length = pcsl_string_utf16_length(pcsl_str); if (length < 0) { KNI_ReleaseHandle(java_str); return PCSL_STRING_EINVAL; } else { const jchar * buffer = pcsl_string_get_utf16_data(pcsl_str); if (buffer == NULL) { KNI_ReleaseHandle(java_str); return PCSL_STRING_ERR; } else { KNI_NewString(buffer, length, java_str); return PCSL_STRING_OK; } } } }
KNIEXPORT KNI_RETURNTYPE_OBJECT KNIDECL(KNITest_newString) { KNI_StartHandles(1); KNI_DeclareHandle(newStr); KNI_NewString(NULL, -1, newStr); KNI_EndHandlesAndReturnObject(newStr); }
static void readControlStringField(KNIDECLARGS kjobject objectHandle, jfieldID fieldID) { int len, i; jchar *data; (void) _arguments; (void) _p_mb; read(controlPipe[0], &len, sizeof(int)); data = (jchar*)midpMalloc(len * sizeof(jchar)); if (data != NULL) { read(controlPipe[0], data, len * sizeof(jchar)); } else { for (i=0; i<len; i++) { jchar dummy; read(controlPipe[0], &dummy, sizeof(jchar)); } len = 0; /* IMPL_NOTE: throw out of memory */ } KNI_StartHandles(1); KNI_DeclareHandle(stringObj); KNI_NewString(data, len, stringObj); KNI_SetObjectField(objectHandle, fieldID, stringObj); KNI_EndHandles(); }
/** * Transforms prepared result buffer to jstring object and release memory of * the allocated buffer. * It is safe to call this function after detecting out-of-memory error * provided that buf is set to _JSR211_RESULT_INITIALIZER_ */ static void result2string(_JSR211_INTERNAL_RESULT_BUFFER_* buf, jstring str) { if (buf->len > 0 && buf->buf != NULL) { KNI_NewString(buf->buf, buf->len, str); pcsl_mem_free(buf->buf); } else { KNI_ReleaseHandle(str); } buf->len = 0; buf->buf = NULL; }
KNIEXPORT KNI_RETURNTYPE_OBJECT KNIDECL(KNITest_getStringRegion) { jchar buf[20]; jint offset = KNI_GetParameterAsInt(2); jint len = KNI_GetParameterAsInt(3); KNI_StartHandles(2); KNI_DeclareHandle(str); KNI_DeclareHandle(newStr); KNI_GetParameterAsObject(1, str); KNI_GetStringRegion(str, offset, len, buf); KNI_NewString(buf, len, newStr); KNI_EndHandlesAndReturnObject(newStr); }
/** * Create Java platform String object from the specified javacall_utf16_string. * * @param utf16_str pointer to the javacall_utf16_string instance * @param java_str pointer to the Java platform String instance * @return status of the operation */ javacall_result jsrop_jstring_from_utf16_string(KNIDECLARGS const javacall_utf16_string utf16_str, jstring java_str) { javacall_result res = JAVACALL_FAIL; javacall_int32 string_length; if (JAVACALL_OK == javautil_unicode_utf16_chlength(utf16_str, &string_length)) { KNI_NewString(utf16_str, string_length, java_str); res = JAVACALL_OK; } return res; }
/** * Get one of supported device locales (by number). * <p> * Java declaration: * <pre> * getDevLocaleName(I)Ljava/lang/String * </pre> * * @param index index of locale to select * @return locale name */ KNIEXPORT KNI_RETURNTYPE_OBJECT KNIDECL(com_sun_j2me_global_DevResourceManagerFactory_getDevLocaleName) { jint len=MAX_LOCALE_LENGTH, res; jint index = KNI_GetParameterAsInt(1); jchar locale_name[MAX_LOCALE_LENGTH]; KNI_StartHandles(1); KNI_DeclareHandle(hloc); res = jsr238_get_resource_locale_name(index,locale_name,&len); if (res < 0 ) { KNI_ReleaseHandle(hloc); KNI_ThrowNew(jsropRuntimeException,"Get locale name"); } else { KNI_NewString(locale_name, len - 1, hloc); } KNI_EndHandlesAndReturnObject(hloc); }
/** * Get one of supported locales (by number). * <p> * Java declaration: * <pre> * getCollationLocale(I)Ljava/lang/String * </pre> * * @param index index of locale to select * @return locale */ KNIEXPORT KNI_RETURNTYPE_OBJECT Java_com_sun_j2me_global_CollationAbstractionLayerImpl_getCollationLocaleName() { jint len=MAX_LOCALE_LENGTH, res; jint index = KNI_GetParameterAsInt(1); jchar locale_name[MAX_LOCALE_LENGTH]; KNI_StartHandles(1); KNI_DeclareHandle(hloc); res = jsr238_get_collation_locale_name(index,locale_name,&len); if (res < 0 ) { KNI_ReleaseHandle(hloc); KNI_ThrowNew(midpRuntimeException,"Get locale name"); } else { KNI_NewString(locale_name, len - 1, hloc); } KNI_EndHandlesAndReturnObject(hloc); }
/** * Return the current predictive text completion option * * @param handle the handle of the iterator. If iterator handle is not valid * function returns null. * @param outStringLen max size of the outArray. If size is equal or less than * 0 function returns null * * @return next element in the iteration */ KNI_RETURNTYPE_OBJECT Java_com_sun_midp_chameleon_input_PTIteratorImpl_ptNextCompletionOption0() { jint result = 0; jint handle = 0; jint outStringLen; jchar nextCompletion[256] = {0}; jsize resultLength; KNI_StartHandles(1); KNI_DeclareHandle(string); handle = (int)KNI_GetParameterAsInt(1); outStringLen = (int)KNI_GetParameterAsInt(2) - 1; result = ptNextCompletionOption(handle, nextCompletion, outStringLen); if (result == KNI_TRUE) { for (resultLength = 0; nextCompletion[resultLength]; resultLength++) {} KNI_NewString(nextCompletion, resultLength, string); } else { KNI_ThrowNew(midpOutOfMemoryError, NULL); } KNI_EndHandlesAndReturnObject(string); }
/** * Get a hostname for the given raw IPv4 address. * <p> * Java declaration: * <pre> * static addrToString(I)Ljava/lang/String; * </pre> * * @param ipn raw IPv4 address * @return The hostname or <tt>ipn</tt> as a dotted-quad if * no hostname was found */ KNIEXPORT KNI_RETURNTYPE_OBJECT Java_com_sun_midp_io_j2me_datagram_Protocol_addrToString(void) { jint ipn; jchar* result; int resultLen; int status; ipn = KNI_GetParameterAsInt(1); status = pcsl_network_addrToString((unsigned char*)&ipn, &result, &resultLen); KNI_StartHandles(1); KNI_DeclareHandle(resultObj); if (PCSL_NET_SUCCESS == status) { KNI_NewString(result, (jsize)resultLen, resultObj); pcsl_mem_free(result); } else { KNI_ThrowNew(midpOutOfMemoryError, NULL); } KNI_EndHandlesAndReturnObject(resultObj); }
KNIEXPORT KNI_RETURNTYPE_OBJECT KNIDECL(com_sun_midp_chameleon_skins_resources_LoadedSkinData_readStringArray) { int arrayLength; int i; KNI_StartHandles(2); KNI_DeclareHandle(returnArray); KNI_DeclareHandle(stringHandle); do { /* * First, read array length */ ENSURE_SKIN_DATA_AVAILABILITY(sizeof(jint)); memcpy((void*)&arrayLength, (void*)gsSkinFileDataPos, sizeof(jint)); gsSkinFileDataPos += sizeof(jint); /* * Then create array */ SNI_NewArray(SNI_STRING_ARRAY, arrayLength, returnArray); if (KNI_IsNullHandle(returnArray)) { KNI_ThrowNew(midpOutOfMemoryError, NULL); break; } /* * And finally populate it with strings */ for (i = 0; i < arrayLength; ++i) { unsigned char dataLength; unsigned char encoding; /* read data length */ ENSURE_SKIN_DATA_AVAILABILITY(sizeof(char)); dataLength = *((unsigned char*)gsSkinFileDataPos); gsSkinFileDataPos += 1; /* read encoding */ ENSURE_SKIN_DATA_AVAILABILITY(sizeof(char)); encoding = *((unsigned char*)gsSkinFileDataPos); gsSkinFileDataPos += 1; ENSURE_SKIN_DATA_AVAILABILITY(dataLength * sizeof(char)); if (encoding == STRING_ENCODING_USASCII) { int j; /* * In case of USASCII encoding, each byte of * string data corresponds to one string char */ int stringLength = dataLength; /* use gKNIBuffer for storing string chars */ jchar* stringChars = (jchar*)gKNIBuffer; /* * Safety measure to prevent gKNIBuffer overflow * (which should never happens unless something is broken) */ if (stringLength > (int)(KNI_BUFFER_SIZE/sizeof(jchar))) { stringLength = (int)(KNI_BUFFER_SIZE/sizeof(jchar)); REPORT_WARN(LC_HIGHUI, "gKNIBuffer is too small for skin string"); } /* fill string chars array */ for (j = 0; j < stringLength; ++j) { stringChars[j] = gsSkinFileDataPos[j]; } /* and create string from it */ KNI_NewString(stringChars, stringLength, stringHandle); } else if (encoding == STRING_ENCODING_UTF8) { KNI_NewStringUTF((char*)gsSkinFileDataPos, stringHandle); } else { KNI_ThrowNew(midpIllegalStateException, "Illegal skin string encoding"); break; } KNI_SetObjectArrayElement(returnArray, i, stringHandle); gsSkinFileDataPos += dataLength; } } while (0); KNI_EndHandlesAndReturnObject(returnArray); }
/** * Copies the contents of fromMsg to the contents of toMsg. Both must be * instances of LinkMessage. The toLink object must be an instance of Link. * It's filled in if the contents of fromMsg are a Link. Returns KNI_TRUE if * successful, otherwise KNI_FALSE. */ static jboolean copy(jobject fromMsg, jobject toMsg, jobject toLink) { jboolean retval; KNI_StartHandles(6); KNI_DeclareHandle(byteArrayClass); KNI_DeclareHandle(stringClass); KNI_DeclareHandle(linkClass); KNI_DeclareHandle(fromContents); KNI_DeclareHandle(newString); KNI_DeclareHandle(newByteArray); KNI_FindClass("[B", byteArrayClass); KNI_FindClass("java/lang/String", stringClass); KNI_FindClass("com/sun/midp/links/Link", linkClass); getContents(fromMsg, fromContents); if (KNI_IsInstanceOf(fromContents, byteArrayClass)) { /* do a byte array copy */ jint fromOffset; jint fromLength; getRange(fromMsg, &fromOffset, &fromLength); SNI_NewArray(SNI_BYTE_ARRAY, fromLength, newByteArray); if (KNI_IsNullHandle(newByteArray)) { retval = KNI_FALSE; } else { KNI_GetRawArrayRegion(fromContents, fromOffset, fromLength, SNI_GetRawArrayPointer(newByteArray)); setContents(toMsg, newByteArray); setRange(toMsg, 0, fromLength); retval = KNI_TRUE; } } else if (KNI_IsInstanceOf(fromContents, stringClass)) { /* do a string copy */ jchar *buf; jsize slen = KNI_GetStringLength(fromContents); SNI_NewArray(SNI_BYTE_ARRAY, slen*sizeof(jchar), newByteArray); if (KNI_IsNullHandle(newByteArray)) { retval = KNI_FALSE; } else { buf = SNI_GetRawArrayPointer(newByteArray); KNI_GetStringRegion(fromContents, 0, slen, buf); KNI_NewString(buf, slen, newString); setContents(toMsg, newString); retval = KNI_TRUE; } } else if (KNI_IsInstanceOf(fromContents, linkClass)) { /* copy the link */ rendezvous *rp = getNativePointer(fromContents); setNativePointer(toLink, rp); rp_incref(rp); setContents(toMsg, toLink); retval = KNI_TRUE; } else { retval = KNI_FALSE; } KNI_EndHandles(); return retval; }
/** * Create Java platform String object from the specified javacall_utf16_string. * exactly utf16_chars_n characters are copied * utf16_str can contain any number of zero characters in it * * @param utf16_str pointer to the javacall_utf16_string instance * @param utf16_chars_n number of utf16 characters to copy.. * @param java_str pointer to the Java platform String instance * @return status of the operation */ javacall_result jsrop_jstring_from_utf16_string_n(KNIDECLARGS const javacall_utf16_string utf16_str, int utf16_chars_n, jstring java_str) { KNI_NewString(utf16_str, utf16_chars_n, java_str); return JAVACALL_OK; }