Java APIs for Extended Attributes

Java APIs to manage extended attributes

You can set, retrieve, and remove extended attributes on files, directories, and FUSE mounted file path using Extended Attribute Java APIs.

Set Extended Attributes

To set extended attributes, use the following APIs:

public void setXAttr(Path path, String name, byte[] value) throws IOException
Set an extended attribute on a file or directory. The name must be prefixed with the namespace followed by ".". For example, "user.attr". By default, if a given extended attribute exists, then it will be replaced with the specified attribute.
public void setXAttr(Path path, String name, byte[] value, Enum<SetXAttrSetFlag> flag) throws IOException
Set an extended attribute on a file or directory. The name must be prefixed with the namespace followed by ".". For example, "user.attr". The XAttrSetFlag value can be:
  • CREATE to create a new extended attribute. An error is returned if an extended attribute with the given name already exists.
  • REPLACE to replace an existing extended attribute. An error is returned if the specified extended attribute does not already exist.

Retrieve Extended Attributes

To retrieve extended attributes, use the following APIs:

public byte[] getXAttr(Path path, String name) throws IOException
Get an extended attribute name and value for a file or directory. The name must be prefixed with the namespace followed by ".". For example, "user.attr".
public Map<String,byte[]> getXAttrs(Path path) throws IOException
Get all the extended attribute name/value pairs for a file or directory. Only those extended attributes that the logged-in user has permissions to view, are returned.
public Map<String,byte[]> getXAttrs(Path path, List<String> names) throws IOException
Get the extended attributes specified by the given list of names. Only those extended attributes that the logged-in user has permissions to view, are returned.
public List<String> listXAttrs(Path path) throws IOException
Get all the extended attribute names for a file or directory. Only those extended attribute names that the logged-in user has permissions to view, are returned.

Remove Extended Attributes

To remove an extended attribute associated with a file or directory, use the following API:

public void removeXAttr(Path path, String name) throws IOException
Remove an extended attribute of a file or directory. The name must be prefixed with the namespace followed by ".". For example, "user.attr".