de.carne.nio.file.attribute.FileAttributes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-default Show documentation
Show all versions of java-default Show documentation
Common Java code shared between my projects
/*
* Copyright (c) 2016-2018 Holger de Carne and contributors, All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package de.carne.nio.file.attribute;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
/**
* Utility class providing {@linkplain FileAttribute} related functions.
*/
public final class FileAttributes {
private FileAttributes() {
// Prevent instantiation
}
private static final String POSIX = "posix";
/**
* Get the {@linkplain FileSystem} specific best choice for the access rights for a user's private directory.
*
* @param fileSystem The {@linkplain FileSystem} to determine the access rights for.
* @return The access rights to use for a user's private directory.
*/
@SuppressWarnings("squid:S1452")
public static FileAttribute>[] userDirectoryDefault(FileSystem fileSystem) {
List> userDefault = new ArrayList<>();
Set fileAttributeViews = fileSystem.supportedFileAttributeViews();
if (fileAttributeViews.contains(POSIX)) {
userDefault.add(PosixFilePermissions.asFileAttribute(EnumSet.of(PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE)));
}
return userDefault.toArray(new FileAttribute>[userDefault.size()]);
}
/**
* Get the {@linkplain FileSystem} specific best choice for the access rights for a user's private directory.
*
* @param path The {@linkplain Path} to determine the access rights for.
* @return The access rights to use for a user's private directory.
*/
@SuppressWarnings("squid:S1452")
public static FileAttribute>[] userDirectoryDefault(Path path) {
return userDirectoryDefault(path.getFileSystem());
}
}