PlatformString::PlatformString(JNIEnv *env, jstring value) {
    initialize();

    if (env != NULL) {
        const char* lvalue = env->GetStringUTFChars(value, JNI_FALSE);

        if (lvalue == NULL || env->ExceptionCheck() == JNI_TRUE) {
            throw JavaException();
        }

        if (lvalue != NULL) {
            FLength = env->GetStringUTFLength(value);

            if (env->ExceptionCheck() == JNI_TRUE) {
                throw JavaException();
            }

            FData = new char[FLength + 1];
            PlatformString::CopyString(FData, FLength + 1, lvalue);

            env->ReleaseStringUTFChars(value, lvalue);

            if (env->ExceptionCheck() == JNI_TRUE) {
                throw JavaException();
            }
        }
    }
}
示例#2
0
文件: Java.cpp 项目: loveyoupeng/rt
void JavaStringArray::SetValue(jsize Index, jstring Item) {
    FEnv->SetObjectArrayElement(FData, Index, Item);

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        throw JavaException(FEnv, _T("Error"));
    }
}
示例#3
0
文件: Java.cpp 项目: loveyoupeng/rt
JavaClass::~JavaClass() {
    FEnv->DeleteLocalRef(FClass);

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        throw JavaException(FEnv, _T("Error"));
    }
}
示例#4
0
void check_java_exception(JNIEnv *env) {
    jthrowable object = env->ExceptionOccurred();
    if (object) {
        env->ExceptionClear();
        throw JavaException(object);
    }
}
示例#5
0
文件: Java.cpp 项目: loveyoupeng/rt
void JavaStringArray::Initialize(size_t Size) {
    JavaClass jstringClass(FEnv, _T("java/lang/String"));

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        Messages& messages = Messages::GetInstance();
        TString message = messages.GetMessage(CLASS_NOT_FOUND);
        message = PlatformString::Format(message, _T("String"));
        throw JavaException(FEnv, message.data());
    }

    jstring str = PlatformString("").toJString(FEnv);
    FData = (jobjectArray)FEnv->NewObjectArray((jsize)Size, jstringClass, str);

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        throw JavaException(FEnv, _T("Error"));
    }
}
示例#6
0
文件: Java.cpp 项目: loveyoupeng/rt
unsigned int JavaStringArray::Count() {
    unsigned int result = FEnv->GetArrayLength(FData);

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        throw JavaException(FEnv, _T("Error"));
    }

    return result;
}
示例#7
0
文件: Java.cpp 项目: loveyoupeng/rt
jstring JavaStringArray::GetValue(jsize Index) {
    jstring result = (jstring)FEnv->GetObjectArrayElement(FData, Index);

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        throw JavaException(FEnv, _T("Error"));
    }

    return result;
}
示例#8
0
文件: Java.cpp 项目: loveyoupeng/rt
void JavaMethod::CallVoidMethod(int Count, ...) {
    va_list args;
    va_start(args, Count);
    FEnv->CallVoidMethodV(FObj, FMethod, args);
    va_end(args);

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        Messages& messages = Messages::GetInstance();
        throw JavaException(FEnv, messages.GetMessage(ERROR_INVOKING_METHOD));
    }
}
示例#9
0
文件: Java.cpp 项目: loveyoupeng/rt
JavaStaticMethod JavaClass::GetStaticMethod(TString Name, TString Signature) {
    jmethodID method = FEnv->GetStaticMethodID(FClass, PlatformString(Name), PlatformString(Signature));

    if (method == NULL || FEnv->ExceptionCheck() == JNI_TRUE) {
        Messages& messages = Messages::GetInstance();
        TString message = messages.GetMessage(METHOD_NOT_FOUND);
        message = PlatformString::Format(message, Name.data(), FClassName.data());
        throw JavaException(FEnv, message);
    }

    return JavaStaticMethod(FEnv, FClass, method);
}
示例#10
0
文件: Java.cpp 项目: loveyoupeng/rt
JavaClass::JavaClass(JNIEnv *Env, TString Name) {
    FEnv = Env;
    FClassName = Name;
    FClass = FEnv->FindClass(PlatformString(FClassName));

    if (FClass == NULL || FEnv->ExceptionCheck() == JNI_TRUE) {
        Messages& messages = Messages::GetInstance();
        TString message = messages.GetMessage(CLASS_NOT_FOUND);
        message = PlatformString::Format(message, FClassName.data());
        throw JavaException(FEnv, message);
    }
}
jstring PlatformString::toJString(JNIEnv *env) {
    jstring result = NULL;

    if (env != NULL) {
        result = env->NewStringUTF(c_str());

        if (result == NULL || env->ExceptionCheck() == JNI_TRUE) {
            throw JavaException();
        }
    }

    return result;
}
示例#12
0
	const Env & HiddenFunctions::CheckJavaExcFunc(const Env & env) {
		if (env->ExceptionCheck())
			throw JavaException(env);
		return env;
	}
示例#13
0
	const Env & HiddenFunctions::CheckJavaExcFunc(const Env & env, PCSTR file, size_t line, PCSTR func) {
		if (env->ExceptionCheck())
			throw JavaException(env, file, line, func);
		return env;
	}
示例#14
0
inline void checkException(JNIEnv* env)
{
    if (env->ExceptionOccurred())
        throw JavaException();
}
AdblockPlus::ServerResponse AndroidWebRequest::GET(
  const std::string& url, const AdblockPlus::HeaderList& requestHeaders) const
{
  JNIEnv* jniEnv = NULL;
  int stat = globalJvm->GetEnv((void **)&jniEnv, JNI_VERSION_1_6);
  if (stat == JNI_EDETACHED)
  {
    if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0)
      throw std::runtime_error("Failed to get JNI environment");
  }

  AdblockPlus::ServerResponse result;
  try
  {
    // URL jUrl = new URL(url)
    jstring jUrlStr = jniEnv->NewStringUTF(url.c_str());

    jobject jUrl = jniEnv->NewObject(jUrlClass, jUrlConstructorID, jUrlStr);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);
    jniEnv->DeleteLocalRef(jUrlStr);

    // HttpURLConnection connection = (HttpURLConnection) jUrl.openConnection();
    jobject jConnection = jniEnv->CallObjectMethod(jUrl, jUrlOpenConnectionID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    // connection.setRequestMethod("GET");
    jstring jMethod = jniEnv->NewStringUTF("GET");
    jniEnv->CallVoidMethod(jConnection, jConnectionSetMethodID, jMethod);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);
    jniEnv->DeleteLocalRef(jMethod);

    for (int i = 0; i < requestHeaders.size(); i++)
    {
      // connection.setRequestProperty(requestHeaders[i].first, requestHeaders[i].second);
      jstring jHeader = jniEnv->NewStringUTF(requestHeaders[i].first.c_str());
      jstring jValue = jniEnv->NewStringUTF(requestHeaders[i].second.c_str());
      jniEnv->CallVoidMethod(jConnection, jConnectionSetRequestPropertyID, jHeader, jValue);
      if (jniEnv->ExceptionCheck())
        throw JavaException(jniEnv);
      jniEnv->DeleteLocalRef(jHeader);
      jniEnv->DeleteLocalRef(jValue);
    }

    // connection.connect();
    jniEnv->CallVoidMethod(jConnection, jConnectionConnectID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    // int lenghtOfFile = connection.getContentLength();
    jint lenghtOfFile = jniEnv->CallIntMethod(jConnection, jConnectionGetContentLengthID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    D(D_WARN, "Size: %d", lenghtOfFile);

    // result.responseStatus = connection.getResponseCode();
    result.responseStatus = jniEnv->CallIntMethod(jConnection, jConnectionGetResponseCodeID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    /* Read response data */

    // String jEncoding = connection.getContentEncoding();
    jstring jEncoding = (jstring) jniEnv->CallObjectMethod(jConnection, jConnectionGetContentEncodingID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    if (jEncoding == NULL)
      jEncoding = jniEnv->NewStringUTF("utf-8");

    // InputStream jis = connection.getInputStream();
    jobject jis = jniEnv->CallObjectMethod(jConnection, jConnectionGetInputStreamID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    // InputStreamReader jisr = new InputStreamReader(jis, jEncoding);
    jobject jisr = jniEnv->NewObject(jInputStreamReaderClass, jInputStreamReaderConstructorID, jis, jEncoding);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    jniEnv->DeleteLocalRef(jEncoding);

    // BufferedReader jin = new BufferedReader(jisr);
    jobject jin = jniEnv->NewObject(jBufferedReaderClass, jBufferedReaderConstructorID, jisr);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    // char[] jBuffer = new char[0x10000];
    jcharArray jBuffer = jniEnv->NewCharArray(0x10000);

    // StringBuilder jout = new StringBuilder();
    jobject jout = jniEnv->NewObject(jStringBuilderClass, jStringBuilderConstructorID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    jlong total = 0;
    jint read;

    jint jBufferLength = (jint) jniEnv->GetArrayLength(jBuffer);

    do
    {
      // read = jin.read(buffer, 0, buffer.length);
      read = jniEnv->CallIntMethod(jin, jBufferedReaderReadID, jBuffer, 0, jBufferLength);
      if (jniEnv->ExceptionCheck())
        throw JavaException(jniEnv);

      if (read > 0)
      {
        // jout.append(buffer, 0, read);
        jniEnv->CallObjectMethod(jout, jStringBuilderAppendID, jBuffer, 0, jBufferLength);
        if (jniEnv->ExceptionCheck())
          throw JavaException(jniEnv);

        total += read;
      }
    }
    while (read >= 0);

    // String jData = out.toString();
    jstring jData = (jstring) jniEnv->CallObjectMethod(jout, jStringBuilderToStringID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    result.responseText = GetString(jniEnv, jData);

    // jin.close();
    jniEnv->CallVoidMethod(jin, jBufferedReaderCloseID);
    if (jniEnv->ExceptionCheck())
      throw JavaException(jniEnv);

    jint i = 0;
    while (true)
    {
      // String jHeaderName = connection.getHeaderFieldKey(i)
      jstring jHeaderName = (jstring) jniEnv->CallObjectMethod(jConnection, jConnectionGetHeaderFieldKeyID, i);
      if (jniEnv->ExceptionCheck())
        throw JavaException(jniEnv);

      // String jHeaderValue = connection.getHeaderField(i)
      jstring jHeaderValue = (jstring) jniEnv->CallObjectMethod(jConnection, jConnectionGetHeaderFieldID, i);
      if (jniEnv->ExceptionCheck())
        throw JavaException(jniEnv);

      if (!jHeaderValue)
        break;

      std::string headerName = GetString(jniEnv, jHeaderName);
      std::string headerValue = GetString(jniEnv, jHeaderValue);

      headerName = TrimString(headerName);
      headerValue = TrimString(headerValue);

      std::transform(headerName.begin(), headerName.end(), headerName.begin(), ::tolower);

      result.responseHeaders.push_back(std::pair<std::string, std::string>(headerName, headerValue));

      i++;
    }
    D(D_WARN, "Finished downloading");

    result.status = NS_OK;
  }
  catch(JavaException& e)
  {
    result.responseStatus = 0;
    result.status = ExceptionToStatus(e);
    D(D_ERROR, "%s", e.what());
  }
  catch (const std::exception& e)
  {
    D(D_ERROR, "Exception: %s", e.what());
    result.status = AdblockPlus::DefaultWebRequest::NS_ERROR_FAILURE;
  }
  catch (...)
  {
    D(D_ERROR, "Unknown exception");
    result.status = AdblockPlus::DefaultWebRequest::NS_ERROR_FAILURE;
  }

  if (stat == JNI_EDETACHED)
    globalJvm->DetachCurrentThread();

  return result;
}