1 /*
   2  * Copyright (c) 2008, 2009, 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 {
  40     protected AbstractUserDefinedFileAttributeView() { }
  41 
  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
  58     public final String name() {
  59         return "user";
  60     }
  61 
  62     private Object getAttribute(String attribute) throws IOException {
  63         int size;
  64         try {
  65             size = size(attribute);
  66         } catch (IOException e) {
  67             // not found or some other I/O error
  68             if (list().contains(attribute))
  69                 throw e;
  70             return null;
  71         }
  72 
  73         byte[] buf = new byte[size];
  74         int n = read(attribute, ByteBuffer.wrap(buf));
  75         return (n == size) ? buf : Arrays.copyOf(buf, n);
  76     }
  77 
  78     @Override
  79     public final void setAttribute(String attribute, Object value)
  80         throws IOException
  81     {
  82         ByteBuffer bb;
  83         if (value instanceof byte[]) {
  84             bb = ByteBuffer.wrap((byte[])value);
  85         } else {
  86             bb = (ByteBuffer)value;
  87         }
  88         write(attribute, bb);
  89     }
  90 
  91     @Override
  92     public final Map<String,Object> readAttributes(String[] attributes)
  93         throws IOException
  94     {
  95         // names of attributes to return
  96         List<String> names = new ArrayList<>();
  97 
  98         for (String name: attributes) {
  99             if (name.equals("*")) {
 100                 names = list();
 101                 break;
 102             } else {
 103                 names.add(name);
 104             }
 105         }
 106 
 107         // read each value and return in map
 108         Map<String,Object> result = new HashMap<>();
 109         for (String name: names) {
 110             Object value = getAttribute(name);
 111             if (value != null)
 112                 result.put(name, value);
 113         }
 114 
 115         return result;
 116     }
 117 }