Refactor MetaAttributeView

This commit is contained in:
Reinhard Pointner 2017-04-01 23:56:35 +08:00
parent ef70619447
commit d61400ed96
1 changed files with 26 additions and 22 deletions

View File

@ -1,7 +1,7 @@
package net.filebot; package net.filebot;
import static java.nio.charset.StandardCharsets.*; import static java.nio.charset.StandardCharsets.*;
import static java.nio.file.Files.*; import static net.filebot.Logging.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -25,45 +25,49 @@ public class MetaAttributeView extends AbstractMap<String, String> {
private Charset encoding = UTF_8; private Charset encoding = UTF_8;
public MetaAttributeView(File file) throws IOException { public MetaAttributeView(File file) throws IOException {
Path path = file.getCanonicalFile().toPath(); // resolve symlinks
while (isSymbolicLink(path)) { Path path = file.toPath().toRealPath();
Path link = readSymbolicLink(path);
if (!link.isAbsolute()) {
link = path.getParent().resolve(link);
}
path = link;
}
// UserDefinedFileAttributeView (for Windows and Linux) OR our own xattr.h JNA wrapper via MacXattrView (for Mac) because UserDefinedFileAttributeView is not supported (Oracle Java 7/8) // UserDefinedFileAttributeView (for Windows and Linux) OR our own xattr.h JNA wrapper via MacXattrView (for Mac) because UserDefinedFileAttributeView is not supported (Oracle Java 7/8)
if (Platform.isMac()) { if (Platform.isMac()) {
xattr = new MacXattrView(path); xattr = new MacXattrView(path);
} else { } else {
xattr = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); xattr = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
}
if (xattr == null) { // sanity check
throw new IOException("UserDefinedFileAttributeView is not supported"); if (xattr == null) {
} throw new IOException("UserDefinedFileAttributeView is not supported");
} }
} }
@Override @Override
public String get(Object key) { public String get(Object key) {
return get(key.toString());
}
public String get(String key) {
try { try {
if (xattr instanceof UserDefinedFileAttributeView) { if (xattr instanceof UserDefinedFileAttributeView) {
UserDefinedFileAttributeView attributeView = (UserDefinedFileAttributeView) xattr; UserDefinedFileAttributeView attributeView = (UserDefinedFileAttributeView) xattr;
ByteBuffer buffer = ByteBuffer.allocate(attributeView.size(key.toString())); int size = attributeView.size(key);
attributeView.read(key.toString(), buffer); if (size > 0) {
buffer.flip(); ByteBuffer buffer = ByteBuffer.allocate(size);
attributeView.read(key, buffer);
buffer.flip();
return encoding.decode(buffer).toString(); return encoding.decode(buffer).toString();
} else {
return null; // attribute does not exist
}
} }
if (xattr instanceof MacXattrView) { if (xattr instanceof MacXattrView) {
MacXattrView macXattr = (MacXattrView) xattr; MacXattrView macXattr = (MacXattrView) xattr;
return macXattr.read(key.toString()); return macXattr.read(key);
} }
} catch (Exception e) { } catch (IOException e) {
// ignore debug.warning(cause(e));
} }
return null; return null;
@ -89,7 +93,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
macXattr.write(key, value); macXattr.write(key, value);
} }
} }
} catch (Exception e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -118,7 +122,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
entries.add(new AttributeEntry(name)); entries.add(new AttributeEntry(name));
} }
return entries; return entries;
} catch (Exception e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -129,7 +133,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
for (String key : this.list()) { for (String key : this.list()) {
this.put(key, null); this.put(key, null);
} }
} catch (Exception e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }