Java Posix toOctalFileMode(Set permissions)

Here you can find the source of toOctalFileMode(Set permissions)

Description

Converts a set of PosixFilePermission to chmod-style octal file mode.

License

Open Source License

Declaration

public static int toOctalFileMode(Set<PosixFilePermission> permissions) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 SAP SE and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from ww w  .j ava2  s . c om*/
 *    SAP SE - initial API and implementation
 *******************************************************************************/

import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

public class Main {
    private static final int OWNER_READ_FILEMODE = 0400;
    private static final int OWNER_WRITE_FILEMODE = 0200;
    private static final int OWNER_EXEC_FILEMODE = 0100;
    private static final int GROUP_READ_FILEMODE = 0040;
    private static final int GROUP_WRITE_FILEMODE = 0020;
    private static final int GROUP_EXEC_FILEMODE = 0010;
    private static final int OTHERS_READ_FILEMODE = 0004;
    private static final int OTHERS_WRITE_FILEMODE = 0002;
    private static final int OTHERS_EXEC_FILEMODE = 0001;

    /**
     * Converts a set of {@link PosixFilePermission} to chmod-style octal file mode.
     */
    public static int toOctalFileMode(Set<PosixFilePermission> permissions) {
        int result = 0;
        for (PosixFilePermission permissionBit : permissions) {
            switch (permissionBit) {
            case OWNER_READ:
                result |= OWNER_READ_FILEMODE;
                break;
            case OWNER_WRITE:
                result |= OWNER_WRITE_FILEMODE;
                break;
            case OWNER_EXECUTE:
                result |= OWNER_EXEC_FILEMODE;
                break;
            case GROUP_READ:
                result |= GROUP_READ_FILEMODE;
                break;
            case GROUP_WRITE:
                result |= GROUP_WRITE_FILEMODE;
                break;
            case GROUP_EXECUTE:
                result |= GROUP_EXEC_FILEMODE;
                break;
            case OTHERS_READ:
                result |= OTHERS_READ_FILEMODE;
                break;
            case OTHERS_WRITE:
                result |= OTHERS_WRITE_FILEMODE;
                break;
            case OTHERS_EXECUTE:
                result |= OTHERS_EXEC_FILEMODE;
                break;
            }
        }
        return result;
    }
}

Related

  1. givePerms(File f, Set perms, boolean recursive)
  2. intFilePermissions(Set permissions)
  3. mapValue(PosixFilePermission p)
  4. posixFilePermissions(int mode)
  5. setPermissionsToFile(File f, Collection perms)
  6. toUnixFileMode(Set perms)
  7. unixModeToPosixSet(int unixMode)