List of usage examples for java.io File setWritable
public boolean setWritable(boolean writable, boolean ownerOnly)
From source file:Main.java
public static boolean cacheDrawable(String packageName, int resId, BitmapDrawable drawable) { try {//from w w w . j a va2 s . c o m 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:org.apache.kerby.kerberos.tool.token.TokenCache.java
public static void writeToken(String token) { File cacheFile = getDefaultTokenCache(); try {// www .j ava 2s.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/*from w ww . j a va 2 s . c om*/ */ 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:ezbake.protect.ezca.EzCABootstrap.java
public static void createAndWriteText(String name, String filePath, byte[] text) { OutputStream fof = null;//from w w w.ja v a 2 s.c o m 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:org.apache.cordova.mediacapture.Capture.java
private static void createWritableFile(File file) throws IOException { file.createNewFile();/*from w ww . j a va 2 s . co m*/ file.setWritable(true, false); }
From source file:io.agi.framework.persistence.PersistenceUtil.java
public static boolean SaveSubtree(String entityName, String type, String path) { boolean success = false; String subtree = ExportSubtree(entityName, type); File file = new File(path); try {//ww w .j ava2 s .co m FileUtils.writeStringToFile(file, subtree); file.setWritable(true, false); success = true; } catch (IOException e) { _logger.error("Unable to save subtree for entity: " + entityName); _logger.error(e.toString(), e); } return success; }
From source file:ezbake.protect.ezca.EzCABootstrap.java
public static void createAndWriteTarball(String name, AppCerts certs, String filePath) { TarArchiveOutputStream os = null;/*from w w w. ja v a 2s .c om*/ try { File outputFile = new File(filePath, name + ".tar.gz"); outputFile.createNewFile(); outputFile.setWritable(false, false); outputFile.setWritable(true, true); outputFile.setReadable(false, false); outputFile.setReadable(true, true); FileOutputStream fos = new FileOutputStream(outputFile); CompressorOutputStream cos = new CompressorStreamFactory() .createCompressorOutputStream(CompressorStreamFactory.GZIP, fos); os = new TarArchiveOutputStream(cos); // For each field in the app certs, create an entry in the tar archive for (AppCerts._Fields field : AppCerts._Fields.values()) { Object o = certs.getFieldValue(field); if (o instanceof byte[]) { String fieldName = field.getFieldName().replace("_", "."); addTarArchiveEntry(os, fieldName, (byte[]) o); } } } catch (FileNotFoundException e) { logger.error("Unable to write tarball", e); } catch (CompressorException e) { logger.error("Error compressing tarball", e); } catch (IOException e) { logger.error("Error creating output file for tarball", e); } finally { if (os != null) { try { os.finish(); os.close(); } catch (IOException e) { logger.warn("Unable to close output stream", e); } } } }
From source file:org.jboss.as.forge.util.Files.java
/** * Sets the permissions on the file./*from w w w. j a va2 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);// w w w. j a va 2 s .c om 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:ProxyAuthTest.java
private static void generateData() throws Exception { String fileData[] = { "1|aaa", "2|bbb", "3|ccc", "4|ddd", "5|eee", }; File tmpFile = File.createTempFile(tabName, ".data"); tmpFile.deleteOnExit();/*from w ww .jav a 2s . c o m*/ tabDataFileName = tmpFile.getPath(); FileWriter fstream = new FileWriter(tabDataFileName); BufferedWriter out = new BufferedWriter(fstream); for (String line : fileData) { out.write(line); out.newLine(); } out.close(); tmpFile.setWritable(true, true); }