List of usage examples for java.io FileOutputStream flush
public void flush() throws IOException
From source file:jBittorrentAPI.utils.IOManager.java
/** * Save the byte array into a file with the give filename * @param data byte[]//from w w w.ja v a 2s.c om * @param filename String * @return boolean */ public static boolean save(byte[] data, String filename) { try { FileOutputStream fos = new FileOutputStream(filename); fos.write(data, 0, data.length); fos.flush(); fos.close(); return true; } catch (IOException ioe) { } return false; }
From source file:com.fun.util.TesseractUtil.java
/** * url?// w w w .j a va2s . c o m * * @param url * @return * @throws IOException */ private static File getFileFromUrl(URL url) throws IOException { File tmpImage = File.createTempFile("tesseract-ocr-download", null); InputStream in = url.openConnection().getInputStream(); FileOutputStream fos = new FileOutputStream(tmpImage); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); fos.close(); return tmpImage; }
From source file:helpers.FileUpload.java
public static boolean processFile(String path, FileItemStream item) { try {//w ww .j a va2s . c o m File f = new File(path + File.separator + "assets" + File.separator + "uploads"); if (!f.exists()) { f.mkdir(); } File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName()); FileOutputStream fos = new FileOutputStream(savedFile); InputStream is = item.openStream(); int x = 0; byte[] b = new byte[1024]; while ((x = is.read(b)) != -1) { fos.write(b, 0, x); } fos.flush(); fos.close(); return true; } catch (Exception e) { System.out.println("FileUpload 35: " + e.getMessage()); } return false; }
From source file:Main.java
public static boolean OutPutImage(File file, Bitmap bitmap) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs();/*from w ww. j a v a 2 s . co m*/ } BufferedOutputStream bos = null; FileOutputStream fos = null; try { fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); if (bos != null) { bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); bos.flush(); fos.flush(); return true; } else { return false; } } catch (IOException e) { return false; } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:Main.java
@SuppressWarnings("ResultOfMethodCallIgnored") public static boolean saveBitmap(Bitmap bitmap, String filePath, int quality) { boolean result = false; FileOutputStream fileOutputStream = null; try {/*from w w w. j a va 2s . co m*/ File file = new File(filePath); if (file.exists()) file.delete(); fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream); fileOutputStream.flush(); result = true; } catch (IOException ignored) { } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException ignored) { } } } return result; }
From source file:Main.java
public static boolean copyFileTo(File srcFile, File destFile) throws IOException { if (srcFile == null || destFile == null) { return false; }/*from w w w . j a v a2 s . c o m*/ if (srcFile.isDirectory() || destFile.isDirectory()) return false; if (!srcFile.exists()) { return false; } if (!destFile.exists()) { createFile(destFile.getAbsolutePath()); } FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); int readLen = 0; byte[] buf = new byte[1024]; while ((readLen = fis.read(buf)) != -1) { fos.write(buf, 0, readLen); } fos.flush(); fos.close(); fis.close(); return true; }
From source file:com.streamsets.datacollector.util.ClusterUtil.java
public static void setupCluster(String testName, String pipelineJson, YarnConfiguration yarnConfiguration) throws Exception { System.setProperty("sdc.testing-mode", "true"); System.setProperty(MiniSDCTestingUtility.PRESERVE_TEST_DIR, "true"); yarnConfiguration.set("yarn.nodemanager.delete.debug-delay-sec", "600"); miniSDCTestingUtility = new MiniSDCTestingUtility(); File dataTestDir = miniSDCTestingUtility.getDataTestDir(); //copy spark files under the test data directory into a dir called "spark" File sparkHome = ClusterUtil.createSparkHome(dataTestDir); //start mini yarn cluster miniYarnCluster = miniSDCTestingUtility.startMiniYarnCluster(testName, 1, 1, 1, yarnConfiguration); Configuration config = miniYarnCluster.getConfig(); long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10); while (config.get(YarnConfiguration.RM_ADDRESS).split(":")[1] == "0") { if (System.currentTimeMillis() > deadline) { throw new IllegalStateException("Timed out waiting for RM to come up."); }//from w ww.j av a2 s . co m LOG.debug("RM address still not set in configuration, waiting..."); TimeUnit.MILLISECONDS.sleep(100); } LOG.debug("RM at " + config.get(YarnConfiguration.RM_ADDRESS)); Properties sparkHadoopProps = new Properties(); for (Map.Entry<String, String> entry : config) { sparkHadoopProps.setProperty("spark.hadoop." + entry.getKey(), entry.getValue()); } LOG.debug("Creating spark properties file at " + dataTestDir); File propertiesFile = new File(dataTestDir, "spark.properties"); propertiesFile.createNewFile(); FileOutputStream sdcOutStream = new FileOutputStream(propertiesFile); sparkHadoopProps.store(sdcOutStream, null); sdcOutStream.flush(); sdcOutStream.close(); // Need to pass this property file to spark-submit for it pick up yarn confs System.setProperty(SPARK_PROPERTY_FILE, propertiesFile.getAbsolutePath()); File sparkBin = new File(sparkHome, "bin"); for (File file : sparkBin.listFiles()) { MiniSDCTestingUtility.setExecutePermission(file.toPath()); } miniSDC = miniSDCTestingUtility.createMiniSDC(MiniSDC.ExecutionMode.CLUSTER); miniSDC.startSDC(); serverURI = miniSDC.getServerURI(); miniSDC.createPipeline(pipelineJson); miniSDC.startPipeline(); int attempt = 0; //Hard wait for 2 minutes while (miniSDC.getListOfSlaveSDCURI().size() == 0 && attempt < 24) { Thread.sleep(5000); attempt++; LOG.debug("Attempt no: " + attempt + " to retrieve list of slaves"); } if (miniSDC.getListOfSlaveSDCURI().size() == 0) { throw new IllegalStateException("Timed out waiting for slaves to come up."); } }
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 . co m 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:msec.org.GzipUtil.java
static public void unzip(String srcFile) throws Exception { GzipCompressorInputStream in = new GzipCompressorInputStream(new FileInputStream(srcFile)); int index = srcFile.indexOf(".gz"); String destFile = ""; if (index == srcFile.length() - 3) { destFile = srcFile.substring(0, index); } else {//from w ww. j a v a 2 s . c o m destFile = srcFile + ".decompress"; } FileOutputStream out = new FileOutputStream(destFile); byte[] buf = new byte[10240]; while (true) { int len = in.read(buf); if (len <= 0) { break; } out.write(buf, 0, len); } out.flush(); out.close(); in.close(); }
From source file:Main.java
public static boolean copyFileTo(InputStream inputStream, File destFile) throws IOException { if (inputStream == null || destFile == null) { return false; }//from w w w.j a v a 2s.c o m if (destFile.isDirectory()) return false; if (!destFile.exists()) { createFile(destFile.getAbsolutePath()); } FileOutputStream fos = new FileOutputStream(destFile); int readLen = 0; byte[] buf = new byte[1024]; while ((readLen = inputStream.read(buf)) != -1) { fos.write(buf, 0, readLen); } fos.flush(); fos.close(); inputStream.close(); return true; }