Path: blob/master/src/java.base/share/classes/sun/nio/fs/AbstractUserDefinedFileAttributeView.java
41159 views
/*1* Copyright (c) 2008, 2021, 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 sun.nio.fs;2627import java.nio.ByteBuffer;28import java.nio.file.attribute.UserDefinedFileAttributeView;29import java.io.IOException;30import java.util.*;3132/**33* Base implementation of UserDefinedAttributeView34*/3536abstract class AbstractUserDefinedFileAttributeView37implements UserDefinedFileAttributeView, DynamicFileAttributeView38{39protected AbstractUserDefinedFileAttributeView() { }4041protected void checkAccess(String file,42boolean checkRead,43boolean checkWrite)44{45assert checkRead || checkWrite;46@SuppressWarnings("removal")47SecurityManager sm = System.getSecurityManager();48if (sm != null) {49if (checkRead)50sm.checkRead(file);51if (checkWrite)52sm.checkWrite(file);53sm.checkPermission(new RuntimePermission("accessUserDefinedAttributes"));54}55}5657@Override58public final String name() {59return "user";60}6162@Override63public final void setAttribute(String attribute, Object value)64throws IOException65{66ByteBuffer bb;67if (value instanceof byte[]) {68bb = ByteBuffer.wrap((byte[])value);69} else {70bb = (ByteBuffer)value;71}72write(attribute, bb);73}7475@Override76public final Map<String,Object> readAttributes(String[] attributes)77throws IOException78{79// names of attributes to return80List<String> names = new ArrayList<>();81for (String name: attributes) {82if (name.equals("*")) {83names = list();84break;85} else {86if (name.isEmpty())87throw new IllegalArgumentException();88names.add(name);89}90}9192// read each value and return in map93Map<String,Object> result = new HashMap<>();94for (String name: names) {95int size = size(name);96byte[] buf = new byte[size];97int n = read(name, ByteBuffer.wrap(buf));98byte[] value = (n == size) ? buf : Arrays.copyOf(buf, n);99result.put(name, value);100}101return result;102}103}104105106