1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-17 23:05:03 -05:00

Refactor MetaAttributeView

This commit is contained in:
Reinhard Pointner 2017-04-02 00:33:40 +08:00
parent d61400ed96
commit 2e221d98dc

View File

@ -6,7 +6,6 @@ import static net.filebot.Logging.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.UserDefinedFileAttributeView; import java.nio.file.attribute.UserDefinedFileAttributeView;
@ -21,8 +20,7 @@ import net.filebot.platform.mac.MacXattrView;
public class MetaAttributeView extends AbstractMap<String, String> { public class MetaAttributeView extends AbstractMap<String, String> {
private Object xattr; private final Object xattr;
private Charset encoding = UTF_8;
public MetaAttributeView(File file) throws IOException { public MetaAttributeView(File file) throws IOException {
// resolve symlinks // resolve symlinks
@ -56,7 +54,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
attributeView.read(key, buffer); attributeView.read(key, buffer);
buffer.flip(); buffer.flip();
return encoding.decode(buffer).toString(); return UTF_8.decode(buffer).toString();
} else { } else {
return null; // attribute does not exist return null; // attribute does not exist
} }
@ -81,7 +79,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
if (value == null || value.isEmpty()) { if (value == null || value.isEmpty()) {
attributeView.delete(key); attributeView.delete(key);
} else { } else {
attributeView.write(key, encoding.encode(value)); attributeView.write(key, UTF_8.encode(value));
} }
} }
@ -100,6 +98,17 @@ public class MetaAttributeView extends AbstractMap<String, String> {
return null; // since we don't know the old value return null; // since we don't know the old value
} }
@Override
public void clear() {
try {
for (String key : list()) {
put(key, null);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public List<String> list() throws IOException { public List<String> list() throws IOException {
if (xattr instanceof UserDefinedFileAttributeView) { if (xattr instanceof UserDefinedFileAttributeView) {
UserDefinedFileAttributeView attributeView = (UserDefinedFileAttributeView) xattr; UserDefinedFileAttributeView attributeView = (UserDefinedFileAttributeView) xattr;
@ -118,7 +127,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
public Set<Entry<String, String>> entrySet() { public Set<Entry<String, String>> entrySet() {
try { try {
Set<Entry<String, String>> entries = new LinkedHashSet<Entry<String, String>>(); Set<Entry<String, String>> entries = new LinkedHashSet<Entry<String, String>>();
for (String name : this.list()) { for (String name : list()) {
entries.add(new AttributeEntry(name)); entries.add(new AttributeEntry(name));
} }
return entries; return entries;
@ -127,17 +136,6 @@ public class MetaAttributeView extends AbstractMap<String, String> {
} }
} }
@Override
public void clear() {
try {
for (String key : this.list()) {
this.put(key, null);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private class AttributeEntry implements Entry<String, String> { private class AttributeEntry implements Entry<String, String> {
private final String name; private final String name;