Java Path File Write nio setPosixPermissions(String path, boolean ownerRead, boolean ownerWrite, boolean ownerExecute, boolean groupRead, boolean groupWrite, boolean groupExecute, boolean othersRead, boolean othersWrite, boolean othersExecute)

Here you can find the source of setPosixPermissions(String path, boolean ownerRead, boolean ownerWrite, boolean ownerExecute, boolean groupRead, boolean groupWrite, boolean groupExecute, boolean othersRead, boolean othersWrite, boolean othersExecute)

Description

set Posix Permissions

License

Open Source License

Declaration

public static void setPosixPermissions(String path, boolean ownerRead, boolean ownerWrite, boolean ownerExecute,
            boolean groupRead, boolean groupWrite, boolean groupExecute, boolean othersRead, boolean othersWrite,
            boolean othersExecute) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException, NoSuchFieldException 

Method Source Code


//package com.java2s;
/*-/*ww w  .j  a v  a  2s . c o m*/
 * Copyright (C) 2014 Erik Larsson
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void setPosixPermissions(String path, boolean ownerRead, boolean ownerWrite, boolean ownerExecute,
            boolean groupRead, boolean groupWrite, boolean groupExecute, boolean othersRead, boolean othersWrite,
            boolean othersExecute) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
        Class<?> fileSystemsClass = Class.forName("java.nio.file.FileSystems");
        Class<?> fileSystemClass = Class.forName("java.nio.file.FileSystem");
        Class<?> pathClass = Class.forName("java.nio.file.Path");
        Class<?> posixFileAttributeViewClass = Class.forName("java.nio.file.attribute.PosixFileAttributeView");
        Class<?> filesClass = Class.forName("java.nio.file.Files");
        Class<?> linkOptionClass = Class.forName("java.nio.file.LinkOption");
        Class<?> posixFilePermissionClass = Class.forName("java.nio.file.attribute.PosixFilePermission");

        /* java.nio.file.FileSystem defaultFileSystem =
           java.nio.file.FileSystems.getDefault(); */
        Method fileSystemsGetDefaultMethod = fileSystemsClass.getMethod("getDefault");
        Object defaultFileSystemObject = fileSystemsGetDefaultMethod.invoke(null);

        /* java.nio.file.Path p = defaultFileSystem.getPath(path); */
        Method fileSystemGetPathMethod = fileSystemClass.getMethod("getPath", String.class, String[].class);
        Object pObject = fileSystemGetPathMethod.invoke(defaultFileSystemObject, path, new String[0]);

        /* java.nio.file.attribute.PosixFileAttributeView attrView =
         *         java.nio.file.Files.getFileAttributeView(p,
         *         java.nio.file.attribute.PosixFileAttributeView.class,
         *         java.nio.file.LinkOption.NOFOLLOW_LINKS); */
        Field noFollowLinksField = linkOptionClass.getField("NOFOLLOW_LINKS");
        Object noFollowLinksObject = noFollowLinksField.get(null);

        Object linkOptionsArray = Array.newInstance(linkOptionClass, 1);
        Array.set(linkOptionsArray, 0, noFollowLinksObject);

        Method getFileAttributeViewMethod = filesClass.getMethod("getFileAttributeView", pathClass, Class.class,
                linkOptionsArray.getClass());
        Object attrViewObject = getFileAttributeViewMethod.invoke(null, pObject, posixFileAttributeViewClass,
                linkOptionsArray);

        if (attrViewObject == null) {
            /* No PosixFileAttributeView available. Platform does not support
             * POSIX attributes. Just return quietly here. */
            return;
        }

        HashSet<Object> perms = new HashSet<Object>();

        if (ownerRead) {
            Field curField = posixFilePermissionClass.getField("OWNER_READ");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        if (ownerWrite) {
            Field curField = posixFilePermissionClass.getField("OWNER_WRITE");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        if (ownerExecute) {
            Field curField = posixFilePermissionClass.getField("OWNER_EXECUTE");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        if (groupRead) {
            Field curField = posixFilePermissionClass.getField("GROUP_READ");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        if (groupWrite) {
            Field curField = posixFilePermissionClass.getField("GROUP_WRITE");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        if (groupExecute) {
            Field curField = posixFilePermissionClass.getField("GROUP_EXECUTE");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        if (othersRead) {
            Field curField = posixFilePermissionClass.getField("OTHERS_READ");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        if (othersWrite) {
            Field curField = posixFilePermissionClass.getField("OTHERS_WRITE");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        if (othersExecute) {
            Field curField = posixFilePermissionClass.getField("OTHERS_EXECUTE");
            Object curObject = curField.get(null);
            perms.add(curObject);
        }

        /* attrView.setPermissions(perms); */
        Method posixFileAttributeViewSetPermissionMethod = posixFileAttributeViewClass.getMethod("setPermissions",
                Set.class);
        try {
            posixFileAttributeViewSetPermissionMethod.invoke(attrViewObject, perms);
        } catch (InvocationTargetException ex) {
            final Throwable cause = ex.getCause();
            if (cause instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) cause;
            } else if (cause instanceof NoSuchMethodException) {
                throw (NoSuchMethodException) cause;
            } else if (cause instanceof IllegalAccessException) {
                throw (IllegalAccessException) cause;
            } else if (cause instanceof IllegalArgumentException) {
                throw (IllegalArgumentException) cause;
            } else if (cause instanceof InvocationTargetException) {
                throw (InvocationTargetException) cause;
            } else if (cause instanceof NoSuchFieldException) {
                throw (NoSuchFieldException) cause;
            } else if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else {
                throw ex;
            }
        }
    }
}

Related

  1. saveFile(String path, String text)
  2. saveHeaders(Path path, ArrayList headers)
  3. saveProperties(Path filePath, Properties prop)
  4. saveTo(String path, InputStream in)
  5. saveToFile(String path, byte[] text)
  6. srcImageToSavePath(String src, Path saveFolder)
  7. write(List list, String outputPath)
  8. write(Object obj, String filePath)
  9. write(Path p)