{
char *rtn =NULL;
jsize len= env->GetArrayLength(bytes);
jbyte *arrayBody =env->GetByteArrayElements(bytes,0);
if(len>0)
{
rtn= newchar[len+1];//(char*)malloc(len+1);
memcpy(rtn,arrayBody,len);
rtn[len]=0;
}
env->ReleaseByteArrayElements(bytes,arrayBody,0);
returnrtn;
}
jstring stoJstring(JNIEnv* env, const char* pat)
{
jclass strClass =env->FindClass("Ljava/lang/String;");
jmethodID ctorID =env->GetMethodID(strClass,"<init>","([BLjava/lang/String;)V");
jbyteArray bytes =env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0,strlen(pat), (jbyte*)pat);
jstring encoding =env->NewStringUTF("utf-8");
return(jstring)env->NewObject(strClass, ctorID, bytes,encoding);
}
char* jstringTostring(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring =env->FindClass("java/lang/String");
jstring strencode =env->NewStringUTF("utf-8");
jmethodID mid =env->GetMethodID(clsstring, "getBytes","(Ljava/lang/String;)[B");
jbyteArray barr=(jbyteArray)env->CallObjectMethod(jstr, mid,strencode);
jsize alen =env->GetArrayLength(barr);
jbyte* ba =env->GetByteArrayElements(barr, JNI_FALSE);
if (alen > 0)
{
rtn = (char*)malloc(alen +1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
env->ReleaseByteArrayElements(barr,ba, 0);
return rtn;
}
JNIEXPORT void JNICALL Java_cn_itcast_Test_Info_1in(JNIEnv *env,jobject obj, jbyteArray buf, jint len)
{
char *CBuf=jbyteArrayToChar(env,buf);
int MyLen=len;
unsigned charuBuf[1024];
memset(uBuf,0,sizeof(uBuf));
memcpy(uBuf,CBuf,strlen(CBuf)+1);
ydt_info_in(uBuf,&MyLen);
CBuf=(char*)uBuf;
jfieldIDj_len=env->GetFieldID(env->GetObjectClass(obj),"MyLen","I");
env->SetIntField(obj,j_len,MyLen);
jfieldIDj_arr=env->GetFieldID(env->GetObjectClass(obj),"MyArr","Ljava/lang/String;");
jstringMyString=stoJstring(env,CBuf);
env->SetObjectField(obj,j_arr,MyString);
}
JNIEXPORT void JNICALL Java_cn_itcast_Test_Info_1out(JNIEnv*env, jobject obj, jstring buf, jint len)
{
char *MyBuf=jstringTostring(env,buf);
int MyLen=len;
ydt_info_out(MyBuf,&MyLen);
jfieldIDj_len=env->GetFieldID(env->GetObjectClass(obj),"MyLen","I");
env->SetIntField(obj,j_len,MyLen);
jfieldIDj_arr=env->GetFieldID(env->GetObjectClass(obj),"MyArr","Ljava/lang/String;");
jstring MyString=stoJstring(env,MyBuf);
env->SetObjectField(obj,j_arr,MyString);
}