Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,12 @@
|
|||
public class JNIDemo
|
||||
{
|
||||
static
|
||||
{ System.loadLibrary("JNIDemo"); }
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println(callStrdup("Hello World!"));
|
||||
}
|
||||
|
||||
private static native String callStrdup(String s);
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class JNIDemo */
|
||||
|
||||
#ifndef _Included_JNIDemo
|
||||
#define _Included_JNIDemo
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: JNIDemo
|
||||
* Method: callStrdup
|
||||
* Signature: (Ljava/lang/String;)Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jstring JNICALL Java_JNIDemo_callStrdup
|
||||
(JNIEnv *, jclass, jstring);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#include "string.h"
|
||||
#include "JNIDemo.h"
|
||||
|
||||
void throwByName(JNIEnv* env, const char* className, const char* msg)
|
||||
{
|
||||
jclass exceptionClass = (*env)->FindClass(env, className);
|
||||
if (exceptionClass != NULL)
|
||||
{
|
||||
(*env)->ThrowNew(env, exceptionClass, msg);
|
||||
(*env)->DeleteLocalRef(env, exceptionClass);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL Java_JNIDemo_callStrdup(JNIEnv *env, jclass cls, jstring s)
|
||||
{
|
||||
const jbyte* utf8String;
|
||||
char* dupe;
|
||||
jstring dupeString;
|
||||
|
||||
if (s == NULL)
|
||||
{
|
||||
throwByName(env, "java/lang/NullPointerException", "String is null");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Convert from UTF-16 to UTF-8 (C-style)
|
||||
utf8String = (*env)->GetStringUTFChars(env, s, NULL);
|
||||
|
||||
// Duplicate
|
||||
dupe = strdup(utf8String);
|
||||
|
||||
// Free the UTF-8 string back to the JVM
|
||||
(*env)->ReleaseStringUTFChars(env, s, utf8String);
|
||||
|
||||
// Convert the duplicate string from strdup to a Java String
|
||||
dupeString = (*env)->NewStringUTF(env, dupe);
|
||||
|
||||
// Free the duplicate c-string back to the C runtime heap
|
||||
free(dupe);
|
||||
|
||||
return dupeString;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue