mirror of
https://github.com/moparisthebest/curl
synced 2024-12-21 23:58:49 -05:00
now it works
This commit is contained in:
parent
5eafb69bdb
commit
d7b54eb835
@ -11,6 +11,7 @@ struct javacurl {
|
|||||||
struct writecallback {
|
struct writecallback {
|
||||||
jmethodID mid;
|
jmethodID mid;
|
||||||
JNIEnv *java;
|
JNIEnv *java;
|
||||||
|
jclass cls; /* global reference */
|
||||||
jobject object;
|
jobject object;
|
||||||
} write;
|
} write;
|
||||||
};
|
};
|
||||||
@ -43,11 +44,18 @@ JNIEXPORT void JNICALL Java_CurlGlue_jni_1cleanup(JNIEnv *java,
|
|||||||
jobject myself,
|
jobject myself,
|
||||||
jint jcurl)
|
jint jcurl)
|
||||||
{
|
{
|
||||||
void *handle=(void *)((struct javacurl*)jcurl)->libcurl;
|
|
||||||
|
|
||||||
curl_easy_cleanup(handle); /* cleanup libcurl stuff */
|
struct javacurl *curl = (struct javacurl*)jcurl;
|
||||||
|
|
||||||
free((void *)jcurl); /* free the struct too */
|
if(curl->write.cls) {
|
||||||
|
/* a global reference we must delete */
|
||||||
|
(*java)->DeleteGlobalRef(java, curl->write.cls);
|
||||||
|
(*java)->DeleteGlobalRef(java, curl->write.object);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_easy_cleanup(curl->libcurl); /* cleanup libcurl stuff */
|
||||||
|
|
||||||
|
free((void *)curl); /* free the struct too */
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -91,8 +99,9 @@ static int javacurl_write_callback(void *ptr,
|
|||||||
jbyteArray jb=NULL;
|
jbyteArray jb=NULL;
|
||||||
int ret=0;
|
int ret=0;
|
||||||
|
|
||||||
fprintf(stderr, "%d bytes data received in callback, ptr %p, java =%p\n",
|
fprintf(stderr, "%d bytes data received in callback:\n"
|
||||||
realsize, curl, java);
|
"ptr=%p, java=%p cls=%p\n",
|
||||||
|
realsize, curl, java, curl->write.cls);
|
||||||
|
|
||||||
jb=(*java)->NewByteArray(java, realsize);
|
jb=(*java)->NewByteArray(java, realsize);
|
||||||
(*java)->SetByteArrayRegion(java, jb, 0,
|
(*java)->SetByteArrayRegion(java, jb, 0,
|
||||||
@ -117,25 +126,38 @@ static int javacurl_write_callback(void *ptr,
|
|||||||
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__IILCurlWrite_2
|
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__IILCurlWrite_2
|
||||||
(JNIEnv *java, jobject myself, jint jcurl, jint option, jobject object)
|
(JNIEnv *java, jobject myself, jint jcurl, jint option, jobject object)
|
||||||
{
|
{
|
||||||
jclass cls = (*java)->GetObjectClass(java, object);
|
jclass cls_local = (*java)->GetObjectClass(java, object);
|
||||||
jmethodID mid;
|
jmethodID mid;
|
||||||
struct javacurl *curl = (struct javacurl *)jcurl;
|
struct javacurl *curl = (struct javacurl *)jcurl;
|
||||||
|
jclass cls;
|
||||||
printf("setopt int + object, option = %d cls= %p\n", option, cls);
|
jobject obj_global;
|
||||||
|
|
||||||
switch(option) {
|
switch(option) {
|
||||||
case CURLOPT_WRITEFUNCTION:
|
case CURLOPT_WRITEFUNCTION:
|
||||||
|
/* this makes a reference that'll be alive until we kill it! */
|
||||||
|
cls = (*java)->NewGlobalRef(java, cls_local);
|
||||||
|
|
||||||
|
printf("setopt int + object, option = %d cls= %p\n",
|
||||||
|
option, cls);
|
||||||
|
|
||||||
|
if(!cls) {
|
||||||
|
puts("couldn't make local reference global");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* this is the write callback */
|
/* this is the write callback */
|
||||||
mid = (*java)->GetMethodID(java, cls, "handleString",
|
mid = (*java)->GetMethodID(java, cls, "handleString", "([B)I");
|
||||||
"([B)I");
|
|
||||||
if(!mid) {
|
if(!mid) {
|
||||||
/* no callback method found */
|
|
||||||
puts("no callback method found");
|
puts("no callback method found");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
obj_global = (*java)->NewGlobalRef(java, object);
|
||||||
|
|
||||||
curl->write.mid = mid;
|
curl->write.mid = mid;
|
||||||
curl->write.java = java;
|
curl->write.cls = cls;
|
||||||
curl->write.object = object;
|
curl->write.object = obj_global;
|
||||||
|
/*curl->write.java = java; stored on perform */
|
||||||
|
|
||||||
fprintf(stderr, "setopt write callback and write file pointer %p, java = %p\n",
|
fprintf(stderr, "setopt write callback and write file pointer %p, java = %p\n",
|
||||||
curl, java);
|
curl, java);
|
||||||
@ -144,6 +166,7 @@ JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__IILCurlWrite_2
|
|||||||
javacurl_write_callback);
|
javacurl_write_callback);
|
||||||
curl_easy_setopt(curl->libcurl, CURLOPT_FILE,
|
curl_easy_setopt(curl->libcurl, CURLOPT_FILE,
|
||||||
curl);
|
curl);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -158,6 +181,7 @@ JNIEXPORT jint JNICALL Java_CurlGlue_getinfo
|
|||||||
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1perform
|
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1perform
|
||||||
(JNIEnv *java, jobject myself, jint jcurl)
|
(JNIEnv *java, jobject myself, jint jcurl)
|
||||||
{
|
{
|
||||||
void *handle=(void *)((struct javacurl*)jcurl)->libcurl;
|
struct javacurl *curl=(struct javacurl*)jcurl;
|
||||||
return (jint)curl_easy_perform(handle);
|
curl->write.java = java;
|
||||||
|
return (jint)curl_easy_perform(curl->libcurl);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user