List of usage examples for java.io OutputStream flush
public void flush() throws IOException
From source file:Main.java
public static void copy(InputStream in, OutputStream out) throws IOException { if (!(in instanceof BufferedInputStream)) { in = new BufferedInputStream(in); }/*from w w w. java2s . c o m*/ if (!(out instanceof BufferedOutputStream)) { out = new BufferedOutputStream(out); } int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.flush(); }
From source file:com.fizzed.blaze.util.Streamables.java
static private void copy(InputStream input, OutputStream output, int bufferSize) throws IOException { int read;/*from w w w . j a va 2 s. com*/ byte[] buffer = new byte[bufferSize]; while ((read = input.read(buffer)) > -1) { output.write(buffer, 0, read); } output.flush(); }
From source file:com.photon.phresco.util.FileUtil.java
public static File writeFileFromInputStream(InputStream inputStream, String tempZip) throws PhrescoException { File tempZipFile = new File(tempZip); try {//from ww w. ja v a2 s . c om if (!tempZipFile.exists()) { tempZipFile.createNewFile(); } OutputStream out = new FileOutputStream(new File(tempZip)); int read = 0; byte[] bytes = new byte[BUFFER_SIZE]; while ((read = inputStream.read(bytes)) != -1) { out.write(bytes, 0, read); } inputStream.close(); out.flush(); out.close(); } catch (Exception e) { throw new PhrescoException(e); } return tempZipFile; }
From source file:Main.java
public static void copyFile(File from, File to) { if (null == from || !from.exists()) { return;// w w w . j av a 2s.co m } if (null == to) { return; } InputStream is = null; OutputStream os = null; try { is = new FileInputStream(from); if (!to.exists()) { to.createNewFile(); } os = new FileOutputStream(to); byte[] buffer = new byte[1024]; int len = 0; while (-1 != (len = is.read(buffer))) { os.write(buffer, 0, len); } os.flush(); } catch (Exception e) { } finally { closeIO(is, os); } }
From source file:com.hunantv.fw.utils.StreamUtils.java
/** * Copy the contents of the given InputStream to the given OutputStream. * Leaves both streams open when done./*from www .j a v a 2 s . c o m*/ * @param in the InputStream to copy from * @param out the OutputStream to copy to * @return the number of bytes copied * @throws IOException in case of I/O errors */ public static int copy(InputStream in, OutputStream out) throws IOException { int byteCount = 0; byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); byteCount += bytesRead; } out.flush(); return byteCount; }
From source file:com.buglabs.dragonfly.util.WSHelper.java
/** * @param url/*from w w w. j a va 2s . c o m*/ * @param inputStream * @return * @throws IOException */ protected static String post(URL url, InputStream inputStream) throws IOException { URLConnection connection = url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); OutputStream stream = connection.getOutputStream(); writeTo(stream, inputStream); stream.flush(); stream.close(); InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line, resp = new String(""); while ((line = rd.readLine()) != null) { resp = resp + line + "\n"; } rd.close(); is.close(); return resp; }
From source file:com.iyonger.apm.web.util.FileDownloadUtils.java
/** * Download the given file to the given {@link HttpServletResponse}. * * @param response {@link HttpServletResponse} * @param file file path//from w w w .j a v a2 s.c o m * @return true if succeeded */ public static boolean downloadFile(HttpServletResponse response, File file) { if (file == null || !file.exists()) { return false; } boolean result = true; response.reset(); response.addHeader("Content-Disposition", "attachment;filename=" + file.getName()); response.setContentType("application/octet-stream"); response.addHeader("Content-Length", "" + file.length()); InputStream fis = null; byte[] buffer = new byte[FILE_DOWNLOAD_BUFFER_SIZE]; OutputStream toClient = null; try { fis = new BufferedInputStream(new FileInputStream(file)); toClient = new BufferedOutputStream(response.getOutputStream()); int readLength; while (((readLength = fis.read(buffer)) != -1)) { toClient.write(buffer, 0, readLength); } toClient.flush(); } catch (FileNotFoundException e) { LOGGER.error("file not found:" + file.getAbsolutePath(), e); result = false; } catch (IOException e) { LOGGER.error("read file error:" + file.getAbsolutePath(), e); result = false; } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(toClient); } return result; }
From source file:Main.java
public static boolean copyFile(File src, File dst) { InputStream is = null;/*from w w w . j a v a 2 s . c o m*/ OutputStream os = null; byte[] buffer = new byte[1024]; try { is = new FileInputStream(src); os = new FileOutputStream(dst); while (true) { int len = is.read(buffer); if (len <= 0) { break; } os.write(buffer, 0, len); } os.flush(); return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { if (is != null) { try { is.close(); } catch (IOException e) { // ignore } } if (os != null) { try { os.close(); } catch (IOException e) { // ignore } } } }
From source file:com.mtt.myapp.common.util.FileDownloadUtil.java
/** * Provide file download from the given file path. * @param response {@link javax.servlet.http.HttpServletResponse} * @param desFile file path/* w w w. j av a 2s . co m*/ * @return true if succeeded */ public static boolean downloadFile(HttpServletResponse response, File desFile) { if (desFile == null || !desFile.exists()) { return false; } boolean result = true; response.reset(); response.addHeader("Content-Disposition", "attachment;filename=" + desFile.getName()); response.setContentType("application/octet-stream"); response.addHeader("Content-Length", "" + desFile.length()); InputStream fis = null; byte[] buffer = new byte[FILE_DOWNLOAD_BUFFER_SIZE]; OutputStream toClient = null; try { fis = new BufferedInputStream(new FileInputStream(desFile)); toClient = new BufferedOutputStream(response.getOutputStream()); int readLength; while (((readLength = fis.read(buffer)) != -1)) { toClient.write(buffer, 0, readLength); } toClient.flush(); } catch (FileNotFoundException e) { LOGGER.error("file not found:" + desFile.getAbsolutePath(), e); result = false; } catch (IOException e) { LOGGER.error("read file error:" + desFile.getAbsolutePath(), e); result = false; } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(toClient); } return result; }
From source file:com.textocat.textokit.commons.io.ProcessIOUtils.java
/** * @param proc process which input stream will receive bytes from the * argument input stream// w ww. j a va 2 s . c o m * @param in input stream. Note that it is closed at the end. */ public static void feedProcessInput(Process proc, final InputStream in, final boolean closeStdIn) throws IOException { final OutputStream procStdIn = proc.getOutputStream(); final List<Exception> exceptions = Lists.newLinkedList(); Thread writerThread = new Thread(new Runnable() { @Override public void run() { try { IOUtils.copy(in, procStdIn); if (closeStdIn) { procStdIn.flush(); closeQuietly(procStdIn); } } catch (Exception e) { exceptions.add(e); } finally { closeQuietly(in); } } }); writerThread.start(); try { writerThread.join(); } catch (InterruptedException e) { // do nothing, just set flag Thread.currentThread().interrupt(); } if (!exceptions.isEmpty()) { Exception ex = exceptions.get(0); throw ex instanceof IOException ? (IOException) ex : new IOException("Unexpected exception in writing thread", ex); } }