Example usage for java.nio.file AccessMode WRITE

List of usage examples for java.nio.file AccessMode WRITE

Introduction

In this page you can find the example usage for java.nio.file AccessMode WRITE.

Prototype

AccessMode WRITE

To view the source code for java.nio.file AccessMode WRITE.

Click Source Link

Document

Test write access.

Usage

From source file:com.github.fge.ftpfs.io.commonsnetimpl.CommonsNetFtpAgent.java

private static EnumSet<AccessMode> calculateAccess(final FTPFile file) {
    final EnumSet<AccessMode> ret = EnumSet.noneOf(AccessMode.class);
    if (file.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION))
        ret.add(AccessMode.READ);
    if (file.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION))
        ret.add(AccessMode.WRITE);
    if (file.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION))
        ret.add(AccessMode.EXECUTE);
    return ret;/*from ww w .j  a  va2 s  .co m*/
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void checkAccess(CryptoPath cleartextPath, AccessMode... modes) throws IOException {
    if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
        Set<PosixFilePermission> permissions = readAttributes(cleartextPath, PosixFileAttributes.class)
                .permissions();// www.j a v  a2s. com
        boolean accessGranted = true;
        for (AccessMode accessMode : modes) {
            switch (accessMode) {
            case READ:
                accessGranted &= permissions.contains(PosixFilePermission.OWNER_READ);
                break;
            case WRITE:
                accessGranted &= permissions.contains(PosixFilePermission.OWNER_WRITE);
                break;
            case EXECUTE:
                accessGranted &= permissions.contains(PosixFilePermission.OWNER_EXECUTE);
                break;
            default:
                throw new UnsupportedOperationException("AccessMode " + accessMode + " not supported.");
            }
        }
        if (!accessGranted) {
            throw new AccessDeniedException(cleartextPath.toString());
        }
    } else if (fileStore.supportsFileAttributeView(DosFileAttributeView.class)) {
        DosFileAttributes attrs = readAttributes(cleartextPath, DosFileAttributes.class);
        if (ArrayUtils.contains(modes, AccessMode.WRITE) && attrs.isReadOnly()) {
            throw new AccessDeniedException(cleartextPath.toString(), null, "read only file");
        }
    } else {
        // read attributes to check for file existence / throws IOException if file does not exist
        readAttributes(cleartextPath, BasicFileAttributes.class);
    }
}