List of usage examples for java.io BufferedOutputStream BufferedOutputStream
public BufferedOutputStream(OutputStream out)
From source file:Main.java
public static void downloadImage(String imageUrl) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Log.d("TAG", "monted sdcard"); } else {//from ww w. j ava2s .com Log.d("TAG", "has no sdcard"); } HttpURLConnection con = null; FileOutputStream fos = null; BufferedOutputStream bos = null; BufferedInputStream bis = null; File imageFile = null; try { URL url = new URL(imageUrl); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5 * 1000); con.setReadTimeout(15 * 1000); con.setDoInput(true); con.setDoOutput(true); bis = new BufferedInputStream(con.getInputStream()); imageFile = new File(getImagePath(imageUrl)); fos = new FileOutputStream(imageFile); bos = new BufferedOutputStream(fos); byte[] b = new byte[1024]; int length; while ((length = bis.read(b)) != -1) { bos.write(b, 0, length); bos.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } if (con != null) { con.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } if (imageFile != null) { } }
From source file:Main.java
public static boolean saveBitmap(Bitmap bitmap, String file, CompressFormat format) { OutputStream os = null;/*from ww w . j a va 2 s.co m*/ try { File f = new File(file); if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } os = new BufferedOutputStream(new FileOutputStream(file)); if (bitmap.compress(format, 100, os)) { os.flush(); return true; } } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { // do nothing } } } return false; }
From source file:Util.java
/** * Writes a serialized version of obj to a given file, compressing it using gzip. * @param f File to write to//from w ww. j av a 2s . c o m * @param obj Object to serialize */ public static void writeGzippedObject(File f, Serializable obj) { try { ObjectOutputStream oos = new ObjectOutputStream( new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(f)))); oos.writeObject(obj); oos.close(); } catch (IOException e) { System.err.println("Exception writing file " + f + ": " + e); } }
From source file:Main.java
/** * Copys a document file from one directory to another * @param documentPath/*w w w . j av a 2s .c o m*/ * @param destPath * @param filename * @param entityId src file Entity ID * @param newEntityId dest file Entity ID * @throws IOException */ public static void copyDocumentFile(File documentDir, File destDir, String filename, Object entityId, Object newEntityId) throws IOException { File src = new File(documentDir, filename); File dest = new File(destDir, filename); if (dest.exists()) dest.delete(); byte[] buffer = new byte[4096]; int read = 0; InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src)); out = new BufferedOutputStream(new FileOutputStream(dest)); while (true) { read = in.read(buffer); if (read == -1) { // -1 bedeutet EOF break; } out.write(buffer, 0, read); } } finally { if (in != null) { try { in.close(); } finally { if (out != null) { out.close(); } } } } }
From source file:Main.java
public static void writeByteArray(Context context, byte[] data, String fileName) { FileOutputStream outputStream; try {// w w w .jav a 2s . com outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE); BufferedOutputStream bos = new BufferedOutputStream(outputStream); for (byte s : data) { bos.write(s); } bos.close(); outputStream.close(); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } }
From source file:MainClass.java
public static void desEncrypt(String f1, String f2) throws Exception { SecretKey key = null;//w ww . j a v a 2 s. co m ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser")); key = (SecretKey) keyFile.readObject(); keyFile.close(); KeyGenerator keygen = KeyGenerator.getInstance("DES"); key = keygen.generateKey(); ObjectOutputStream keyFileout = new ObjectOutputStream(new FileOutputStream("DESKey.ser")); keyFileout.writeObject(key); keyFileout.close(); Cipher cipher = null; cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); BufferedInputStream in = new BufferedInputStream(new FileInputStream(f1)); CipherOutputStream out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(f2)), cipher); int i; do { i = in.read(); if (i != -1) out.write(i); } while (i != -1); in.close(); out.close(); }
From source file:Main.java
public static void extractEntryContent(ZipInputStream zis, ZipEntry entry, String unzipdir) throws IOException, FileNotFoundException { String entryFileName = entry.getName(); String entryPath = unzipdir + File.separator + entryFileName; createFile(entryPath);//from w ww. j av a2s.co m BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entryPath)); byte[] buffer = new byte[1024]; int count = -1; while ((count = zis.read(buffer)) != -1) { bos.write(buffer, 0, count); } bos.close(); }
From source file:Main.java
public static boolean copyFile(File fromFile, File toFile) { BufferedInputStream bis = null; BufferedOutputStream fos = null; try {/*from ww w .j av a 2 s . co m*/ FileInputStream is = new FileInputStream(fromFile); bis = new BufferedInputStream(is); fos = new BufferedOutputStream(new FileOutputStream(toFile)); byte[] buf = new byte[2048]; int i; while ((i = bis.read(buf)) != -1) { fos.write(buf, 0, i); } fos.flush(); return true; } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (fos != null) { fos.close(); } } catch (IOException e1) { e1.printStackTrace(); } } return false; }
From source file:Main.java
public static boolean saveFile(Bitmap bm, String path, Bitmap.CompressFormat format) { if (bm == null || path == null) return false; File myCaptureFile = new File(path); if (myCaptureFile.exists()) { myCaptureFile.delete();/*w w w. j ava2 s . c om*/ } try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); bm.compress(format, 80, bos); bos.flush(); bos.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:javarestart.Utils.java
static File fetchResourceToTempFile(String resName, String resExt, InputStream from) { File temp;/*from ww w . j a v a 2 s.com*/ try { temp = File.createTempFile(resName, resExt); } catch (IOException e) { return null; } temp.deleteOnExit(); try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(temp))) { copy(from, os); } catch (IOException e) { e.printStackTrace(); return null; } return temp; }