List of usage examples for java.io FileOutputStream write
public void write(byte b[]) throws IOException
b.length
bytes from the specified byte array to this file output stream. From source file:Main.java
public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException { if (bitmap == null || fileName == null || context == null) return;//ww w . ja va 2s .c om FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream); byte[] bytes = stream.toByteArray(); fos.write(bytes); fos.close(); }
From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Saves the key pair to the disk.//from w w w .j a v a 2 s .c om * * @param directory * The directory to save to * @param keyPair * The key pair to save * @throws Exception * If an error occurs */ public static void save(File directory, KeyPair keyPair) throws Exception { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store the public key. X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream out = null; try { out = new FileOutputStream(directory + "/public.key"); out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } // Store the private key. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); try { out = new FileOutputStream(directory + "/private.key"); out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } }
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
private static void saveKeys(PrivateKey privkey, PublicKey pubkey) { km_log.i("Saving keys to disk."); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubkey.getEncoded()); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privkey.getEncoded()); try {//w w w. j a v a 2 s . c om km_log.i(pubkeyFile.createNewFile() ? "Creation of public key file successful." : "Creation of public key file failed!"); FileOutputStream pubOut = new FileOutputStream(pubkeyFile); pubOut.write(Base64.encodeBase64(pubSpec.getEncoded())); pubOut.flush(); pubOut.close(); } catch (IOException e) { km_log.e("Error while saving public key!"); e.printStackTrace(); } try { km_log.i(privkeyFile.createNewFile() ? "Creation of private key file successful." : "Creation of private key file failed!"); FileOutputStream privOut = new FileOutputStream(privkeyFile); privOut.write(Base64.encodeBase64(privSpec.getEncoded())); privOut.flush(); privOut.close(); } catch (IOException e) { km_log.e("Error while saving private key!"); e.printStackTrace(); } km_log.i("Finished saving keys to disk. Operation appears successful."); }
From source file:Main.java
/** * This method allow to append a message to an error file * * @param filename//from w ww . j a v a 2 s . com * the error file to write * @param message * the message to append to file */ public static void errorLog(String filename, String message) { try { File file = new File(filename); String path = file.getParent(); if (path != null) { new File(path).mkdirs(); } FileOutputStream fos = new FileOutputStream(file, true); // true->append String time = "" + (new java.sql.Timestamp(System.currentTimeMillis())); message = time + " " + message + "\n"; fos.write(message.getBytes()); fos.close(); } catch (Exception e) { System.err.println("Unable to write file: " + filename); e.printStackTrace(); } }
From source file:com.goliathonline.android.kegbot.util.BitmapUtils.java
/** * Only call this method from the main (UI) thread. The {@link OnFetchCompleteListener} callback * be invoked on the UI thread, but image fetching will be done in an {@link AsyncTask}. * * @param cookie An arbitrary object that will be passed to the callback. *//*from w w w. ja v a 2 s. c om*/ public static void fetchImage(final Context context, final String url, final BitmapFactory.Options decodeOptions, final Object cookie, final OnFetchCompleteListener callback) { new AsyncTask<String, Void, Bitmap>() { @Override protected Bitmap doInBackground(String... params) { final String url = params[0]; if (TextUtils.isEmpty(url)) { return null; } // First compute the cache key and cache file path for this URL File cacheFile = null; try { MessageDigest mDigest = MessageDigest.getInstance("SHA-1"); mDigest.update(url.getBytes()); final String cacheKey = bytesToHexString(mDigest.digest()); if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { cacheFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android" + File.separator + "data" + File.separator + context.getPackageName() + File.separator + "cache" + File.separator + "bitmap_" + cacheKey + ".tmp"); } } catch (NoSuchAlgorithmException e) { // Oh well, SHA-1 not available (weird), don't cache bitmaps. } if (cacheFile != null && cacheFile.exists()) { Bitmap cachedBitmap = BitmapFactory.decodeFile(cacheFile.toString(), decodeOptions); if (cachedBitmap != null) { return cachedBitmap; } } try { // TODO: check for HTTP caching headers final HttpClient httpClient = SyncService.getHttpClient(context.getApplicationContext()); final HttpResponse resp = httpClient.execute(new HttpGet(url)); final HttpEntity entity = resp.getEntity(); final int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK || entity == null) { return null; } final byte[] respBytes = EntityUtils.toByteArray(entity); // Write response bytes to cache. if (cacheFile != null) { try { cacheFile.getParentFile().mkdirs(); cacheFile.createNewFile(); FileOutputStream fos = new FileOutputStream(cacheFile); fos.write(respBytes); fos.close(); } catch (FileNotFoundException e) { Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e); } catch (IOException e) { Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e); } } // Decode the bytes and return the bitmap. return BitmapFactory.decodeByteArray(respBytes, 0, respBytes.length, decodeOptions); } catch (Exception e) { Log.w(TAG, "Problem while loading image: " + e.toString(), e); } return null; } @Override protected void onPostExecute(Bitmap result) { callback.onFetchComplete(cookie, result); } }.execute(url); }
From source file:tw.idv.gasolin.pycontw2012.util.BitmapUtils.java
/** * Only call this method from the main (UI) thread. The * {@link OnFetchCompleteListener} callback be invoked on the UI thread, but * image fetching will be done in an {@link AsyncTask}. * // www. j ava 2s .com * @param cookie * An arbitrary object that will be passed to the callback. */ public static void fetchImage(final Context context, final String url, final BitmapFactory.Options decodeOptions, final Object cookie, final OnFetchCompleteListener callback) { new AsyncTask<String, Void, Bitmap>() { @Override protected Bitmap doInBackground(String... params) { final String url = params[0]; if (TextUtils.isEmpty(url)) { return null; } // First compute the cache key and cache file path for this URL File cacheFile = null; try { MessageDigest mDigest = MessageDigest.getInstance("SHA-1"); mDigest.update(url.getBytes()); final String cacheKey = bytesToHexString(mDigest.digest()); if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { cacheFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android" + File.separator + "data" + File.separator + context.getPackageName() + File.separator + "cache" + File.separator + "bitmap_" + cacheKey + ".tmp"); } } catch (NoSuchAlgorithmException e) { // Oh well, SHA-1 not available (weird), don't cache // bitmaps. } if (cacheFile != null && cacheFile.exists()) { Bitmap cachedBitmap = BitmapFactory.decodeFile(cacheFile.toString(), decodeOptions); if (cachedBitmap != null) { return cachedBitmap; } } try { // TODO: check for HTTP caching headers final HttpClient httpClient = SyncService.getHttpClient(context.getApplicationContext()); final HttpResponse resp = httpClient.execute(new HttpGet(url)); final HttpEntity entity = resp.getEntity(); final int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK || entity == null) { return null; } final byte[] respBytes = EntityUtils.toByteArray(entity); // Write response bytes to cache. if (cacheFile != null) { try { cacheFile.getParentFile().mkdirs(); cacheFile.createNewFile(); FileOutputStream fos = new FileOutputStream(cacheFile); fos.write(respBytes); fos.close(); } catch (FileNotFoundException e) { Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e); } catch (IOException e) { Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e); } } // Decode the bytes and return the bitmap. return BitmapFactory.decodeByteArray(respBytes, 0, respBytes.length, decodeOptions); } catch (Exception e) { Log.w(TAG, "Problem while loading image: " + e.toString(), e); } return null; } @Override protected void onPostExecute(Bitmap result) { callback.onFetchComplete(cookie, result); } }.execute(url); }
From source file:Main.java
static void createOptOut(Context context, boolean optedOut) { FileOutputStream stream = null; try {/*from ww w. ja v a 2 s . co m*/ stream = context.openFileOutput(QCMEASUREMENT_OPTOUT_STRING, Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE); stream.write(optedOut ? 1 : 0); } catch (Exception ignored) { } finally { try { if (stream != null) { stream.close(); } } catch (IOException ignored) { } } }
From source file:Main.java
public static void saveUtfFileWithBOM(File file, String content) throws IOException { BufferedWriter bw = null;/*from w ww. j a v a 2s . c om*/ OutputStreamWriter osw = null; FileOutputStream fos = new FileOutputStream(file); try { // write UTF8 BOM mark if file is empty if (file.length() < 1) { final byte[] bom = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; fos.write(bom); } osw = new OutputStreamWriter(fos, "UTF-8"); bw = new BufferedWriter(osw); if (content != null) { bw.write(content); } } catch (IOException ex) { throw ex; } finally { try { bw.close(); fos.close(); } catch (Exception ex) { } } }
From source file:eu.scape_project.tool.toolwrapper.core.utils.Utils.java
/** * Method useful to write content that was previously merged to a certain * Velocity template into a file//from www.j a va 2 s. com * * @param outputDirectory * directory where to write to * @param childDirectory * child directory if the information being written should be * placed in here * @param filename * name of the file being created * @param w * {@link StringWriter} that holds the information that is going * to be written * @param makeItExecutable * if the file being created should be executable * @return true if the file was successfully written, false otherwise * */ public static boolean writeTemplateContent(File outputDirectory, String childDirectory, String filename, StringWriter w, boolean makeItExecutable) { boolean res = true; File directory; if (childDirectory != null) { directory = new File(outputDirectory, childDirectory); } else { directory = outputDirectory; } File file = new File(directory, filename); FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(w.toString().getBytes(Charset.defaultCharset())); } catch (FileNotFoundException e) { log.error(e); res = false; } catch (IOException e) { log.error(e); res = false; } finally { if (fos != null) { try { fos.close(); file.setExecutable(makeItExecutable); } catch (IOException e) { log.error(e); res = false; } } } return res; }
From source file:io.fabric8.tooling.archetype.ArchetypeUtils.java
public static void writeFile(File file, String data, boolean append) { try {// ww w.j a v a2 s .c om FileOutputStream fos = new FileOutputStream(file, append); fos.write(data.getBytes()); fos.close(); } catch (Exception e) { // ignore } }