Class FileAttributeSupport


  • public final class FileAttributeSupport
    extends Object
    A utility class for file attributes.

    Reading attributes

    Methods of this class can be used to implement FileSystemProvider.readAttributes(Path, String, LinkOption...) as follows, using a FileAttributeViewCollection instance called views:
    
     @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 FileAttributeView sub types, the above setup can still be used. Just provide a custom FileAttributeViewMetadata instance, and use populateAttributeMap(Map, String, Set, Supplier) for any custom properties. For instance, where MyFileAttributes extends BasicFileAttributes and has a method myAttribute() 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 implement FileSystemProvider.setAttribute(Path, String, Object, LinkOption...) as follows, using constants from FileAttributeConstants:
    
     @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 FileAttributeView sub types, the above setup can still be used. Use a switch or if statement to handle your custom attributes, and call any of the setAttribute methods of this class for the default case or else block. For instance, where MyFileAttributeView extends BasicFileAttributeView and has a method setMyAttribute(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

    The toAttributeMap methods of this class can be used to collect FileAttribute objects into maps where the key and value of each entry can be passed to any of the setAttribute methods of this class. This allows file attributes to be set from methods like FileSystemProvider.createDirectory(Path, FileAttribute...) or FileSystemProvider.newByteChannel(Path, Set, FileAttribute...).
    Author:
    Rob Spoor
    Since:
    2.2