- Changed char_to_java() to use C charset instead of JVM's default encoding.
- Changed PosixFileFunctions.readLink() to return String instead of byte[].
This commit is contained in:
@@ -122,11 +122,11 @@ You can run `$INSTALL_DIR/bin/native-platform` to run the test application.
|
|||||||
* Freebsd: finish port.
|
* Freebsd: finish port.
|
||||||
* Freebsd: fail for unsupported architecture.
|
* Freebsd: fail for unsupported architecture.
|
||||||
* Freebsd: build 32 bit and 64 bit libraries.
|
* Freebsd: build 32 bit and 64 bit libraries.
|
||||||
* Unix: char_to_java() should use libc charset instead of java file.encoding
|
|
||||||
|
|
||||||
### Improvements
|
### Improvements
|
||||||
|
|
||||||
* Implement java_to_char_str()
|
* Implement java_to_char_str()
|
||||||
|
* Use iconv() to convert from C char string to UTF-16 when converting from C char string to Java String.
|
||||||
* Support for cygwin terminal
|
* Support for cygwin terminal
|
||||||
* Use TERM=xtermc instead of TERM=xterm on Solaris.
|
* Use TERM=xtermc instead of TERM=xterm on Solaris.
|
||||||
* Add diagnostics for terminal.
|
* Add diagnostics for terminal.
|
||||||
|
|||||||
@@ -17,5 +17,5 @@ void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, job
|
|||||||
|
|
||||||
JNIEXPORT jint JNICALL
|
JNIEXPORT jint JNICALL
|
||||||
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
|
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
|
||||||
return 7;
|
return 8;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,14 +31,23 @@ void char_str_free(char_str* str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jstring char_to_java(JNIEnv* env, const char* chars, jobject result) {
|
jstring char_to_java(JNIEnv* env, const char* chars, jobject result) {
|
||||||
|
// TODO - share this code with nnn_getSystemInfo() below
|
||||||
|
locale_t locale = newlocale(LC_CTYPE_MASK, "", NULL);
|
||||||
|
if (locale == NULL) {
|
||||||
|
mark_failed_with_message(env, "could not create locale", result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
jstring encoding = env->NewStringUTF(nl_langinfo_l(CODESET, locale));
|
||||||
|
freelocale(locale);
|
||||||
|
|
||||||
size_t len = strlen(chars);
|
size_t len = strlen(chars);
|
||||||
jbyteArray byteArray = env->NewByteArray(len);
|
jbyteArray byteArray = env->NewByteArray(len);
|
||||||
jbyte* bytes = env->GetByteArrayElements(byteArray, NULL);
|
jbyte* bytes = env->GetByteArrayElements(byteArray, NULL);
|
||||||
memcpy(bytes, chars, len);
|
memcpy(bytes, chars, len);
|
||||||
env->ReleaseByteArrayElements(byteArray, bytes, JNI_COMMIT);
|
env->ReleaseByteArrayElements(byteArray, bytes, JNI_COMMIT);
|
||||||
jclass strClass = env->FindClass("java/lang/String");
|
jclass strClass = env->FindClass("java/lang/String");
|
||||||
jmethodID method = env->GetMethodID(strClass, "<init>", "([B)V");
|
jmethodID method = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
|
||||||
return (jstring)env->NewObject(strClass, method, byteArray);
|
return (jstring)env->NewObject(strClass, method, byteArray, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL
|
JNIEXPORT void JNICALL
|
||||||
@@ -111,7 +120,7 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_symlink(JNIEnv
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jbyteArray JNICALL
|
JNIEXPORT jstring JNICALL
|
||||||
Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_readlink(JNIEnv *env, jclass target, jbyteArray path, jobject result) {
|
Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_readlink(JNIEnv *env, jclass target, jbyteArray path, jobject result) {
|
||||||
struct stat link_info;
|
struct stat link_info;
|
||||||
jbyte* pathUtf8 = env->GetByteArrayElements(path, NULL);
|
jbyte* pathUtf8 = env->GetByteArrayElements(path, NULL);
|
||||||
@@ -122,23 +131,24 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_readlink(JNIEnv
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
jbyteArray contents = env->NewByteArray(link_info.st_size);
|
char* contents = (char*)malloc(link_info.st_size + 1);
|
||||||
if (contents == NULL) {
|
if (contents == NULL) {
|
||||||
env->ReleaseByteArrayElements(path, pathUtf8, JNI_ABORT);
|
env->ReleaseByteArrayElements(path, pathUtf8, JNI_ABORT);
|
||||||
mark_failed_with_message(env, "could not create array", result);
|
mark_failed_with_message(env, "could not create array", result);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
jbyte* contentsUtf8 = env->GetByteArrayElements(contents, NULL);
|
retval = readlink((const char*)pathUtf8, contents, link_info.st_size);
|
||||||
retval = readlink((const char*)pathUtf8, (char*)contentsUtf8, link_info.st_size);
|
|
||||||
env->ReleaseByteArrayElements(path, pathUtf8, JNI_ABORT);
|
env->ReleaseByteArrayElements(path, pathUtf8, JNI_ABORT);
|
||||||
if (retval < 0) {
|
if (retval < 0) {
|
||||||
|
free(contents);
|
||||||
mark_failed_with_errno(env, "could not readlink", result);
|
mark_failed_with_errno(env, "could not readlink", result);
|
||||||
env->ReleaseByteArrayElements(contents, contentsUtf8, JNI_ABORT);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
env->ReleaseByteArrayElements(contents, contentsUtf8, JNI_COMMIT);
|
contents[link_info.st_size] = 0;
|
||||||
return contents;
|
jstring contents_str = char_to_java(env, contents, result);
|
||||||
|
free(contents);
|
||||||
|
return contents_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -36,11 +36,11 @@ public class DefaultPosixFile implements PosixFile {
|
|||||||
@Override
|
@Override
|
||||||
public String readLink(File link) throws NativeException {
|
public String readLink(File link) throws NativeException {
|
||||||
FunctionResult result = new FunctionResult();
|
FunctionResult result = new FunctionResult();
|
||||||
byte[] encodedContents = PosixFileFunctions.readlink(encode(link), result);
|
String contents = PosixFileFunctions.readlink(encode(link), result);
|
||||||
if (result.isFailed()) {
|
if (result.isFailed()) {
|
||||||
throw new NativeException(String.format("Could not read symlink %s: %s", link, result.getMessage()));
|
throw new NativeException(String.format("Could not read symlink %s: %s", link, result.getMessage()));
|
||||||
}
|
}
|
||||||
return decode(encodedContents);
|
return contents;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -52,14 +52,6 @@ public class DefaultPosixFile implements PosixFile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String decode(byte[] path) {
|
|
||||||
try {
|
|
||||||
return new String(path, 0, path.length, characterEncoding);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new NativeException(String.format("Could not decode path using encoding %s.", characterEncoding), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] encode(File file) {
|
private byte[] encode(File file) {
|
||||||
return encode(file.getPath());
|
return encode(file.getPath());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import net.rubygrapefruit.platform.internal.FunctionResult;
|
|||||||
import net.rubygrapefruit.platform.internal.MutableSystemInfo;
|
import net.rubygrapefruit.platform.internal.MutableSystemInfo;
|
||||||
|
|
||||||
public class NativeLibraryFunctions {
|
public class NativeLibraryFunctions {
|
||||||
public static final int VERSION = 7;
|
public static final int VERSION = 8;
|
||||||
|
|
||||||
public static native int getVersion();
|
public static native int getVersion();
|
||||||
|
|
||||||
|
|||||||
@@ -10,5 +10,5 @@ public class PosixFileFunctions {
|
|||||||
|
|
||||||
public static native void symlink(byte[] file, byte[] content, FunctionResult result);
|
public static native void symlink(byte[] file, byte[] content, FunctionResult result);
|
||||||
|
|
||||||
public static native byte[] readlink(byte[] file, FunctionResult result);
|
public static native String readlink(byte[] file, FunctionResult result);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user