Example usage for java.io File setReadable

List of usage examples for java.io File setReadable

Introduction

In this page you can find the example usage for java.io File setReadable.

Prototype

public boolean setReadable(boolean readable, boolean ownerOnly) 

Source Link

Document

Sets the owner's or everybody's read permission for this abstract pathname.

Usage

From source file:org.apache.kerby.kerberos.tool.token.TokenCache.java

public static void writeToken(String token) {
    File cacheFile = getDefaultTokenCache();

    try {//ww  w  . ja v a  2 s  . c  o  m
        Writer writer = new FileWriterWithEncoding(cacheFile, StandardCharsets.UTF_8);
        writer.write(token);
        writer.flush();
        writer.close();
        // sets read-write permissions to owner only
        cacheFile.setReadable(false, false);
        cacheFile.setReadable(true, true);
        if (!cacheFile.setWritable(true, true)) {
            throw new KrbException("Cache file is not readable.");
        }
    } catch (IOException ioe) {
        // if case of any error we just delete the cache, if user-only
        // write permissions are not properly set a security exception
        // is thrown and the file will be deleted.
        if (cacheFile.delete()) {
            System.err.println("Cache file is deleted.");
        }
    } catch (KrbException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.kerby.kerberos.kerb.integration.test.jaas.TokenCache.java

/**
 * Write the token string to token cache file.
 *
 * @param token The token string//ww w.  java2 s.c  o  m
 */
public static void writeToken(String token) {
    File cacheFile = getDefaultTokenCache();

    try {
        Writer writer = new FileWriterWithEncoding(cacheFile, StandardCharsets.UTF_8);
        writer.write(token);
        writer.flush();
        writer.close();
        // sets read-write permissions to owner only
        cacheFile.setReadable(false, false);
        cacheFile.setReadable(true, true);
        if (!cacheFile.setWritable(true, true)) {
            throw new KrbException("Cache file is not readable.");
        }
    } catch (IOException ioe) {
        // if case of any error we just delete the cache, if user-only
        // write permissions are not properly set a security exception
        // is thrown and the file will be deleted.
        if (cacheFile.delete()) {
            System.err.println("Cache file is deleted.");
        }
    } catch (KrbException e) {
        e.printStackTrace();
    }
}

From source file:pl.lawiusz.lockscreenvisualizerxposed.SettingsActivity.java

@SuppressLint("SetWorldReadable")
private static void makePrefsReadable(Context context) {
    File prefsDir = new File(context.getApplicationInfo().dataDir, "shared_prefs");
    File prefsFile = new File(prefsDir, PREFS_PUBLIC + ".xml");
    if (prefsFile.exists()) {
        if (!prefsFile.setReadable(true, false)) {
            Log.e(TAG, "Error accessing shared preferences!");
        } else if (BuildConfig.DEBUG) {
            Log.d(TAG, "Successfully chmoded prefs!");
        }/*from  www.  j ava 2s  .  co  m*/
    } else
        Log.e(TAG, "No shared preferences file!");
}

From source file:Main.java

public static boolean cacheDrawable(String packageName, int resId, BitmapDrawable drawable) {
    try {/*w ww  . ja  v a2 s. c  om*/
        File f = new File("/data/data/" + PACKAGE_NAME + "/cache/icons/", packageName + "_" + resId);
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        drawable.getBitmap().compress(CompressFormat.PNG, 0, bos);
        new FileOutputStream(f).write(bos.toByteArray());
        f.setReadable(true, false);
        f.setWritable(true, false);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.microsoft.mimickeralarm.ringing.ShareFragment.java

public static Uri saveShareableBitmap(Context context, Bitmap bitmap, String question) {
    drawStamp(context, bitmap, question);

    File tempFile;
    try {//  w w  w. j a  va2  s . c  o m
        tempFile = File.createTempFile("mimicker", ".jpg", context.getCacheDir());
        tempFile.setReadable(true, false);
        tempFile.deleteOnExit();
    } catch (IOException ex) {
        Logger.trackException(ex);
        return null;
    }

    if (tempFile.canWrite()) {
        try {
            FileOutputStream stream = new FileOutputStream(tempFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
            stream.close();
        } catch (IOException ex) {
            Logger.trackException(ex);
            return null;
        }
    }
    return Uri.fromFile(tempFile);
}

From source file:org.jboss.as.forge.util.Files.java

/**
 * Sets the permissions on the file./*from ww w. j  a va  2  s. c  om*/
 *
 * @param file        the file
 * @param permissions the permissions to set
 */
public static void setPermissions(final File file, final FilePermissions permissions) {
    file.setExecutable(permissions.owner().canExecute(),
            !(permissions.group().canExecute() && permissions.pub().canExecute()));
    file.setReadable(permissions.owner().canWrite(),
            !(permissions.group().canWrite() && permissions.pub().canWrite()));
    file.setWritable(permissions.owner().canWrite(),
            !(permissions.group().canWrite() && permissions.pub().canWrite()));
}

From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.PeriodicResourceUpdater.java

static void copyDatabase(final File existingDB, final File newDB) throws IOException {
    final FileInputStream in = new FileInputStream(newDB);
    final FileOutputStream out = new FileOutputStream(existingDB);
    final FileLock lock = out.getChannel().tryLock();
    if (lock != null) {
        LOGGER.info("Updating location database.");
        IOUtils.copy(in, out);//ww  w  . j a va  2  s  . c  o m
        existingDB.setReadable(true, false);
        existingDB.setWritable(true, false);
        lock.release();
        LOGGER.info("Successfully updated location database.");
    } else {
        LOGGER.info("Location database locked by another process.");
    }
    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
}

From source file:ezbake.deployer.impl.Files.java

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void setPosixFilePermissions(File file, Set<PosixFilePermission> filePermissions) {
    if (filePermissions.contains(PosixFilePermission.OWNER_EXECUTE))
        file.setExecutable(true, filePermissions.contains(PosixFilePermission.GROUP_EXECUTE)
                || filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE));
    if (filePermissions.contains(PosixFilePermission.OWNER_READ))
        file.setReadable(true, filePermissions.contains(PosixFilePermission.GROUP_READ)
                || filePermissions.contains(PosixFilePermission.OTHERS_READ));
    if (filePermissions.contains(PosixFilePermission.OWNER_WRITE))
        file.setWritable(true, filePermissions.contains(PosixFilePermission.GROUP_WRITE)
                || filePermissions.contains(PosixFilePermission.OTHERS_WRITE));
}

From source file:ezbake.protect.ezca.EzCABootstrap.java

public static void createAndWriteText(String name, String filePath, byte[] text) {
    OutputStream fof = null;/*from   ww w .  j  a v  a  2  s  .  c om*/
    try {
        File outputFile = new File(filePath, name);
        outputFile.createNewFile();
        outputFile.setWritable(false, false);
        outputFile.setWritable(true, true);
        outputFile.setReadable(false, false);
        outputFile.setReadable(true, true);
        fof = new FileOutputStream(outputFile);
        fof.write(text);
    } catch (IOException e) {
        logger.error("Error creating output file", e);
    } finally {
        if (fof != null) {
            try {
                fof.close();
            } catch (IOException e) {
                logger.warn("Unable to close output stream", e);
            }
        }
    }

}

From source file:com.meltmedia.cadmium.core.FileSystemManager.java

public static void copyAllContent(final String source, final String target, final boolean ignoreHidden)
        throws Exception {
    File sourceFile = new File(source);
    File targetFile = new File(target);
    if (!targetFile.exists()) {
        targetFile.mkdirs();/*from   w  w  w  .j av a 2 s . c  om*/
    }
    if (sourceFile.exists() && sourceFile.canRead() && sourceFile.isDirectory() && targetFile.exists()
            && targetFile.canWrite() && targetFile.isDirectory()) {
        List<File> copyList = new ArrayList<File>();
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File file, String name) {
                return !ignoreHidden || !name.startsWith(".");
            }

        };
        File files[] = sourceFile.listFiles(filter);
        if (files.length > 0) {
            copyList.addAll(Arrays.asList(files));
        }
        for (int index = 0; index < copyList.size(); index++) {
            File aFile = copyList.get(index);
            String relativePath = aFile.getAbsoluteFile().getAbsolutePath()
                    .replaceFirst(sourceFile.getAbsoluteFile().getAbsolutePath(), "");
            if (aFile.isDirectory()) {
                File newDir = new File(target, relativePath);
                if (newDir.mkdir()) {
                    newDir.setExecutable(aFile.canExecute(), false);
                    newDir.setReadable(aFile.canRead(), false);
                    newDir.setWritable(aFile.canWrite(), false);
                    newDir.setLastModified(aFile.lastModified());
                    files = aFile.listFiles(filter);
                    if (files.length > 0) {
                        copyList.addAll(index + 1, Arrays.asList(files));
                    }
                } else {
                    log.warn("Failed to create new subdirectory \"{}\" in the target path \"{}\".",
                            relativePath, target);
                }
            } else {
                File newFile = new File(target, relativePath);
                if (newFile.createNewFile()) {
                    FileInputStream inStream = null;
                    FileOutputStream outStream = null;
                    try {
                        inStream = new FileInputStream(aFile);
                        outStream = new FileOutputStream(newFile);
                        streamCopy(inStream, outStream);
                    } finally {
                        if (inStream != null) {
                            try {
                                inStream.close();
                            } catch (Exception e) {
                            }
                        }
                        if (outStream != null) {
                            try {
                                outStream.flush();
                            } catch (Exception e) {
                            }
                            try {
                                outStream.close();
                            } catch (Exception e) {
                            }
                        }
                    }
                    newFile.setExecutable(aFile.canExecute(), false);
                    newFile.setReadable(aFile.canRead(), false);
                    newFile.setWritable(aFile.canWrite(), false);
                    newFile.setLastModified(aFile.lastModified());
                }
            }
        }
    }
}