Class FileAttributeSupport
- java.lang.Object
-
- com.github.robtimus.filesystems.attribute.FileAttributeSupport
-
public final class FileAttributeSupport extends Object
A utility class for file attributes.Reading attributes
Methods of this class can be used to implementFileSystemProvider.readAttributes(Path, String, LinkOption...)as follows, using aFileAttributeViewCollectioninstance calledviews:@Override public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException { String viewName = getViewName(attributes); FileAttributeViewMetadata view = views.getView(viewName); Set<String> attributeNames = getAttributeNames(attributes, view); PosixFileAttributes fileAttributes = readAttributes(path, PosixFileAttributes.class, options); Map<String, Object> result = new HashMap<>(); populateAttributeMap(result, fileAttributes, attributeNames); return result; }For custom
FileAttributeViewsub types, the above setup can still be used. Just provide a customFileAttributeViewMetadatainstance, and usepopulateAttributeMap(Map, String, Set, Supplier)for any custom properties. For instance, whereMyFileAttributesextendsBasicFileAttributesand has a methodmyAttribute()that returns the value for a custom attribute:MyFileAttributes fileAttributes = readAttributes(path, MyFileAttributes.class, options); Map<String, Object> result = new HashMap<>(); populateAttributeMap(result, fileAttributes, attributeNames); // upcast to BasicFileAttributes populateAttributeMap(result, "myAttribute", attributeNames, fileAttributes::myAttribute); return result;Setting attributes
Methods of this class can be used to implementFileSystemProvider.setAttribute(Path, String, Object, LinkOption...)as follows, using constants fromFileAttributeConstants:@Override public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException { String viewName = getViewName(attribute); String attributeName = getAttributeName(attribute); switch (viewName) { case BASIC_VIEW: BasicFileAttributeView basicView = getFileAttributeView(path, BasicFileAttributeView.class, options); FileAttributeSupport.setAttribute(attribute, value, basicView); break; case FILE_OWNER_VIEW: FileOwnerAttributeView fileOwnerView = getFileAttributeView(path, FileOwnerAttributeView.class, options); FileAttributeSupport.setAttribute(attribute, value, fileOwnerView); break; case POSIX_VIEW: PosixFileAttributeView posixView = getFileAttributeView(path, PosixFileAttributeView.class, options); FileAttributeSupport.setAttribute(attribute, value, posixView); break; default: throw Messages.fileSystemProvider().unsupportedFileAttributeView(viewName); } }For custom
FileAttributeViewsub types, the above setup can still be used. Use a switch or if statement to handle your custom attributes, and call any of thesetAttributemethods of this class for the default case or else block. For instance, whereMyFileAttributeViewextendsBasicFileAttributeViewand has a methodsetMyAttribute(String value)that sets the value for a custom attribute:MyFileAttributeView myView = getFileAttributeView(path, MyFileAttributeView.class, options); if ("myAttribute".equals(attributeName)) { view.setMyAttribute((String) value); } else { FileAttributeSupport.setAttribute(attribute, value, myView); // upcast to BasicFileAttributeView }Setting attributes during object creation
ThetoAttributeMapmethods of this class can be used to collectFileAttributeobjects into maps where the key and value of each entry can be passed to any of thesetAttributemethods of this class. This allows file attributes to be set from methods likeFileSystemProvider.createDirectory(Path, FileAttribute...)orFileSystemProvider.newByteChannel(Path, Set, FileAttribute...).- Author:
- Rob Spoor
- Since:
- 2.2
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringgetAttributeName(String attribute)Returns the attribute name from a string representing an attribute to be set for a specific view.static Set<String>getAttributeNames(String attributes, FileAttributeViewMetadata view)Returns the actual attribute names from a string representing attributes to read for a specific view.static StringgetViewName(String attributes)Returns the view name from a string representing attributes to read.static voidpopulateAttributeMap(Map<String,Object> attributeMap, String attributeName, Set<String> attributeNames, Supplier<?> getter)Populates an attribute map with a single attribute.static voidpopulateAttributeMap(Map<String,Object> attributeMap, BasicFileAttributes fileAttributes, Set<String> attributeNames)Populates an attribute map from aBasicFileAttributesobject.static voidpopulateAttributeMap(Map<String,Object> attributeMap, DosFileAttributes fileAttributes, Set<String> attributeNames)Populates an attribute map from aDosFileAttributesobject.static voidpopulateAttributeMap(Map<String,Object> attributeMap, PosixFileAttributes fileAttributes, Set<String> attributeNames)Populates an attribute map from aPosixFileAttributesobject.static voidsetAttribute(String attributeName, Object value, AclFileAttributeView view)Sets an attribute on anAclFileAttributeViewobject.static voidsetAttribute(String attributeName, Object value, BasicFileAttributeView view)Sets an attribute on aBasicFileAttributeViewobject.static voidsetAttribute(String attributeName, Object value, DosFileAttributeView view)Sets an attribute on aDosFileAttributeViewobject.static voidsetAttribute(String attributeName, Object value, FileOwnerAttributeView view)Sets an attribute on aFileOwnerAttributeViewobject.static voidsetAttribute(String attributeName, Object value, PosixFileAttributeView view)Sets an attribute on aPosixFileAttributeViewobject.static Map<String,Object>toAttributeMap(FileAttribute<?>[] attributes, FileAttributeViewCollection supportedViews)Collects severalFileAttributeobjects into a map.static Map<String,Object>toAttributeMap(FileAttribute<?>[] attributes, FileAttributeViewCollection supportedViews, String... nonSupportedAttributeNames)Collects severalFileAttributeobjects into a map.static Map<String,Object>toAttributeMap(FileAttribute<?>[] attributes, FileAttributeViewCollection supportedViews, Collection<String> nonSupportedAttributeNames)Collects severalFileAttributeobjects into a map.static Map<String,Object>toAttributeMap(FileAttribute<?>[] attributes, FileAttributeViewMetadata... supportedViews)Collects severalFileAttributeobjects into a map.static Map<String,Object>toAttributeMap(FileAttribute<?>[] attributes, Collection<FileAttributeViewMetadata> supportedViews)Collects severalFileAttributeobjects into a map.static Map<String,Object>toAttributeMap(FileAttribute<?>[] attributes, Collection<FileAttributeViewMetadata> supportedViews, String... nonSupportedAttributeNames)Collects severalFileAttributeobjects into a map.static Map<String,Object>toAttributeMap(FileAttribute<?>[] attributes, Collection<FileAttributeViewMetadata> supportedViews, Collection<String> nonSupportedAttributeNames)Collects severalFileAttributeobjects into a map.
-
-
-
Method Detail
-
getViewName
public static String getViewName(String attributes)
Returns the view name from a string representing attributes to read. This string must be formatted as specified inFiles.readAttributes(Path, String, LinkOption...)orFiles.setAttribute(Path, String, Object, LinkOption...).- Parameters:
attributes- The string representing the attributes to read.- Returns:
- The view name from the string, or
basicif the string did not contain a view name. - Throws:
NullPointerException- If the given string isnull.
-
getAttributeName
public static String getAttributeName(String attribute)
Returns the attribute name from a string representing an attribute to be set for a specific view. This string must be formatted as specified inFiles.setAttribute(Path, String, Object, LinkOption...).- Parameters:
attribute- The string representing the attribute to set.- Returns:
- The attribute name to set.
- Throws:
NullPointerException- If the given string isnull.
-
getAttributeNames
public static Set<String> getAttributeNames(String attributes, FileAttributeViewMetadata view)
Returns the actual attribute names from a string representing attributes to read for a specific view. This string must be formatted as specified inFiles.readAttributes(Path, String, LinkOption...).- Parameters:
attributes- The string representing the attributes to read.view- AFileAttributeViewMetadataobject representing the view.- Returns:
- A set with the actual attribute names to read.
- Throws:
NullPointerException- If the given string orFileAttributeViewMetadataobject isnull.IllegalArgumentException- If the string contains a view name that does not match the view name of the givenFileAttributeViewMetadataobject, or if any of the specified attributes to read is not supported by the view.
-
populateAttributeMap
public static void populateAttributeMap(Map<String,Object> attributeMap, BasicFileAttributes fileAttributes, Set<String> attributeNames)
Populates an attribute map from aBasicFileAttributesobject.- Parameters:
attributeMap- The attribute map to populate.fileAttributes- TheBasicFileAttributeswith values to populate with.attributeNames- A set containing the attribute names to populate. This should usually come fromgetAttributeNames(String, FileAttributeViewMetadata).- Throws:
NullPointerException- If any of the arguments isnull.- See Also:
FileSystemProvider.readAttributes(Path, String, LinkOption...)
-
populateAttributeMap
public static void populateAttributeMap(Map<String,Object> attributeMap, DosFileAttributes fileAttributes, Set<String> attributeNames)
Populates an attribute map from aDosFileAttributesobject.- Parameters:
attributeMap- The attribute map to populate.fileAttributes- TheBasicFileAttributeswith values to populate with.attributeNames- A set containing the attribute names to populate. This should usually come fromgetAttributeNames(String, FileAttributeViewMetadata).- Throws:
NullPointerException- If any of the arguments isnull.- See Also:
FileSystemProvider.readAttributes(Path, String, LinkOption...)
-
populateAttributeMap
public static void populateAttributeMap(Map<String,Object> attributeMap, PosixFileAttributes fileAttributes, Set<String> attributeNames)
Populates an attribute map from aPosixFileAttributesobject.- Parameters:
attributeMap- The attribute map to populate.fileAttributes- TheBasicFileAttributeswith values to populate with.attributeNames- A set containing the attribute names to populate. This should usually come fromgetAttributeNames(String, FileAttributeViewMetadata).- Throws:
NullPointerException- If any of the arguments isnull.- See Also:
FileSystemProvider.readAttributes(Path, String, LinkOption...)
-
populateAttributeMap
public static void populateAttributeMap(Map<String,Object> attributeMap, String attributeName, Set<String> attributeNames, Supplier<?> getter)
Populates an attribute map with a single attribute. If the given attribute name is not contained in the given set of attribute names, nothing is done.- Parameters:
attributeMap- The attribute map to populate.attributeName- The name of the attribute to populate.attributeNames- A set containing the attribute names to populate. This should usually come fromgetAttributeNames(String, FileAttributeViewMetadata).getter- A getter for the attribute value.- Throws:
NullPointerException- If any of the arguments isnull.- See Also:
FileSystemProvider.readAttributes(Path, String, LinkOption...)
-
setAttribute
public static void setAttribute(String attributeName, Object value, BasicFileAttributeView view) throws IOException
Sets an attribute on aBasicFileAttributeViewobject.- Parameters:
attributeName- The name of the attribute to set. It should not be prefixed with the view name;getAttributeName(String)can be used to extract it.value- The value to set.view- The view to set the attribute on. IllegalArgumentException If the attribute name is not recognized, or the attribute value is of the correct type but has an inappropriate value. ClassCastException If the attribute value is not of the expected type or is a collection containing elements that are not of the expected type.- Throws:
IOException- If an I/O error occurs.
-
setAttribute
public static void setAttribute(String attributeName, Object value, FileOwnerAttributeView view) throws IOException
Sets an attribute on aFileOwnerAttributeViewobject.- Parameters:
attributeName- The name of the attribute to set. It should not be prefixed with the view name;getAttributeName(String)can be used to extract it.value- The value to set.view- The view to set the attribute on. IllegalArgumentException If the attribute name is not recognized, or the attribute value is of the correct type but has an inappropriate value. ClassCastException If the attribute value is not of the expected type or is a collection containing elements that are not of the expected type.- Throws:
IOException- If an I/O error occurs.
-
setAttribute
public static void setAttribute(String attributeName, Object value, DosFileAttributeView view) throws IOException
Sets an attribute on aDosFileAttributeViewobject.- Parameters:
attributeName- The name of the attribute to set. It should not be prefixed with the view name;getAttributeName(String)can be used to extract it.value- The value to set.view- The view to set the attribute on. IllegalArgumentException If the attribute name is not recognized, or the attribute value is of the correct type but has an inappropriate value. ClassCastException If the attribute value is not of the expected type or is a collection containing elements that are not of the expected type.- Throws:
IOException- If an I/O error occurs.
-
setAttribute
public static void setAttribute(String attributeName, Object value, PosixFileAttributeView view) throws IOException
Sets an attribute on aPosixFileAttributeViewobject.- Parameters:
attributeName- The name of the attribute to set. It should not be prefixed with the view name;getAttributeName(String)can be used to extract it.value- The value to set.view- The view to set the attribute on. IllegalArgumentException If the attribute name is not recognized, or the attribute value is of the correct type but has an inappropriate value. ClassCastException If the attribute value is not of the expected type or is a collection containing elements that are not of the expected type.- Throws:
IOException- If an I/O error occurs.
-
setAttribute
public static void setAttribute(String attributeName, Object value, AclFileAttributeView view) throws IOException
Sets an attribute on anAclFileAttributeViewobject.- Parameters:
attributeName- The name of the attribute to set. It should not be prefixed with the view name;getAttributeName(String)can be used to extract it.value- The value to set.view- The view to set the attribute on. IllegalArgumentException If the attribute name is not recognized, or the attribute value is of the correct type but has an inappropriate value. ClassCastException If the attribute value is not of the expected type or is a collection containing elements that are not of the expected type.- Throws:
IOException- If an I/O error occurs.
-
toAttributeMap
public static Map<String,Object> toAttributeMap(FileAttribute<?>[] attributes, FileAttributeViewMetadata... supportedViews)
Collects severalFileAttributeobjects into a map. Each entry of the map can be used withFileSystemProvider.setAttribute(Path, String, Object, LinkOption...). Combined withgetViewName(String)andgetAttributeName(String), each entry can also be used with any of thesetAttributemethods of this class.- Parameters:
attributes- TheFileAttributeobjects to collect.supportedViews- A number ofFileAttributeViewMetadataobjects representing the supported views.- Returns:
- A map where each key is the name of a given
FileAttributeobject, prefixed with the matching view name where needed. - Throws:
NullPointerException- If any of the givenFileAttributeorFileAttributeViewMetadataobjects isnull.UnsupportedOperationException- If any of the givenFileAttributeobjects refers to a view that is not referred to by any of the given supportedFileAttributeViewMetadataobjects, or has a non-supported name, or has a value that does not match theexpected type.
-
toAttributeMap
public static Map<String,Object> toAttributeMap(FileAttribute<?>[] attributes, Collection<FileAttributeViewMetadata> supportedViews)
Collects severalFileAttributeobjects into a map. Each entry of the map can be used withFileSystemProvider.setAttribute(Path, String, Object, LinkOption...). Combined withgetViewName(String)andgetAttributeName(String), each entry can also be used with any of thesetAttributemethods of this class.- Parameters:
attributes- TheFileAttributeobjects to collect.supportedViews- A collection withFileAttributeViewMetadataobjects representing the supported views.- Returns:
- A map where each key is the name of a given
FileAttributeobject, prefixed with the matching view name where needed. - Throws:
NullPointerException- If any of the givenFileAttributeorFileAttributeViewMetadataobjects isnull.UnsupportedOperationException- If any of the givenFileAttributeobjects refers to a view that is not referred to by any of the given supportedFileAttributeViewMetadataobjects, or has a non-supported name, or has a value that does not match theexpected type.
-
toAttributeMap
public static Map<String,Object> toAttributeMap(FileAttribute<?>[] attributes, Collection<FileAttributeViewMetadata> supportedViews, String... nonSupportedAttributeNames)
Collects severalFileAttributeobjects into a map. Each entry of the map can be used withFileSystemProvider.setAttribute(Path, String, Object, LinkOption...). Combined withgetViewName(String)andgetAttributeName(String), each entry can also be used with any of thesetAttributemethods of this class.- Parameters:
attributes- TheFileAttributeobjects to collect.supportedViews- A collection withFileAttributeViewMetadataobjects representing the supported views.nonSupportedAttributeNames- A collection of attribute names that are not supported, regardless of what the supported views say. This can be used for attributes that cannot be set during creation but only afterwards. Elements should not be prefixed with view names.- Returns:
- A map where each key is the name of a given
FileAttributeobject, prefixed with the matching view name where needed. - Throws:
NullPointerException- If any of the givenFileAttributeorFileAttributeViewMetadataobjects isnull.UnsupportedOperationException- If any of the givenFileAttributeobjects refers to a view that is not referred to by any of the given supportedFileAttributeViewMetadataobjects, or has a non-supported name, or has a value that does not match theexpected type.
-
toAttributeMap
public static Map<String,Object> toAttributeMap(FileAttribute<?>[] attributes, Collection<FileAttributeViewMetadata> supportedViews, Collection<String> nonSupportedAttributeNames)
Collects severalFileAttributeobjects into a map. Each entry of the map can be used withFileSystemProvider.setAttribute(Path, String, Object, LinkOption...). Combined withgetViewName(String)andgetAttributeName(String), each entry can also be used with any of thesetAttributemethods of this class.- Parameters:
attributes- TheFileAttributeobjects to collect.supportedViews- A collection withFileAttributeViewMetadataobjects representing the supported views.nonSupportedAttributeNames- A collection of attribute names that are not supported, regardless of what the supported views say. This can be used for attributes that cannot be set during creation but only afterwards. Elements should not be prefixed with view names.- Returns:
- A map where each key is the name of a given
FileAttributeobject, prefixed with the matching view name where needed. - Throws:
NullPointerException- If any of the givenFileAttributeorFileAttributeViewMetadataobjects isnull.UnsupportedOperationException- If any of the givenFileAttributeobjects refers to a view that is not referred to by any of the given supportedFileAttributeViewMetadataobjects, or has a non-supported name, or has a value that does not match theexpected type.
-
toAttributeMap
public static Map<String,Object> toAttributeMap(FileAttribute<?>[] attributes, FileAttributeViewCollection supportedViews)
Collects severalFileAttributeobjects into a map. Each entry of the map can be used withFileSystemProvider.setAttribute(Path, String, Object, LinkOption...). Combined withgetViewName(String)andgetAttributeName(String), each entry can also be used with any of thesetAttributemethods of this class.- Parameters:
attributes- TheFileAttributeobjects to collect.supportedViews- A collection withFileAttributeViewMetadataobjects representing the supported views.- Returns:
- A map where each key is the name of a given
FileAttributeobject, prefixed with the matching view name where needed. - Throws:
NullPointerException- If any of the givenFileAttributeobjects or the givenFileAttributeViewCollectionobject isnull.UnsupportedOperationException- If any of the givenFileAttributeobjects refers to a view that is not referred to by any of the given supportedFileAttributeViewMetadataobjects, or has a non-supported name, or has a value that does not match theexpected type.
-
toAttributeMap
public static Map<String,Object> toAttributeMap(FileAttribute<?>[] attributes, FileAttributeViewCollection supportedViews, String... nonSupportedAttributeNames)
Collects severalFileAttributeobjects into a map. Each entry of the map can be used withFileSystemProvider.setAttribute(Path, String, Object, LinkOption...). Combined withgetViewName(String)andgetAttributeName(String), each entry can also be used with any of thesetAttributemethods of this class.- Parameters:
attributes- TheFileAttributeobjects to collect.supportedViews- A collection withFileAttributeViewMetadataobjects representing the supported views.nonSupportedAttributeNames- A collection of attribute names that are not supported, regardless of what the supported views say. This can be used for attributes that cannot be set during creation but only afterwards. Elements should not be prefixed with view names.- Returns:
- A map where each key is the name of a given
FileAttributeobject, prefixed with the matching view name where needed. - Throws:
NullPointerException- If any of the givenFileAttributeobjects or the givenFileAttributeViewCollectionobject isnull.UnsupportedOperationException- If any of the givenFileAttributeobjects refers to a view that is not referred to by any of the given supportedFileAttributeViewMetadataobjects, or has a non-supported name, or has a value that does not match theexpected type.
-
toAttributeMap
public static Map<String,Object> toAttributeMap(FileAttribute<?>[] attributes, FileAttributeViewCollection supportedViews, Collection<String> nonSupportedAttributeNames)
Collects severalFileAttributeobjects into a map. Each entry of the map can be used withFileSystemProvider.setAttribute(Path, String, Object, LinkOption...). Combined withgetViewName(String)andgetAttributeName(String), each entry can also be used with any of thesetAttributemethods of this class.- Parameters:
attributes- TheFileAttributeobjects to collect.supportedViews- A collection withFileAttributeViewMetadataobjects representing the supported views.nonSupportedAttributeNames- A collection of attribute names that are not supported, regardless of what the supported views say. This can be used for attributes that cannot be set during creation but only afterwards. Elements should not be prefixed with view names.- Returns:
- A map where each key is the name of a given
FileAttributeobject, prefixed with the matching view name where needed. - Throws:
NullPointerException- If any of the givenFileAttributeobjects or the givenFileAttributeViewCollectionobject isnull.UnsupportedOperationException- If any of the givenFileAttributeobjects refers to a view that is not referred to by any of the given supportedFileAttributeViewMetadataobjects, or has a non-supported name, or has a value that does not match theexpected type.
-
-