Use the com.sun.jna.platform additions that have been merged now

This commit is contained in:
Reinhard Pointner 2017-02-09 18:41:01 +08:00
parent 45ee90475f
commit 3f24dc4c42
7 changed files with 42 additions and 202 deletions

View File

@ -28,7 +28,7 @@
<classpathentry kind="lib" path="lib/ivy/jar/commons-logging.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/sevenzipjbinding.jar"/>
<classpathentry kind="lib" path="lib/ivy/bundle/json-io.jar" sourcepath="lib/ivy/source/json-io.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/jna-platform.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/jna-platform.jar" sourcepath="lib/ivy/source/jna-platform.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/language-detector.jar"/>
<classpathentry kind="lib" path="lib/ivy/bundle/guava.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/streamex.jar"/>

View File

@ -10,8 +10,6 @@ import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.AbstractMap;
import java.util.LinkedHashSet;
import java.util.List;
@ -19,7 +17,7 @@ import java.util.Set;
import com.sun.jna.Platform;
import net.filebot.mac.xattr.XAttrUtil;
import net.filebot.mac.MacXattrView;
public class MetaAttributeView extends AbstractMap<String, String> {
@ -165,30 +163,4 @@ public class MetaAttributeView extends AbstractMap<String, String> {
}
}
private static class MacXattrView {
private final String path;
public MacXattrView(Path path) {
// MacOS filesystem may require NFD unicode decomposition
this.path = Normalizer.normalize(path.toFile().getAbsolutePath(), Form.NFD);
}
public List<String> list() {
return XAttrUtil.listXAttr(path);
}
public String read(String key) {
return XAttrUtil.getXAttr(path, key);
}
public void write(String key, String value) {
XAttrUtil.setXAttr(path, key, value);
}
public void delete(String key) {
XAttrUtil.removeXAttr(path, key);
}
}
}

View File

@ -0,0 +1,35 @@
package net.filebot.mac;
import java.nio.file.Path;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.List;
import static com.sun.jna.platform.mac.XAttrUtil.*;
public class MacXattrView {
private final String path;
public MacXattrView(Path path) {
// MacOS filesystem may require NFD unicode decomposition
this.path = Normalizer.normalize(path.toFile().getAbsolutePath(), Form.NFD);
}
public List<String> list() {
return listXAttr(path);
}
public String read(String key) {
return getXAttr(path, key);
}
public void write(String key, String value) {
setXAttr(path, key, value);
}
public void delete(String key) {
removeXAttr(path, key);
}
}

View File

@ -1,51 +0,0 @@
/* Copyright (c) 2014 Reinhard Pointner, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package net.filebot.mac.xattr;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
/**
* JNA wrapper for <sys/xattr.h>
*
*/
interface XAttr extends Library {
// load from current image
XAttr INSTANCE = (XAttr) Native.loadLibrary(null, XAttr.class);
// see /usr/include/sys/xattr.h
int XATTR_NOFOLLOW = 0x0001;
int XATTR_CREATE = 0x0002;
int XATTR_REPLACE = 0x0004;
int XATTR_NOSECURITY = 0x0008;
int XATTR_NODEFAULT = 0x0010;
int XATTR_SHOWCOMPRESSION = 0x0020;
int XATTR_MAXNAMELEN = 127;
String XATTR_FINDERINFO_NAME = "com.apple.FinderInfo";
String XATTR_RESOURCEFORK_NAME = "com.apple.ResourceFork";
// see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/getxattr.2.html
long getxattr(String path, String name, Pointer value, long size, int position, int options);
// see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/setxattr.2.html
int setxattr(String path, String name, Pointer value, long size, int position, int options);
// see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/removexattr.2.html
int removexattr(String path, String name, int options);
// see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/listxattr.2.html
long listxattr(String path, Pointer namebuff, long size, int options);
}

View File

@ -1,99 +0,0 @@
/* Copyright (c) 2014 Reinhard Pointner, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package net.filebot.mac.xattr;
import static java.nio.charset.StandardCharsets.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import com.sun.jna.Memory;
public class XAttrUtil {
public static List<String> listXAttr(String path) {
// get required buffer size
long bufferLength = XAttr.INSTANCE.listxattr(path, null, 0, 0);
if (bufferLength < 0)
return null;
if (bufferLength == 0)
return new ArrayList<String>();
Memory valueBuffer = new Memory(bufferLength);
long valueLength = XAttr.INSTANCE.listxattr(path, valueBuffer, bufferLength, 0);
if (valueLength < 0)
return null;
return decodeStringSequence(valueBuffer.getByteBuffer(0, valueLength));
}
public static String getXAttr(String path, String name) {
// get required buffer size
long bufferLength = XAttr.INSTANCE.getxattr(path, name, null, 0, 0, 0);
if (bufferLength < 0)
return null;
Memory valueBuffer = new Memory(bufferLength);
long valueLength = XAttr.INSTANCE.getxattr(path, name, valueBuffer, bufferLength, 0, 0);
if (valueLength < 0)
return null;
return decodeString(valueBuffer.getByteBuffer(0, valueLength - 1));
}
public static int setXAttr(String path, String name, String value) {
Memory valueBuffer = encodeString(value);
return XAttr.INSTANCE.setxattr(path, name, valueBuffer, valueBuffer.size(), 0, 0);
}
public static int removeXAttr(String path, String name) {
return XAttr.INSTANCE.removexattr(path, name, 0);
}
protected static Memory encodeString(String s) {
// create NULL-terminated UTF-8 String
byte[] bb = s.getBytes(UTF_8);
Memory valueBuffer = new Memory(bb.length + 1);
valueBuffer.write(0, bb, 0, bb.length);
valueBuffer.setByte(valueBuffer.size() - 1, (byte) 0);
return valueBuffer;
}
protected static String decodeString(ByteBuffer bb) {
return UTF_8.decode(bb).toString();
}
protected static List<String> decodeStringSequence(ByteBuffer bb) {
List<String> names = new ArrayList<String>();
bb.mark(); // first key starts from here
while (bb.hasRemaining()) {
if (bb.get() == 0) {
ByteBuffer nameBuffer = (ByteBuffer) bb.duplicate().limit(bb.position() - 1).reset();
if (nameBuffer.hasRemaining()) {
names.add(decodeString(nameBuffer));
}
bb.mark(); // next key starts from here
}
}
return names;
}
}

View File

@ -1,17 +0,0 @@
package net.filebot.win;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.WString;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
interface Shell32 extends StdCallLibrary {
Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32", Shell32.class, W32APIOptions.DEFAULT_OPTIONS);
NativeLong SetCurrentProcessExplicitAppUserModelID(WString appID);
NativeLong GetCurrentProcessExplicitAppUserModelID(PointerByReference appID);
}

View File

@ -4,8 +4,9 @@ import static net.filebot.Logging.*;
import java.util.logging.Level;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
import com.sun.jna.platform.win32.Shell32;
import com.sun.jna.platform.win32.WinError;
import com.sun.jna.ptr.PointerByReference;
public class WinAppUtilities {
@ -20,10 +21,9 @@ public class WinAppUtilities {
public static String getAppUserModelID() {
try {
PointerByReference r = new PointerByReference();
if (Shell32.INSTANCE.GetCurrentProcessExplicitAppUserModelID(r).longValue() != 0) {
Pointer p = r.getPointer();
return p.getWideString(0); // LEAK NATIVE MEMORY
PointerByReference ppszAppID = new PointerByReference();
if (Shell32.INSTANCE.GetCurrentProcessExplicitAppUserModelID(ppszAppID) == WinError.S_OK) {
return ppszAppID.getValue().getWideString(0);
}
} catch (Throwable t) {
debug.log(Level.WARNING, t.getMessage(), t);