List of usage examples for java.io BufferedOutputStream flush
@Override public synchronized void flush() throws IOException
From source file:com.forecast.io.toolbox.NetworkServiceTask.java
@Override protected INetworkResponse doInBackground(INetworkRequest... params) { INetworkRequest request = params[0]; if (request == null || isCancelled()) { return null; }//from w w w . ja va 2s . c o m InputStream input = null; BufferedOutputStream output = null; HttpURLConnection connection = null; INetworkResponse response = null; try { response = (INetworkResponse) request.getResponse().newInstance(); URL url = new URL(request.getUri().toString()); connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(SOCKET_TIME_OUT); connection.setConnectTimeout(CONNECTION_TIME_OUT); connection.setRequestMethod(request.getMethod().toString()); connection.setDoInput(true); if (NetworkUtils.Method.POST.equals(request.getMethod())) { String data = request.getPostBody(); if (data != null) { connection.setDoOutput(true); connection.setRequestProperty("Content-Type", request.getContentType()); output = new BufferedOutputStream(connection.getOutputStream()); output.write(data.getBytes()); output.flush(); IOUtils.closeQuietly(output); } } int code = connection.getResponseCode(); input = (code != HttpStatus.SC_OK) ? connection.getErrorStream() : connection.getInputStream(); response.onNetworkResponse(new JSONObject(IOUtils.toString(input))); IOUtils.closeQuietly(input); } catch (Exception e) { Log.e(Constants.LOG_TAG, e.toString()); } finally { if (connection != null) { connection.disconnect(); } IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } return response; }
From source file:org.apache.ofbiz.base.util.UtilHttp.java
/** * Stream binary content from InputStream to OutputStream * This method does not close the streams passed * * @param out OutputStream content should go to * @param in InputStream of the actual content * @param length Size (in bytes) of the content * @throws IOException/*from w w w.j ava2s . c o m*/ */ public static void streamContent(OutputStream out, InputStream in, int length) throws IOException { int bufferSize = 512; // same as the default buffer size; change as needed // make sure we have something to write to if (out == null) { throw new IOException("Attempt to write to null output stream"); } // make sure we have something to read from if (in == null) { throw new IOException("Attempt to read from null input stream"); } // make sure we have some content if (length == 0) { throw new IOException("Attempt to write 0 bytes of content to output stream"); } // initialize the buffered streams BufferedOutputStream bos = new BufferedOutputStream(out, bufferSize); BufferedInputStream bis = new BufferedInputStream(in, bufferSize); byte[] buffer = new byte[length]; int read = 0; try { while ((read = bis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, read); } } catch (IOException e) { Debug.logError(e, "Problem reading/writing buffers", module); bis.close(); bos.close(); throw e; } finally { if (bis != null) { bis.close(); } if (bos != null) { bos.flush(); bos.close(); } } }
From source file:com.gfan.sbbs.utils.images.ImageManager.java
private String writeToFile(InputStream is, String filename) { Log.d("LDS", "new write to file"); BufferedInputStream in = null; BufferedOutputStream out = null; try {//from w ww .j a v a 2 s.co m in = new BufferedInputStream(is); out = new BufferedOutputStream(mContext.openFileOutput(filename, Context.MODE_PRIVATE)); byte[] buffer = new byte[1024]; int l; while ((l = in.read(buffer)) != -1) { out.write(buffer, 0, l); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (in != null) in.close(); if (out != null) { Log.d("LDS", "new write to file to -> " + filename); out.flush(); out.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } } return mContext.getFilesDir() + "/" + filename; }
From source file:com.agilejava.docbkx.maven.AbstractTransformerMojo.java
/** * Saves the Docbook XML file with all XInclude resolved. * * @param initialFilename Filename of the root docbook source file. * @param doc XOM Document resolved. * @return The new file generated.// w w w.j a v a 2 s. com * @throws MojoExecutionException DOCUMENT ME! */ protected File dumpResolvedXML(String initialFilename, nu.xom.Document doc) throws MojoExecutionException { final File file = new File(initialFilename); final String parent = file.getParent(); File resolvedXML = null; if (parent != null) { resolvedXML = new File(getGeneratedSourceDirectory(), parent); resolvedXML.mkdirs(); resolvedXML = new File(resolvedXML, "(gen)" + file.getName()); } else { getGeneratedSourceDirectory().mkdirs(); resolvedXML = new File(getGeneratedSourceDirectory(), "(gen)" + initialFilename); } FileOutputStream fos = null; try { fos = new FileOutputStream(resolvedXML); } catch (FileNotFoundException e) { throw new MojoExecutionException("Failed to open dump file", e); } if (fos != null) { getLog().info("Dumping to " + resolvedXML.getAbsolutePath()); final BufferedOutputStream bos = new BufferedOutputStream(fos); final Serializer serializer = new Serializer(bos); try { serializer.write(doc); bos.flush(); bos.close(); fos.close(); return resolvedXML; } catch (IOException e) { throw new MojoExecutionException("Failed to write to dump file", e); } finally { IOUtils.closeQuietly(bos); IOUtils.closeQuietly(fos); } } throw new MojoExecutionException("Failed to open dump file"); }
From source file:org.broadleafcommerce.core.search.service.solr.SolrConfiguration.java
public void copyConfigToSolrHome(InputStream configIs, File destFile) throws IOException { BufferedInputStream bis = null; BufferedOutputStream bos = null; try {//from w w w . j ava 2s . c o m bis = new BufferedInputStream(configIs); bos = new BufferedOutputStream(new FileOutputStream(destFile, false)); boolean eof = false; while (!eof) { int temp = bis.read(); if (temp == -1) { eof = true; } else { bos.write(temp); } } bos.flush(); } finally { if (bis != null) { try { bis.close(); } catch (Throwable e) { //do nothing } } if (bos != null) { try { bos.close(); } catch (Throwable e) { //do nothing } } } }
From source file:br.com.carlosrafaelgn.fplay.list.RadioStationList.java
private void saveFavoritesInternal(Context context) throws IOException { FileOutputStream fs = null;/* w ww . ja v a2 s. co m*/ BufferedOutputStream bs = null; try { final int count = Math.min(MAX_COUNT, favorites.size()); int i = 0; fs = context.openFileOutput("_RadioFav", 0); bs = new BufferedOutputStream(fs, 4096); Serializer.serializeInt(bs, 0x0100); Serializer.serializeInt(bs, count); for (RadioStation s : favorites) { if (i >= count) break; s.serialize(bs); i++; } bs.flush(); } finally { try { if (bs != null) bs.close(); } catch (Throwable ex) { } try { if (fs != null) fs.close(); } catch (Throwable ex) { } } }
From source file:com.jeson.xutils.http.callback.FileDownloadHandler.java
public File handleEntity(HttpEntity entity, RequestCallBackHandler callBackHandler, String target, boolean isResume, String responseFileName) throws IOException { if (entity == null || TextUtils.isEmpty(target)) { return null; }// ww w .ja v a 2 s . c om Log.e("target", " target:" + target); File targetFile = new File(target); Log.e("target", " targetFile:" + targetFile); if (!targetFile.exists()) { File dir = targetFile.getParentFile(); if (dir.exists() || dir.mkdirs()) { targetFile.createNewFile(); } } long current = 0; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { FileOutputStream fileOutputStream = null; if (isResume) { current = targetFile.length(); fileOutputStream = new FileOutputStream(target, true); } else { fileOutputStream = new FileOutputStream(target); } long total = entity.getContentLength() + current; bis = new BufferedInputStream(entity.getContent()); bos = new BufferedOutputStream(fileOutputStream); if (callBackHandler != null && !callBackHandler.updateProgress(total, current, true)) { return targetFile; } byte[] tmp = new byte[4096]; int len; while ((len = bis.read(tmp)) != -1) { bos.write(tmp, 0, len); current += len; if (callBackHandler != null) { if (!callBackHandler.updateProgress(total, current, false)) { return targetFile; } } } bos.flush(); if (callBackHandler != null) { callBackHandler.updateProgress(total, current, true); } } finally { IOUtils.closeQuietly(bis); IOUtils.closeQuietly(bos); } if (targetFile.exists() && !TextUtils.isEmpty(responseFileName)) { File newFile = new File(targetFile.getParent(), responseFileName); while (newFile.exists()) { newFile = new File(targetFile.getParent(), System.currentTimeMillis() + responseFileName); } return targetFile.renameTo(newFile) ? newFile : targetFile; } else { return targetFile; } }
From source file:cn.isif.util_plus.http.callback.FileDownloadHandler.java
public File handleEntity(HttpEntity entity, RequestCallBackHandler callBackHandler, String target, boolean isResume, String responseFileName) throws IOException { if (entity == null || TextUtils.isEmpty(target)) { return null; }//from w w w . j a v a 2 s . c om File targetFile = new File(target); if (!targetFile.exists()) { File dir = targetFile.getParentFile(); if (dir.exists() || dir.mkdirs()) { targetFile.createNewFile(); } } long current = 0; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { FileOutputStream fileOutputStream = null; if (isResume) { current = targetFile.length(); fileOutputStream = new FileOutputStream(target, true); } else { fileOutputStream = new FileOutputStream(target); } long total = entity.getContentLength() + current; bis = new BufferedInputStream(entity.getContent()); bos = new BufferedOutputStream(fileOutputStream); if (callBackHandler != null && !callBackHandler.updateProgress(total, current, true)) { return targetFile; } byte[] tmp = new byte[4096]; int len; while ((len = bis.read(tmp)) != -1) { bos.write(tmp, 0, len); current += len; if (callBackHandler != null) { if (!callBackHandler.updateProgress(total, current, false)) { return targetFile; } } } bos.flush(); if (callBackHandler != null) { callBackHandler.updateProgress(total, current, true); } } finally { IOUtils.closeQuietly(bis); IOUtils.closeQuietly(bos); } if (targetFile.exists() && !TextUtils.isEmpty(responseFileName)) { File newFile = new File(targetFile.getParent(), responseFileName); while (newFile.exists()) { newFile = new File(targetFile.getParent(), System.currentTimeMillis() + responseFileName); } return targetFile.renameTo(newFile) ? newFile : targetFile; } else { return targetFile; } }
From source file:com.drive.student.xutils.http.callback.FileDownloadHandler.java
public File handleEntity(HttpEntity entity, com.drive.student.xutils.http.callback.RequestCallBackHandler callBackHandler, String target, boolean isResume, String responseFileName) throws IOException { if (entity == null || TextUtils.isEmpty(target)) { return null; }/*from w w w.j a v a 2 s. c om*/ File targetFile = new File(target); if (!targetFile.exists()) { File dir = targetFile.getParentFile(); if (dir.exists() || dir.mkdirs()) { targetFile.createNewFile(); } } long current = 0; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { FileOutputStream fileOutputStream = null; if (isResume) { current = targetFile.length(); fileOutputStream = new FileOutputStream(target, true); } else { fileOutputStream = new FileOutputStream(target); } long total = entity.getContentLength() + current; bis = new BufferedInputStream(entity.getContent()); bos = new BufferedOutputStream(fileOutputStream); if (callBackHandler != null && !callBackHandler.updateProgress(total, current, true)) { return targetFile; } byte[] tmp = new byte[4096]; int len; while ((len = bis.read(tmp)) != -1) { bos.write(tmp, 0, len); current += len; if (callBackHandler != null) { if (!callBackHandler.updateProgress(total, current, false)) { return targetFile; } } } bos.flush(); if (callBackHandler != null) { callBackHandler.updateProgress(total, current, true); } } finally { IOUtils.closeQuietly(bis); IOUtils.closeQuietly(bos); } if (targetFile.exists() && !TextUtils.isEmpty(responseFileName)) { File newFile = new File(targetFile.getParent(), responseFileName); while (newFile.exists()) { newFile = new File(targetFile.getParent(), System.currentTimeMillis() + responseFileName); } return targetFile.renameTo(newFile) ? newFile : targetFile; } else { return targetFile; } }
From source file:org.cruk.seq.SampleFastq.java
/** * Writes an XML file containing a summary of the sampling. * * @param datasetId the dataset id// w w w . j ava 2s.c om * @param sampledCount the sample size */ private void writeSummary(String datasetId, int sampledCount) { if (summaryFilename == null) return; BufferedOutputStream outputStream = null; try { outputStream = new BufferedOutputStream(new FileOutputStream(summaryFilename)); } catch (IOException e) { error("Error creating file " + summaryFilename); } try { Element root = new Element("SamplingSummary"); Element element = new Element("DatasetId"); if (datasetId != null) element.appendChild(datasetId); root.appendChild(element); element = new Element("SampledCount"); element.appendChild(Integer.toString(sampledCount)); root.appendChild(element); Document document = new Document(root); Serializer serializer; serializer = new Serializer(outputStream, "ISO-8859-1"); serializer.setIndent(2); serializer.setMaxLength(64); serializer.setLineSeparator("\n"); serializer.write(document); outputStream.flush(); } catch (UnsupportedEncodingException e) { error(e); } catch (IOException e) { error("Error writing summary XML file"); } finally { try { outputStream.close(); } catch (IOException e) { error("Error closing file " + summaryFilename); } } }