1 /* 2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.nio.fs; 27 28 import java.nio.ByteBuffer; 29 import java.nio.file.attribute.UserDefinedFileAttributeView; 30 import java.io.IOException; 31 import java.util.*; 32 33 /** 34 * Base implementation of UserDefinedAttributeView 35 */ 36 37 abstract class AbstractUserDefinedFileAttributeView 38 implements UserDefinedFileAttributeView, DynamicFileAttributeView 39 { AbstractUserDefinedFileAttributeView()40 protected AbstractUserDefinedFileAttributeView() { } 41 checkAccess(String file, boolean checkRead, boolean checkWrite)42 protected void checkAccess(String file, 43 boolean checkRead, 44 boolean checkWrite) 45 { 46 assert checkRead || checkWrite; 47 SecurityManager sm = System.getSecurityManager(); 48 if (sm != null) { 49 if (checkRead) 50 sm.checkRead(file); 51 if (checkWrite) 52 sm.checkWrite(file); 53 sm.checkPermission(new RuntimePermission("accessUserDefinedAttributes")); 54 } 55 } 56 57 @Override name()58 public final String name() { 59 return "user"; 60 } 61 62 @Override setAttribute(String attribute, Object value)63 public final void setAttribute(String attribute, Object value) 64 throws IOException 65 { 66 ByteBuffer bb; 67 if (value instanceof byte[]) { 68 bb = ByteBuffer.wrap((byte[])value); 69 } else { 70 bb = (ByteBuffer)value; 71 } 72 write(attribute, bb); 73 } 74 75 @Override readAttributes(String[] attributes)76 public final Map<String,Object> readAttributes(String[] attributes) 77 throws IOException 78 { 79 // names of attributes to return 80 List<String> names = new ArrayList<>(); 81 for (String name: attributes) { 82 if (name.equals("*")) { 83 names = list(); 84 break; 85 } else { 86 if (name.isEmpty()) 87 throw new IllegalArgumentException(); 88 names.add(name); 89 } 90 } 91 92 // read each value and return in map 93 Map<String,Object> result = new HashMap<>(); 94 for (String name: names) { 95 int size = size(name); 96 byte[] buf = new byte[size]; 97 int n = read(name, ByteBuffer.wrap(buf)); 98 byte[] value = (n == size) ? buf : Arrays.copyOf(buf, n); 99 result.put(name, value); 100 } 101 return result; 102 } 103 } 104