143 * Creates a {@link FileAttribute}, encapsulating a copy of the given file 144 * permissions, suitable for passing to the {@link java.nio.file.Files#createFile 145 * createFile} or {@link java.nio.file.Files#createDirectory createDirectory} 146 * methods. 147 * 148 * @param perms 149 * the set of permissions 150 * 151 * @return an attribute encapsulating the given file permissions with 152 * {@link FileAttribute#name name} {@code "posix:permissions"} 153 * 154 * @throws ClassCastException 155 * if the set contains elements that are not of type {@code 156 * PosixFilePermission} 157 */ 158 public static FileAttribute<Set<PosixFilePermission>> 159 asFileAttribute(Set<PosixFilePermission> perms) 160 { 161 // copy set and check for nulls (CCE will be thrown if an element is not 162 // a PosixFilePermission) 163 perms = new HashSet<PosixFilePermission>(perms); 164 for (PosixFilePermission p: perms) { 165 if (p == null) 166 throw new NullPointerException(); 167 } 168 final Set<PosixFilePermission> value = perms; 169 return new FileAttribute<Set<PosixFilePermission>>() { 170 @Override 171 public String name() { 172 return "posix:permissions"; 173 } 174 @Override 175 public Set<PosixFilePermission> value() { 176 return Collections.unmodifiableSet(value); 177 } 178 }; 179 } 180 } | 143 * Creates a {@link FileAttribute}, encapsulating a copy of the given file 144 * permissions, suitable for passing to the {@link java.nio.file.Files#createFile 145 * createFile} or {@link java.nio.file.Files#createDirectory createDirectory} 146 * methods. 147 * 148 * @param perms 149 * the set of permissions 150 * 151 * @return an attribute encapsulating the given file permissions with 152 * {@link FileAttribute#name name} {@code "posix:permissions"} 153 * 154 * @throws ClassCastException 155 * if the set contains elements that are not of type {@code 156 * PosixFilePermission} 157 */ 158 public static FileAttribute<Set<PosixFilePermission>> 159 asFileAttribute(Set<PosixFilePermission> perms) 160 { 161 // copy set and check for nulls (CCE will be thrown if an element is not 162 // a PosixFilePermission) 163 perms = new HashSet<>(perms); 164 for (PosixFilePermission p: perms) { 165 if (p == null) 166 throw new NullPointerException(); 167 } 168 final Set<PosixFilePermission> value = perms; 169 return new FileAttribute<>() { 170 @Override 171 public String name() { 172 return "posix:permissions"; 173 } 174 @Override 175 public Set<PosixFilePermission> value() { 176 return Collections.unmodifiableSet(value); 177 } 178 }; 179 } 180 } |