Path: blob/master/src/java.base/share/classes/java/nio/file/attribute/UserDefinedFileAttributeView.java
41161 views
/*1* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file.attribute;2627import java.nio.ByteBuffer;28import java.util.List;29import java.io.IOException;3031/**32* A file attribute view that provides a view of a file's user-defined33* attributes, sometimes known as <em>extended attributes</em>. User-defined34* file attributes are used to store metadata with a file that is not meaningful35* to the file system. It is primarily intended for file system implementations36* that support such a capability directly but may be emulated. The details of37* such emulation are highly implementation specific and therefore not specified.38*39* <p> This {@code FileAttributeView} provides a view of a file's user-defined40* attributes as a set of name/value pairs, where the attribute name is41* represented by a {@code String}. An implementation may require to encode and42* decode from the platform or file system representation when accessing the43* attribute. The value has opaque content. This attribute view defines the44* {@link #read read} and {@link #write write} methods to read the value into45* or write from a {@link ByteBuffer}. This {@code FileAttributeView} is not46* intended for use where the size of an attribute value is larger than {@link47* Integer#MAX_VALUE}.48*49* <p> User-defined attributes may be used in some implementations to store50* security related attributes so consequently, in the case of the default51* provider at least, all methods that access user-defined attributes require the52* {@code RuntimePermission("accessUserDefinedAttributes")} permission when a53* security manager is installed.54*55* <p> The {@link java.nio.file.FileStore#supportsFileAttributeView56* supportsFileAttributeView} method may be used to test if a specific {@link57* java.nio.file.FileStore FileStore} supports the storage of user-defined58* attributes.59*60* <p> Where dynamic access to file attributes is required, the {@link61* java.nio.file.Files#getAttribute getAttribute} method may be used to read62* the attribute value. The attribute value is returned as a byte array (byte[]).63* The {@link java.nio.file.Files#setAttribute setAttribute} method may be used64* to write the value of a user-defined attribute from a buffer (as if by65* invoking the {@link #write write} method), or byte array (byte[]).66*67* @since 1.768*/6970public interface UserDefinedFileAttributeView71extends FileAttributeView72{73/**74* Returns the name of this attribute view. Attribute views of this type75* have the name {@code "user"}.76*/77@Override78String name();7980/**81* Returns a list containing the names of the user-defined attributes.82*83* @return An unmodifiable list containing the names of the file's84* user-defined85*86* @throws IOException87* If an I/O error occurs88* @throws SecurityException89* In the case of the default provider, a security manager is90* installed, and it denies {@link91* RuntimePermission}{@code ("accessUserDefinedAttributes")}92* or its {@link SecurityManager#checkRead(String) checkRead} method93* denies read access to the file.94*/95List<String> list() throws IOException;9697/**98* Returns the size of the value of a user-defined attribute.99*100* @param name101* The attribute name102*103* @return The size of the attribute value, in bytes.104*105* @throws ArithmeticException106* If the size of the attribute is larger than {@link Integer#MAX_VALUE}107* @throws IOException108* If an I/O error occurs109* @throws SecurityException110* In the case of the default provider, a security manager is111* installed, and it denies {@link112* RuntimePermission}{@code ("accessUserDefinedAttributes")}113* or its {@link SecurityManager#checkRead(String) checkRead} method114* denies read access to the file.115*/116int size(String name) throws IOException;117118/**119* Read the value of a user-defined attribute into a buffer.120*121* <p> This method reads the value of the attribute into the given buffer122* as a sequence of bytes, failing if the number of bytes remaining in123* the buffer is insufficient to read the complete attribute value. The124* number of bytes transferred into the buffer is {@code n}, where {@code n}125* is the size of the attribute value. The first byte in the sequence is at126* index {@code p} and the last byte is at index {@code p + n - 1}, where127* {@code p} is the buffer's position. Upon return the buffer's position128* will be equal to {@code p + n}; its limit will not have changed.129*130* <p> <b>Usage Example:</b>131* Suppose we want to read a file's MIME type that is stored as a user-defined132* attribute with the name "{@code user.mimetype}".133* <pre>134* UserDefinedFileAttributeView view =135* Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);136* String name = "user.mimetype";137* ByteBuffer buf = ByteBuffer.allocate(view.size(name));138* view.read(name, buf);139* buf.flip();140* String value = Charset.defaultCharset().decode(buf).toString();141* </pre>142*143* @param name144* The attribute name145* @param dst146* The destination buffer147*148* @return The number of bytes read, possibly zero149*150* @throws IllegalArgumentException151* If the destination buffer is read-only152* @throws IOException153* If an I/O error occurs or there is insufficient space in the154* destination buffer for the attribute value155* @throws SecurityException156* In the case of the default provider, a security manager is157* installed, and it denies {@link158* RuntimePermission}{@code ("accessUserDefinedAttributes")}159* or its {@link SecurityManager#checkRead(String) checkRead} method160* denies read access to the file.161*162* @see #size163*/164int read(String name, ByteBuffer dst) throws IOException;165166/**167* Writes the value of a user-defined attribute from a buffer.168*169* <p> This method writes the value of the attribute from a given buffer as170* a sequence of bytes. The size of the value to transfer is {@code r},171* where {@code r} is the number of bytes remaining in the buffer, that is172* {@code src.remaining()}. The sequence of bytes is transferred from the173* buffer starting at index {@code p}, where {@code p} is the buffer's174* position. Upon return, the buffer's position will be equal to {@code175* p + n}, where {@code n} is the number of bytes transferred; its limit176* will not have changed.177*178* <p> If an attribute of the given name already exists then its value is179* replaced. If the attribute does not exist then it is created. If it180* implementation specific if a test to check for the existence of the181* attribute and the creation of attribute are atomic with respect to other182* file system activities.183*184* <p> Where there is insufficient space to store the attribute, or the185* attribute name or value exceed an implementation specific maximum size186* then an {@code IOException} is thrown.187*188* <p> <b>Usage Example:</b>189* Suppose we want to write a file's MIME type as a user-defined attribute:190* <pre>191* UserDefinedFileAttributeView view =192* Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);193* view.write("user.mimetype", Charset.defaultCharset().encode("text/html"));194* </pre>195*196* @param name197* The attribute name198* @param src199* The buffer containing the attribute value200*201* @return The number of bytes written, possibly zero202*203* @throws IOException204* If an I/O error occurs205* @throws SecurityException206* In the case of the default provider, a security manager is207* installed, and it denies {@link208* RuntimePermission}{@code ("accessUserDefinedAttributes")}209* or its {@link SecurityManager#checkWrite(String) checkWrite}210* method denies write access to the file.211*/212int write(String name, ByteBuffer src) throws IOException;213214/**215* Deletes a user-defined attribute.216*217* @param name218* The attribute name219*220* @throws IOException221* If an I/O error occurs or the attribute does not exist222* @throws SecurityException223* In the case of the default provider, a security manager is224* installed, and it denies {@link225* RuntimePermission}{@code ("accessUserDefinedAttributes")}226* or its {@link SecurityManager#checkWrite(String) checkWrite}227* method denies write access to the file.228*/229void delete(String name) throws IOException;230}231232233