- Changed PosixFileFunctions to pass Strings to JNI methods, and use java_to_char() to encode as C char string.

- Don't need to pass system encoding back up to Java as part of system info.
This commit is contained in:
Adam Murdoch
2012-09-09 16:29:42 +10:00
parent 208d1be584
commit dd255be667
7 changed files with 49 additions and 72 deletions

View File

@@ -5,19 +5,11 @@ import net.rubygrapefruit.platform.PosixFile;
import net.rubygrapefruit.platform.internal.jni.PosixFileFunctions;
import java.io.File;
import java.io.UnsupportedEncodingException;
public class DefaultPosixFile implements PosixFile {
private final String characterEncoding;
public DefaultPosixFile() {
DefaultSystemInfo systemInfo = new DefaultSystemInfo();
this.characterEncoding = systemInfo.getCharacterEncoding();
}
public void setMode(File file, int perms) {
FunctionResult result = new FunctionResult();
PosixFileFunctions.chmod(encode(file), perms, result);
PosixFileFunctions.chmod(file.getPath(), perms, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not set UNIX mode on %s: %s", file, result.getMessage()));
}
@@ -26,7 +18,7 @@ public class DefaultPosixFile implements PosixFile {
public int getMode(File file) {
FunctionResult result = new FunctionResult();
FileStat stat = new FileStat();
PosixFileFunctions.stat(encode(file), stat, result);
PosixFileFunctions.stat(file.getPath(), stat, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not get UNIX mode on %s: %s", file, result.getMessage()));
}
@@ -36,7 +28,7 @@ public class DefaultPosixFile implements PosixFile {
@Override
public String readLink(File link) throws NativeException {
FunctionResult result = new FunctionResult();
String contents = PosixFileFunctions.readlink(encode(link), result);
String contents = PosixFileFunctions.readlink(link.getPath(), result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not read symlink %s: %s", link, result.getMessage()));
}
@@ -46,26 +38,9 @@ public class DefaultPosixFile implements PosixFile {
@Override
public void symlink(File link, String contents) throws NativeException {
FunctionResult result = new FunctionResult();
PosixFileFunctions.symlink(encode(link), encode(contents), result);
PosixFileFunctions.symlink(link.getPath(), contents, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not create symlink %s: %s", link, result.getMessage()));
}
}
private byte[] encode(File file) {
return encode(file.getPath());
}
private byte[] encode(String path) {
byte[] encodedName;
try {
encodedName = path.getBytes(characterEncoding);
} catch (UnsupportedEncodingException e) {
throw new NativeException(String.format("Could not encode path '%s' using encoding %s.", path, characterEncoding), e);
}
byte[] buffer = new byte[encodedName.length + 1];
System.arraycopy(encodedName, 0, buffer, 0, encodedName.length);
buffer[buffer.length - 1] = 0;
return buffer;
}
}

View File

@@ -30,8 +30,4 @@ public class DefaultSystemInfo implements SystemInfo {
public String getMachineArchitecture() {
return systemInfo.getMachineArchitecture();
}
public String getCharacterEncoding() {
return systemInfo.characterEncoding;
}
}

View File

@@ -5,7 +5,6 @@ import net.rubygrapefruit.platform.SystemInfo;
public class MutableSystemInfo implements SystemInfo {
public String osName;
public String osVersion;
public String characterEncoding;
public String machineArchitecture;
public String getKernelName() {

View File

@@ -4,7 +4,7 @@ import net.rubygrapefruit.platform.internal.FunctionResult;
import net.rubygrapefruit.platform.internal.MutableSystemInfo;
public class NativeLibraryFunctions {
public static final int VERSION = 8;
public static final int VERSION = 9;
public static native int getVersion();

View File

@@ -4,11 +4,11 @@ import net.rubygrapefruit.platform.internal.FileStat;
import net.rubygrapefruit.platform.internal.FunctionResult;
public class PosixFileFunctions {
public static native void chmod(byte[] file, int perms, FunctionResult result);
public static native void chmod(String file, int perms, FunctionResult result);
public static native void stat(byte[] file, FileStat stat, FunctionResult result);
public static native void stat(String file, FileStat stat, FunctionResult result);
public static native void symlink(byte[] file, byte[] content, FunctionResult result);
public static native void symlink(String file, String content, FunctionResult result);
public static native String readlink(byte[] file, FunctionResult result);
public static native String readlink(String file, FunctionResult result);
}