List of usage examples for java.io OutputStream flush
public void flush() throws IOException
From source file:Main.java
public static boolean write2SDFromString(String path, String fileName, String input) { boolean flag = true; File file = null;//from www .j a v a 2 s.c o m OutputStream output = null; try { file = new File(path + fileName); file.createNewFile(); // creatSDDir(path); //file = creatSDFile(path + fileName); output = new FileOutputStream(file); byte buffer[] = input.getBytes(); output.write(buffer, 0, buffer.length); output.flush(); } catch (Exception e) { return false; } finally { try { output.close(); } catch (Exception e) { return flag; } } return flag; }
From source file:Main.java
/** * //w ww . java2 s . c o m * @param context * @param backupFolder * @param db_name * @return */ public static boolean backupDB(Context context, String backupFolder, String db_name) { boolean result = false; try { String current_date = DateToString(GetToday(), "dd-MM-yyyy"); File data = Environment.getDataDirectory(); File sdcard = new File(Environment.getExternalStorageDirectory(), backupFolder + "/"); sdcard.mkdirs(); if (sdcard.canWrite()) { String currentDBPath = "//data//" + context.getPackageName() + "//databases//" + db_name + ""; String backupDBPath = "backup_" + db_name + "_" + current_date + ".db"; File currentDB = new File(data, currentDBPath); File backupDB = new File(sdcard, backupDBPath); if (currentDB.exists()) { InputStream input = new FileInputStream(currentDB); OutputStream output = new FileOutputStream(backupDB); byte[] buffer = new byte[1024]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } output.flush(); output.close(); input.close(); result = true; } } } catch (Exception e) { Log.e(TAG, e.getMessage()); } return result; }
From source file:com.yenlo.synapse.transport.vfs.VFSUtils.java
/** * Acquires a file item lock before processing the item, guaranteing that the file is not * processed while it is being uploaded and/or the item is not processed by two listeners * * @param fsManager used to resolve the processing file * @param fo representing the processing file item * @return boolean true if the lock has been acquired or false if not *//*from w ww . j av a 2 s . c o m*/ public synchronized static boolean acquireLock(FileSystemManager fsManager, FileObject fo) { // generate a random lock value to ensure that there are no two parties // processing the same file Random random = new Random(); byte[] lockValue = String.valueOf(random.nextLong()).getBytes(); try { // check whether there is an existing lock for this item, if so it is assumed // to be processed by an another listener (downloading) or a sender (uploading) // lock file is derived by attaching the ".lock" second extension to the file name String fullPath = fo.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos != -1) { fullPath = fullPath.substring(0, pos); } FileObject lockObject = fsManager.resolveFile(fullPath + ".lock"); if (lockObject.exists()) { log.debug("There seems to be an external lock, aborting the processing of the file " + fo.getName() + ". This could possibly be due to some other party already " + "processing this file or the file is still being uploaded"); } else { // write a lock file before starting of the processing, to ensure that the // item is not processed by any other parties lockObject.createFile(); OutputStream stream = lockObject.getContent().getOutputStream(); try { stream.write(lockValue); stream.flush(); stream.close(); } catch (IOException e) { lockObject.delete(); log.error("Couldn't create the lock file before processing the file " + fullPath, e); return false; } finally { lockObject.close(); } // check whether the lock is in place and is it me who holds the lock. This is // required because it is possible to write the lock file simultaneously by // two processing parties. It checks whether the lock file content is the same // as the written random lock value. // NOTE: this may not be optimal but is sub optimal FileObject verifyingLockObject = fsManager.resolveFile(fullPath + ".lock"); if (verifyingLockObject.exists() && verifyLock(lockValue, verifyingLockObject)) { return true; } } } catch (FileSystemException fse) { log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI()) + " before processing"); } return false; }
From source file:Main.java
private static void copy(InputStream in, OutputStream out) throws IOException { out = new BufferedOutputStream(out, 0x1000); in = new BufferedInputStream(in, 0x1000); // Copy the contents from the input stream to the output stream. while (true) { int b = in.read(); if (b == -1) { break; }/*from ww w .ja va2s . com*/ out.write(b); } out.flush(); }
From source file:net.dian1.player.download.DownloadTask.java
private static void downloadCover(DownloadJob job) { PlaylistEntry mPlaylistEntry = job.getPlaylistEntry(); String mDestination = job.getDestination(); String path = DownloadHelper.getAbsolutePath(mPlaylistEntry, mDestination); File file = new File(path + "/" + "cover.jpg"); // check if cover already exists if (file.exists()) { Log.v(Dian1Application.TAG, "File exists - nothing to do"); return;/*from ww w.j av a 2 s . c o m*/ } String albumUrl = mPlaylistEntry.getAlbum().getImage(); if (albumUrl == null) { Log.w(Dian1Application.TAG, "album Url = null. This should not happened"); return; } albumUrl = albumUrl.replace("1.100", "1.500"); InputStream stream = null; URL imageUrl; Bitmap bmp = null; // download cover try { imageUrl = new URL(albumUrl); try { stream = imageUrl.openStream(); bmp = BitmapFactory.decodeStream(stream); } catch (IOException e) { // TODO Auto-generated catch block Log.v(Dian1Application.TAG, "download Cover IOException"); e.printStackTrace(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block Log.v(Dian1Application.TAG, "download CoverMalformedURLException"); e.printStackTrace(); } // save cover to album directory if (bmp != null) { try { file.createNewFile(); OutputStream outStream = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream); outStream.flush(); outStream.close(); Log.v(Dian1Application.TAG, "Album cover saved to sd"); } catch (FileNotFoundException e) { Log.w(Dian1Application.TAG, "FileNotFoundException"); } catch (IOException e) { Log.w(Dian1Application.TAG, "IOException"); } } }
From source file:com.futureplatforms.kirin.internal.attic.IOUtils.java
public static InputStream postConnection(Context context, URL url, String method, String urlParameters) throws IOException { if (!url.getProtocol().startsWith("http")) { return url.openStream(); }/* w w w.java2s . c om*/ URLConnection c = url.openConnection(); HttpURLConnection connection = (HttpURLConnection) c; connection.setRequestMethod(method.toUpperCase()); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); connection.setRequestProperty("Content-Language", "en-US"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); OutputStream output = null; try { output = connection.getOutputStream(); output.write(urlParameters.getBytes("UTF-8")); output.flush(); } finally { IOUtils.close(output); } return connection.getInputStream(); }
From source file:br.org.ipti.guigoh.util.DownloadService.java
public static synchronized void downloadFile(File file, String mimeType) { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext context = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) context.getResponse(); response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\""); response.setContentLength((int) file.length()); response.setContentType(mimeType);//from ww w.j a va 2 s. c o m try { OutputStream out; try (FileInputStream in = new FileInputStream(file)) { out = response.getOutputStream(); byte[] buf = new byte[(int) file.length()]; int count; while ((count = in.read(buf)) >= 0) { out.write(buf, 0, count); } } out.flush(); out.close(); facesContext.responseComplete(); } catch (IOException ex) { System.out.println("Error in downloadFile: " + ex.getMessage()); } }
From source file:com.liveramp.hank.test.ZkTestCase.java
private static boolean waitForServerUp(int port, long timeout) { long start = System.currentTimeMillis(); while (true) { try {/* w w w.jav a2 s .c om*/ Socket sock = new Socket("localhost", port); BufferedReader reader = null; try { OutputStream outstream = sock.getOutputStream(); outstream.write("stat".getBytes()); outstream.flush(); Reader isr = new InputStreamReader(sock.getInputStream()); reader = new BufferedReader(isr); String line = reader.readLine(); if (line != null && line.startsWith("Zookeeper version:")) { return true; } } finally { sock.close(); if (reader != null) { reader.close(); } } } catch (IOException e) { // ignore as this is expected LOG.info("server localhost:" + port + " not up " + e); } if (System.currentTimeMillis() > start + timeout) { break; } try { Thread.sleep(250); } catch (InterruptedException e) { // ignore } } return false; }
From source file:Utils.java
public static void download(String urlStr, File destFile) throws IOException { URL srcUrl = new URL(urlStr); InputStream input = null;/* www. j a v a 2 s.c o m*/ OutputStream output = null; try { input = srcUrl.openStream(); FileOutputStream fos = new FileOutputStream(destFile); output = new BufferedOutputStream(fos); copyStreams(input, output); } finally { if (input != null) { input.close(); } if (output != null) { output.flush(); output.close(); } } }
From source file:com.liferay.mobile.android.http.file.FileTransferUtil.java
public static void transfer(AbstractExecutionAwareRequest request, InputStream is, OutputStream os, FileProgressCallback callback) throws IOException { int count;// w w w .j a va 2s .c o m byte data[] = new byte[4096]; while (((count = is.read(data)) != -1) && !isCancelled(callback)) { os.write(data, 0, count); if (callback != null) { callback.increment(count); } } os.flush(); if (isCancelled(callback)) { request.abort(); } }