List of usage examples for java.io FileOutputStream flush
public void flush() throws IOException
From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java
/** * download file url and save it/*w ww . ja v a 2 s. c om*/ * * @param url */ public static String downloadFile(String url) { int BYTE_ARRAY_SIZE = 1024; int CONNECTION_TIMEOUT = 30000; int READ_TIMEOUT = 30000; // downloading cover image and saving it into file try { URL imageUrl = new URL(URLDecoder.decode(url)); URLConnection conn = imageUrl.openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); File resFile = new File(cachePath + File.separator + Utils.md5(url)); if (!resFile.exists()) { resFile.createNewFile(); } FileOutputStream fos = new FileOutputStream(resFile); int current = 0; byte[] buf = new byte[BYTE_ARRAY_SIZE]; Arrays.fill(buf, (byte) 0); while ((current = bis.read(buf, 0, BYTE_ARRAY_SIZE)) != -1) { fos.write(buf, 0, current); Arrays.fill(buf, (byte) 0); } bis.close(); fos.flush(); fos.close(); Log.d("", ""); return resFile.getAbsolutePath(); } catch (SocketTimeoutException e) { return null; } catch (IllegalArgumentException e) { return null; } catch (Exception e) { return null; } }
From source file:com.lee.sdk.utils.Utils.java
/** * /*from w w w . j ava 2 s.c o m*/ * * @param context Context Object * @param file ?? * @param data ??? * @param mode ? * @return ??? */ public static boolean cache(Context context, String file, byte[] data, int mode) { boolean bResult = false; if (null == data) { data = new byte[0]; } FileOutputStream fos = null; try { fos = context.openFileOutput(file, mode); fos.write(data); fos.flush(); bResult = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != fos) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return bResult; }
From source file:it.greenvulcano.util.txt.TextUtils.java
/** * Writes a text String into a file//ww w .ja va 2 s. com * * @param contentStrings * The StringBuffer to be written into the file * @param file * The destination file * @param endline * The line terminator, if null or empty is used '\n' * @param append * If true the data are appended to existent file * @throws IOException */ public static void writeFile(List<String> contentStrings, File file, String endline, boolean append) throws IOException { FileOutputStream out = null; try { if ((endline == null) || endline.equals("")) { endline = "\n"; } out = new FileOutputStream(file, append); IOUtils.writeLines(contentStrings, endline, out); out.flush(); } finally { if (out != null) { out.close(); } } }
From source file:com.funambol.foundation.util.MediaUtils.java
/** * Creates the thumbnail./*from w w w .j av a2 s .com*/ * * @param imageFile the image file * @param thumbFile the empty thumbnail file * @param thumbX the width of the thumbnail * @param thumbY the height of the thumbnail * @param imageName the image file name with extension * @param tolerance the percentage of tolerance before creating a thumbnail * @return true is the thumbnail has been created, false otherwise * @throws IOException if an error occurs */ private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName, double tolerance) throws IOException { FileInputStream fileis = null; ImageInputStream imageis = null; Iterator readers = null; try { readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1)); if (readers == null || (!readers.hasNext())) { throw new IOException("File not supported"); } ImageReader reader = (ImageReader) readers.next(); fileis = new FileInputStream(imageFile); imageis = ImageIO.createImageInputStream(fileis); reader.setInput(imageis, true); // Determines thumbnail height, width and quality int thumbWidth = thumbX; int thumbHeight = thumbY; double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = reader.getWidth(0); int imageHeight = reader.getHeight(0); // // Don't create the thumbnail if the original file is smaller // than required size increased by % tolerance // if (imageWidth <= (thumbWidth * (1 + tolerance / 100)) && imageHeight <= (thumbHeight * (1 + tolerance / 100))) { return false; } double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(3, 3, 0, 0); BufferedImage bi = reader.read(0, param); Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH); BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null); FileOutputStream fileOutputStream = new FileOutputStream(thumbFile); ImageIO.write(thumbImage, "jpg", fileOutputStream); thumb.flush(); thumbImage.flush(); fileOutputStream.flush(); fileOutputStream.close(); graphics2D.dispose(); } finally { if (fileis != null) { fileis.close(); } if (imageis != null) { imageis.close(); } } return true; }
From source file:com.pdftron.pdf.utils.Utils.java
public static String copyResourceToTempFolder(Context context, int resId, boolean force, String resourceName) throws PDFNetException { if (context == null) { throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "copyResourceToTempFolder()", "Context cannot be null to initialize resource file."); } else {//from w ww . java 2s .c o m File resFile = new File(context.getFilesDir() + File.separator + "resourceName"); if (!resFile.exists() || force) { File filesDir = context.getFilesDir(); StatFs stat = new StatFs(filesDir.getPath()); long size = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if (size < 2903023L) { throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "copyResourceToTempFolder()", "Not enough space available to copy resources file."); } Resources rs = context.getResources(); try { InputStream e = rs.openRawResource(resId); FileOutputStream fos = context.openFileOutput(resourceName, 0); byte[] buffer = new byte[1024]; int read; while ((read = e.read(buffer)) != -1) { fos.write(buffer, 0, read); } e.close(); fos.flush(); fos.close(); } catch (Resources.NotFoundException var13) { throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "initializeResource()", "Resource file ID does not exist."); } catch (FileNotFoundException var14) { throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "initializeResource()", "Resource file not found."); } catch (IOException var15) { throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "initializeResource()", "Error writing resource file to internal storage."); } catch (Exception var16) { throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "initializeResource()", "Unknown error."); } } return context.getFilesDir().getAbsolutePath(); } }
From source file:Main.java
public static void decrypt(String fileIn, String fileOut, byte key[]) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { FileInputStream fis = new FileInputStream(fileIn); FileOutputStream fos = new FileOutputStream(fileOut); // Length is 32 bytes //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-256"); key = sha.digest(key);/* w ww . ja v a 2s . c o m*/ SecretKeySpec sks = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, sks); CipherInputStream cis = new CipherInputStream(fis, cipher); int b; byte[] d = new byte[8]; while ((b = cis.read(d)) != -1) { fos.write(d, 0, b); } fos.flush(); fos.close(); cis.close(); }
From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java
/** * ?/* w w w . java 2s . com*/ * * @param oldPath * String c:/fqf * @param newPath * String ?? f:/fqf/ff * @return boolean * @throws Exception */ public static void copyFolder(String oldPath, String newPath) throws Exception { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "copyFolder", "[oldPath:" + oldPath + "][newPath:+" + newPath + "]")); try { File newPathFile = new File(newPath); newPathFile.mkdirs(); File oldPathFile = new File(oldPath); String[] file = oldPathFile.list(); File temp = null; for (int i = 0; i < file.length; i++) { if (oldPath.endsWith(File.separator)) { temp = new File(oldPath + file[i]); } else { temp = new File(oldPath + File.separator + file[i]); } if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream( newPath + File.separator + (temp.getName()).toString()); byte[] b = new byte[1024 * 5]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if (temp.isDirectory()) { // ? copyFolder(oldPath + File.separator + file[i], newPath + File.separator + file[i]); } } } catch (Exception e) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_EXCEPTION, "copyFolder", "[oldPath:" + oldPath + "][newPath:+" + newPath + "]")); throw e; } LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "copyFolder", "[oldPath:" + oldPath + "][newPath:+" + newPath + "]")); }
From source file:it.greenvulcano.util.txt.TextUtils.java
/** * Writes a text String into a file/* w w w . j a v a 2 s .c o m*/ * * @param contentStrings * The list of strings to be written into the file * @param filename * The name of the file * @param endline * The line terminator, if null or empty is used '\n' * @param append * If true the data are appended to existent file * @throws IOException */ public static void writeFile(List<String> contentStrings, String filename, String endline, boolean append) throws IOException { FileOutputStream out = null; try { filename = adjustPath(filename); if ((endline == null) || endline.equals("")) { endline = "\n"; } out = new FileOutputStream(filename, append); IOUtils.writeLines(contentStrings, endline, out); out.flush(); } finally { if (out != null) { out.close(); } } }
From source file:com.bamobile.fdtks.util.Tools.java
public static boolean saveFileToAppContextCacheDir(String fileName, byte[] data) { try {//from w w w .j a va 2 s . c o m File dir = MainActivity.getInstance().getApplicationContext().getCacheDir(); File outputFile = new File(dir, fileName); if (!outputFile.exists()) { outputFile.createNewFile(); } FileOutputStream outputStream = new FileOutputStream(outputFile); outputStream.write(data); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } return true; }
From source file:com.mindquarry.desktop.pool.PoolBase.java
public void addEntry(String id, Document doc) throws IOException { File entryFile = new File(poolFolder.getAbsolutePath() + "/" + id); //$NON-NLS-1$ FileOutputStream fos = new FileOutputStream(entryFile); fos.write(doc.asXML().getBytes());/*from ww w .j a v a 2 s. c o m*/ fos.flush(); fos.close(); }