public static boolean flushToFile(String file, String data, boolean append) { if (data == null) { return true; } try { FileOutputStream fs = new FileOutputStream(file, append); fs.write(data.getBytes()); fs.flush(); fs.close(); return true; } catch (Exception ignored) { return false; } } }
@Override public void save(String key, String value) throws IOException { if (file != null) { synchronized (this) { properties.put(key, value); final FileOutputStream out = new FileOutputStream(file); properties.store(out, null); out.flush(); out.close(); } } else { throw new IOException(location + " is not writeable"); } }
@Override public void run() { // Do not write the file if committedOffset has never been updated. if (committedOffset.get() == DEFAULT_COMMITTED_OFFSET) { return; } try (final FileOutputStream fos = new FileOutputStream(committedReadOffsetFile)) { fos.write(String.valueOf(committedOffset.get()).getBytes(StandardCharsets.UTF_8)); // flush stream fos.flush(); // actually sync to disk fos.getFD().sync(); } catch (SyncFailedException e) { LOG.error("Cannot sync " + committedReadOffsetFile.getAbsolutePath() + " to disk. Continuing anyway," + " but there is no guarantee that the file has been written.", e); } catch (IOException e) { LOG.error("Cannot write " + committedReadOffsetFile.getAbsolutePath() + " to disk.", e); } } }
@Test public void testCopy_ByteArray_OutputStream() throws Exception { final File destination = TestUtils.newFile(getTestDirectory(), "copy8.txt"); byte[] in; try (FileInputStream fin = new FileInputStream(m_testFile)) { // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid. in = IOUtils.toByteArray(fin); } try (FileOutputStream fout = new FileOutputStream(destination)) { CopyUtils.copy(in, fout); fout.flush(); TestUtils.checkFile(destination, m_testFile); TestUtils.checkWrite(fout); } TestUtils.deleteFile(destination); }
private void saveInArtifactoryCache(String yamlData) { if (yamlData == null) { LOG.warn("Will not save null data into the artifactory cache"); return; } String localFileName = localCacheDir + File.separator + cacheFilename; File cacheFile = new File(localFileName); try (FileOutputStream fos = new FileOutputStream(cacheFile)) { fos.write(yamlData.getBytes()); fos.flush(); } catch (IOException e) { LOG.error("Received exception when writing file {}. Attempting delete", localFileName, e); try { cacheFile.delete(); } catch (Exception deleteException) { LOG.error("Received exception when deleting file {}.", localFileName, deleteException); } } }
/** * Checks if writing {@code len} bytes would go over threshold, and switches to file buffering if * so. */ private void update(int len) throws IOException { if (file == null && (memory.getCount() + len > fileThreshold)) { File temp = File.createTempFile("FileBackedOutputStream", null); if (resetOnFinalize) { // Finalizers are not guaranteed to be called on system shutdown; // this is insurance. temp.deleteOnExit(); } FileOutputStream transfer = new FileOutputStream(temp); transfer.write(memory.getBuffer(), 0, memory.getCount()); transfer.flush(); // We've successfully transferred the data; switch to writing to file out = transfer; file = temp; memory = null; } } }
/** * Cache an element. * * @param bitmap The bitmap to be put in the cache. * @param fileName A string representing the name of the file to be cached. */ synchronized void put(Bitmap bitmap, String fileName) { final File file = buildFileFromFilename(fileName); if (!file.exists()) { try { final FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException e) { Log.e(TAG, e.getMessage()); } } }
/** * Save bitmap to a .png file * * @param bitmap * @param name */ public static void saveBitmapToPNG(Bitmap bitmap, String name) throws FileNotFoundException, IOException { File file = new File(name); FileOutputStream out = new FileOutputStream(file); if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) { out.flush(); out.close(); } }
public URL uploadStream(ActiveMQBlobMessage message, InputStream in) throws JMSException, IOException { File f = getFile(message); try(FileOutputStream out = new FileOutputStream(f)) { byte[] buffer = new byte[policy.getBufferSize()]; for (int c = in.read(buffer); c != -1; c = in.read(buffer)) { out.write(buffer, 0, c); out.flush(); } } // File.toURL() is deprecated return f.toURI().toURL(); }
private void append(File f, RandomEvent event) { try { FileOutputStream fos=new FileOutputStream(f, true); Writer osw=new OutputStreamWriter(fos); osw.write(event.when.toString()); osw.write(" : "); osw.write(Integer.toHexString(event.value)); osw.write('\n'); osw.flush(); fos.flush(); fos.getFD().sync(); fos.close(); Log.d(getClass().getSimpleName(), "logged to "+f.getAbsolutePath()); } catch (IOException e) { Log.e(getClass().getSimpleName(), "Exception writing to file", e); } } }
/** * Writes to log file which should reside on ram disk. * * @param x Data to log. */ public static synchronized void write(Object ... x) { if (!allowLog) return; Thread th = Thread.currentThread(); try { out.write((formatEntry(System.currentTimeMillis(), th.getName(), th.getId(), x) + "\n").getBytes(charset)); out.flush(); } catch (IOException e) { throw new IllegalStateException(e); } }
/** * Save bitmap to a .jpeg file * * @param bitmap * @param path */ public static void saveJPGE_After(Bitmap bitmap, String path) throws FileNotFoundException, IOException { File file = new File(path); FileOutputStream out = new FileOutputStream(file); if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) { out.flush(); out.close(); } }
/** * Checks if writing {@code len} bytes would go over threshold, and switches to file buffering if * so. */ private void update(int len) throws IOException { if (file == null && (memory.getCount() + len > fileThreshold)) { File temp = File.createTempFile("FileBackedOutputStream", null); if (resetOnFinalize) { // Finalizers are not guaranteed to be called on system shutdown; // this is insurance. temp.deleteOnExit(); } FileOutputStream transfer = new FileOutputStream(temp); transfer.write(memory.getBuffer(), 0, memory.getCount()); transfer.flush(); // We've successfully transferred the data; switch to writing to file out = transfer; file = temp; memory = null; } } }
private void writeKeystore(X509Certificate certificate, KeyPair entityKeyPair, String path) throws Exception { KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, PASSWORD); keyStore.setKeyEntry("alias", entityKeyPair.getPrivate(), PASSWORD, new Certificate[] { certificate }); FileOutputStream outputStream = new FileOutputStream(path); keyStore.store(outputStream, PASSWORD); outputStream.flush(); outputStream.close(); }
public void prepareForReading() throws IOException { if (!isOpen) { return; } if (output.getLength() > 0) { fileOutputStream.write(output.getData(), 0, output.getLength()); totalWriteLength += output.getLength(); fileOutputStream.flush(); output.reset(); } if (fileInputStream != null) { fileInputStream.close(); } fileInputStream = new FileInputStream(tmpFile); readNextIndex = 0; readNextCount = 0; }
private static String saveBitmap(Bitmap bm, String imageUrlName) { File f = new File(SAVEADDRESS, imageUrlName); try { FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return SCHEMA + f.getPath(); }
/** * Checks if writing {@code len} bytes would go over threshold, and switches to file buffering if * so. */ private void update(int len) throws IOException { if (file == null && (memory.getCount() + len > fileThreshold)) { File temp = File.createTempFile("FileBackedOutputStream", null); if (resetOnFinalize) { // Finalizers are not guaranteed to be called on system shutdown; // this is insurance. temp.deleteOnExit(); } FileOutputStream transfer = new FileOutputStream(temp); transfer.write(memory.getBuffer(), 0, memory.getCount()); transfer.flush(); // We've successfully transferred the data; switch to writing to file out = transfer; file = temp; memory = null; } } }