1999-09-02 07:36:53 +04:00
|
|
|
/*
|
1999-11-06 05:47:15 +03:00
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
1999-09-02 07:36:53 +04:00
|
|
|
*
|
1999-11-06 05:47:15 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
1999-09-02 07:36:53 +04:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Sun Microsystems,
|
1999-11-06 05:47:15 +03:00
|
|
|
* Inc. Portions created by Sun are
|
|
|
|
* Copyright (C) 1999 Sun Microsystems, Inc. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-09-02 07:36:53 +04:00
|
|
|
*/
|
1999-11-21 02:19:40 +03:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
1999-09-02 07:36:53 +04:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "org_mozilla_pluglet_mozilla_PlugletInputStream.h"
|
2000-01-18 05:53:58 +03:00
|
|
|
#include "PlugletLog.h"
|
1999-09-02 07:36:53 +04:00
|
|
|
|
|
|
|
static jfieldID peerFID = NULL;
|
|
|
|
/*
|
|
|
|
* Class: org_mozilla_pluglet_mozilla_PlugletInputStream
|
|
|
|
* Method: available
|
|
|
|
* Signature: ()I
|
|
|
|
*/
|
|
|
|
JNIEXPORT jint JNICALL Java_org_mozilla_pluglet_mozilla_PlugletInputStream_available
|
|
|
|
(JNIEnv *env, jobject jthis) {
|
2000-01-18 05:53:58 +03:00
|
|
|
nsIInputStream * input = (nsIInputStream*)env->GetLongField(jthis, peerFID);
|
1999-09-02 07:36:53 +04:00
|
|
|
PRUint32 res = 0;
|
|
|
|
if(input) {
|
2000-01-18 05:53:58 +03:00
|
|
|
PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
|
|
|
|
("PlugletInputStream.available: stream = %p\n", input));
|
1999-09-12 03:47:47 +04:00
|
|
|
input->Available(&res);
|
1999-09-02 07:36:53 +04:00
|
|
|
}
|
|
|
|
return (jint)res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_mozilla_pluglet_mozilla_PlugletInputStream
|
|
|
|
* Method: close
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_mozilla_pluglet_mozilla_PlugletInputStream_close
|
|
|
|
(JNIEnv *env, jobject jthis) {
|
|
|
|
nsIInputStream * input = (nsIInputStream*)env->GetLongField(jthis, peerFID);
|
|
|
|
if (input) {
|
2000-01-18 05:53:58 +03:00
|
|
|
PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
|
|
|
|
("PlugletInputStream.close: stream = %p\n", input));
|
1999-09-02 07:36:53 +04:00
|
|
|
NS_RELEASE(input);
|
|
|
|
env->SetLongField(jthis, peerFID,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1999-09-08 04:21:03 +04:00
|
|
|
|
1999-09-02 07:36:53 +04:00
|
|
|
/*
|
|
|
|
* Class: org_mozilla_pluglet_mozilla_PlugletInputStream
|
1999-09-08 04:21:03 +04:00
|
|
|
* Method: nativeRead
|
|
|
|
* Signature: ([BII)I
|
1999-09-02 07:36:53 +04:00
|
|
|
*/
|
1999-09-08 04:21:03 +04:00
|
|
|
JNIEXPORT jint JNICALL Java_org_mozilla_pluglet_mozilla_PlugletInputStream_nativeRead
|
|
|
|
(JNIEnv *env, jobject jthis, jbyteArray b, jint off, jint len) {
|
2000-11-03 02:33:21 +03:00
|
|
|
jint retval = -1;
|
1999-09-02 07:36:53 +04:00
|
|
|
nsIInputStream * input = (nsIInputStream*)env->GetLongField(jthis, peerFID);
|
2000-01-18 05:53:58 +03:00
|
|
|
if (input) {
|
|
|
|
PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
|
|
|
|
("PlugletInputStream.nativeRead: stream = %p, off = %i, len = %i\n", input, off, len));
|
|
|
|
}
|
|
|
|
|
1999-09-02 07:36:53 +04:00
|
|
|
if (env->ExceptionOccurred()) {
|
|
|
|
return retval;
|
|
|
|
}
|
1999-12-03 02:20:29 +03:00
|
|
|
if (!input) {
|
|
|
|
jclass ex = env->FindClass("java/io/IOException");
|
|
|
|
if (!ex) {
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
env->ThrowNew(ex,"Stream was closed");
|
|
|
|
return retval;
|
|
|
|
}
|
1999-09-08 04:21:03 +04:00
|
|
|
jbyte * bufElems = (jbyte*) malloc(sizeof(jbyte)*len);
|
|
|
|
if (!bufElems) {
|
1999-09-02 07:36:53 +04:00
|
|
|
return retval;
|
1999-09-08 04:21:03 +04:00
|
|
|
//nb throw OutOfMemory
|
1999-09-02 07:36:53 +04:00
|
|
|
}
|
|
|
|
nsresult res;
|
2000-11-03 02:33:21 +03:00
|
|
|
PRUint32 actualLen;
|
|
|
|
res = input->Read((char*)bufElems,(PRUint32)len,&actualLen);
|
1999-09-02 07:36:53 +04:00
|
|
|
if (NS_FAILED(res)) {
|
1999-09-08 04:21:03 +04:00
|
|
|
free(bufElems);
|
1999-09-02 07:36:53 +04:00
|
|
|
return retval;
|
|
|
|
}
|
2000-11-03 02:33:21 +03:00
|
|
|
retval = actualLen;
|
2000-01-18 05:53:58 +03:00
|
|
|
PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
|
|
|
|
("PlugletInputStream.nativeRead: %i bytes read\n", retval));
|
|
|
|
|
2000-01-19 01:44:47 +03:00
|
|
|
env->SetByteArrayRegion(b,off,retval,bufElems);
|
1999-09-08 04:21:03 +04:00
|
|
|
free(bufElems);
|
2000-11-03 02:33:21 +03:00
|
|
|
return (len > 0 && actualLen <= 0) ? -1 : retval;
|
1999-09-02 07:36:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_mozilla_pluglet_mozilla_PlugletInputStream
|
|
|
|
* Method: nativeFinalize
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_mozilla_pluglet_mozilla_PlugletInputStream_nativeFinalize
|
|
|
|
(JNIEnv *env, jobject jthis) {
|
|
|
|
/* nb do as in java-dom stuff */
|
|
|
|
nsIInputStream * input = (nsIInputStream*)env->GetLongField(jthis, peerFID);
|
|
|
|
if(input) {
|
2000-01-18 05:53:58 +03:00
|
|
|
PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
|
|
|
|
("PlugletInputStream.nativeFinalize: stream = %p\n", input));
|
1999-09-02 07:36:53 +04:00
|
|
|
NS_RELEASE(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_mozilla_pluglet_mozilla_PlugletInputStream
|
|
|
|
* Method: nativeInitialize
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_mozilla_pluglet_mozilla_PlugletInputStream_nativeInitialize
|
|
|
|
(JNIEnv *env, jobject jthis) {
|
|
|
|
if(!peerFID) {
|
|
|
|
peerFID = env->GetFieldID(env->GetObjectClass(jthis),"peer","J");
|
|
|
|
if (!peerFID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsIInputStream * input = (nsIInputStream*)env->GetLongField(jthis, peerFID);
|
2000-01-18 05:53:58 +03:00
|
|
|
|
1999-09-02 07:36:53 +04:00
|
|
|
if (input) {
|
2000-01-18 05:53:58 +03:00
|
|
|
PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
|
|
|
|
("PlugletInputStream.nativeInitialize: stream = %p\n", input));
|
1999-09-02 07:36:53 +04:00
|
|
|
input->AddRef();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|