- 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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,4 @@ public class DefaultSystemInfo implements SystemInfo {
|
||||
public String getMachineArchitecture() {
|
||||
return systemInfo.getMachineArchitecture();
|
||||
}
|
||||
|
||||
public String getCharacterEncoding() {
|
||||
return systemInfo.characterEncoding;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user