List of usage examples for java.io FileOutputStream flush
public void flush() throws IOException
From source file:com.glaf.core.security.DigestUtil.java
public static void digestFile(String filename, String algorithm) { byte[] b = new byte[65536]; int read = 0; FileInputStream fis = null;//ww w. j ava 2 s. c o m FileOutputStream fos = null; OutputStream encodedStream = null; try { MessageDigest md = MessageDigest.getInstance(algorithm); fis = new FileInputStream(filename); while (fis.available() > 0) { read = fis.read(b); md.update(b, 0, read); } byte[] digest = md.digest(); StringBuffer fileNameBuffer = new StringBuffer(256).append(filename).append('.').append(algorithm); fos = new FileOutputStream(fileNameBuffer.toString()); encodedStream = MimeUtility.encode(fos, "base64"); encodedStream.write(digest); fos.flush(); } catch (Exception ex) { throw new RuntimeException("Error computing Digest: " + ex); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(encodedStream); } }
From source file:com.baidu.rigel.biplatform.ma.file.serv.util.LocalFileOperationUtils.java
/** * ???//from ww w . ja v a 2 s . c om * * @param oldFile * * @param newFile * * @return * @throws IOException */ private static boolean copy(File oldFile, File newFile) { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(oldFile); fileOutputStream = new FileOutputStream(newFile); byte[] buf = new byte[1024]; int len = 0; // ?? while ((len = fileInputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, len); fileOutputStream.flush(); } return true; } catch (IOException e) { LOG.error(e.getMessage(), e); return false; } finally { try { if (fileInputStream != null) { fileInputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { LOG.error(e.getMessage(), e); } } }
From source file:com.zacwolf.commons.crypto.Crypter_AES.java
/** * @param keyfile/*w w w . j ava2 s .co m*/ * @param keysize * @param salter * @throws InvalidKeySpecException * @throws IOException * @throws NoSuchAlgorithmException */ public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter) throws InvalidKeySpecException, IOException, NoSuchAlgorithmException { if (keysize > Cipher.getMaxAllowedKeyLength(mytype)) throw new InvalidKeySpecException( "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of " + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype); final FileOutputStream fos = new FileOutputStream(keyfile); try { final KeyGenerator kgen = KeyGenerator.getInstance(mytype); kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available final SecretKey sk = kgen.generateKey(); fos.write(sk.getEncoded()); } finally { fos.flush(); fos.close(); ; } }
From source file:com.alibaba.otter.shared.common.utils.NioUtils.java
/** * byte//from w w w .ja v a2 s . c om */ public static void write(final byte[] srcArray, File targetFile, final boolean overwrite) throws IOException { if (srcArray == null) { throw new IOException("Source must not be null"); } if (targetFile == null) { throw new IOException("Target must not be null"); } if (true == targetFile.exists()) { if (true == targetFile.isDirectory()) { throw new IOException("Target '" + targetFile.getAbsolutePath() + "' is directory!"); } else if (true == targetFile.isFile()) { if (!overwrite) { throw new IOException("Target file '" + targetFile.getAbsolutePath() + "' already exists!"); } } else { throw new IOException("Invalid target object '" + targetFile.getAbsolutePath() + "'!"); } } else { // create parent dir create(targetFile.getParentFile(), false, 3); } // ?inputStream ByteArrayInputStream input = null; FileOutputStream output = null; try { input = new ByteArrayInputStream(srcArray); output = new FileOutputStream(targetFile); copy(input, output); output.flush(); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } }
From source file:com.baidu.rigel.biplatform.ma.file.serv.util.LocalFileOperationUtils.java
/** * content/*from w ww. j ava2 s . co m*/ * * @param file * ? * @param content * * @param code * ?? */ public static boolean writeFile(File file, byte[] content) { FileOutputStream fileOutputStream = null; try { if (file == null) { return false; } if (content == null) { return false; } if (content.length == 0) { return false; } fileOutputStream = new FileOutputStream(file); // fileOutputStream.write(content); fileOutputStream.flush(); return true; } catch (IOException e) { LOG.error(e.getMessage(), e); return false; } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { LOG.error(e.getMessage(), e); } } } }
From source file:com.alibaba.otter.shared.common.utils.NioUtils.java
/** * Copy source file to destination. If destination is a path then source file name is appended. If destination file * exists then: overwrite=true - destination file is replaced; overwite=false - exception is thrown *//*from w w w . ja v a 2 s . com*/ public static void copy(final File sourceFile, File targetFile, final boolean overwrite) throws IOException { if (sourceFile == null) { throw new IOException("Source must not be null"); } if (targetFile == null) { throw new IOException("Target must not be null"); } // checks if ((false == sourceFile.isFile()) || (false == sourceFile.exists())) { throw new IOException("Source file '" + sourceFile.getAbsolutePath() + "' not found!"); } if (true == targetFile.exists()) { if (true == targetFile.isDirectory()) { // Directory? -> use source file name targetFile = new File(targetFile, sourceFile.getName()); } else if (true == targetFile.isFile()) { if (false == overwrite) { throw new IOException("Target file '" + targetFile.getAbsolutePath() + "' already exists!"); } } else { throw new IOException("Invalid target object '" + targetFile.getAbsolutePath() + "'!"); } } else { // create parent dir FileUtils.forceMkdir(targetFile.getParentFile()); } FileInputStream input = null; FileOutputStream output = null; try { input = new FileInputStream(sourceFile); output = new FileOutputStream(targetFile); copy(input, output); output.flush(); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } }
From source file:Main.java
public static boolean saveToSDCard(Bitmap bitmap) { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return false; }/* w w w .j a v a 2s.c om*/ FileOutputStream fileOutputStream = null; File file = new File("/sdcard/myName/Download/"); if (!file.exists()) { file.mkdirs(); } String fileName = UUID.randomUUID().toString() + ".jpg"; String filePath = "/sdcard/myName/Download/" + fileName; File f = new File(filePath); if (!f.exists()) { try { f.createNewFile(); fileOutputStream = new FileOutputStream(filePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); } catch (IOException e) { return false; } finally { try { fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException e) { return false; } } } return true; }
From source file:com.uf.togathor.db.couchdb.ConnectionHandler.java
/** * Get a File object from an URL// ww w . ja va 2 s. co m * * @param url * @param path * @return * @throws IOException * @throws ClientProtocolException * @throws TogathorException * @throws JSONException * @throws IllegalStateException * @throws TogathorForbiddenException */ public static void getFile(String url, File file, String userId, String token) throws IOException, TogathorException, IllegalStateException, JSONException, TogathorForbiddenException { File mFile = file; //URL mUrl = new URL(url); // you can write here any link InputStream is = httpGetRequest(url, userId); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(20000); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert the Bytes read to a String. */ FileOutputStream fos = new FileOutputStream(mFile); fos.write(baf.toByteArray()); fos.flush(); fos.close(); is.close(); }
From source file:net.grinder.util.LogCompressUtil.java
/** * Uncompress the given array into the given file. * //from ww w.ja v a 2 s . c om * @param zipEntry * byte array of compressed file * @param toFile * file to be written */ public static void unCompressGzip(byte[] zipEntry, File toFile) { FileOutputStream fos = null; GZIPInputStream zipInputStream = null; ByteArrayInputStream bio = null; try { bio = new ByteArrayInputStream(zipEntry); zipInputStream = new GZIPInputStream(bio); fos = new FileOutputStream(toFile); byte[] buffer = new byte[COMPRESS_BUFFER_SIZE]; int count = 0; while ((count = zipInputStream.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) { fos.write(buffer, 0, count); } fos.flush(); } catch (IOException e) { LOGGER.error("Error occurs while uncompress {}", toFile.getAbsolutePath()); LOGGER.error("Details", e); return; } finally { IOUtils.closeQuietly(fos); IOUtils.closeQuietly(bio); IOUtils.closeQuietly(zipInputStream); } }
From source file:com.yibu.kuaibu.app.glApplication.java
public static void saveMyBitmap(Bitmap mBitmap, String bitName) { File f = new File(bitName); Log.i("application", "" + bitName); FileOutputStream fOut = null; try {// w ww .j a v a 2 s. c o m fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } }