List of usage examples for java.io OutputStream flush
public void flush() throws IOException
From source file:ZipSocket.java
public synchronized void close() throws IOException { OutputStream o = getOutputStream(); o.flush(); super.close();/*from w w w . j a v a2 s . com*/ }
From source file:org.csware.ee.utils.Tools.java
public static String reqByPostSI(String path, String cookie, String params) { String info = ""; String sessionId = ""; try {//from www.j a v a2s . c o m URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(20000); conn.setRequestMethod("POST"); conn.setDoOutput(true); // conn.setDoInput(true); // conn.setInstanceFollowRedirects(isJump); conn.setRequestProperty("Cookie", cookie); conn.setRequestProperty("Connection", "keep-alive"); conn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch"); conn.setRequestProperty("Cache-Control", "max-age=0"); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); conn.setRequestProperty("Accept", "application/json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); // conn.setRequestProperty("Host", "creditcardapp.bankcomm.com"); // conn.setRequestProperty("Origin", "https://creditcardapp.bankcomm.com"); // conn.setRequestProperty("Referer", "https://creditcardapp.bankcomm.com/member/home/index.html"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"); // byte[] bypes = params.getBytes(); OutputStream os = conn.getOutputStream(); os.write(bypes);// ? os.flush(); os.close(); InputStream in = conn.getInputStream(); // System.out.println(conn.getHeaderField("Set-Cookie")); try { byte[] buffer = new byte[65536]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); int readLength = in.read(buffer); long totalLength = 0; while (readLength >= 0) { bos.write(buffer, 0, readLength); totalLength = totalLength + readLength; readLength = in.read(buffer); } info = bos.toString("UTF-8"); } finally { if (in != null) try { in.close(); } catch (Exception e) { e.printStackTrace(); } } // //conn.setConnectTimeout(5 * 1000); // // conn.connect(); } catch (Exception ex) { ex.printStackTrace(); return ""; } return info; }
From source file:com.chinamobile.bcbsp.fault.tools.HdfsOperater.java
/** * update file to hdfs//from w w w . j a va2 s .co m * @param localPath * local file path to update * @param destPath * destination path in hdfs * @return upload result */ public static String uploadHdfs(String localPath, String destPath) { InputStream in = null; OutputStream out = null; try { String localSrc = localPath; File srcFile = new File(localSrc); if (srcFile.exists()) { // String dst = hostName + dirPath; in = new BufferedInputStream(new FileInputStream(localSrc)); // Configuration conf = new Configuration(); // FileSystem fs = FileSystem.get(URI.create(destPath), conf); BSPHdfs Hdfsup = new BSPHdfsImpl(); // out = fs.create(new Path(destPath), out = Hdfsup.hdfsOperater(destPath, Hdfsup.getConf()); // new Progressable() { // public void progress() { // // } // }); IOUtils.copyBytes(in, out, 4096, true); out.flush(); out.close(); in.close(); return destPath; } else { return "error"; } } catch (FileNotFoundException e) { LOG.error("[uploadHdfs]", e); return "error"; } catch (IOException e) { LOG.error("[uploadHdfs]", e); try { if (out != null) { out.flush(); } if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException e1) { LOG.error("[uploadHdfs]", e1); } return "error"; } }
From source file:it.qvc.ClientHttpRequest.java
/** * // w ww . ja v a2s.c o m * @param aInputStream * @param aOutputStream * @throws IOException */ private static void pipe(final InputStream aInputStream, final OutputStream aOutputStream) throws IOException { byte[] tByte = new byte[500000]; int iNbRead; int iTotal = 0; synchronized (aInputStream) { while ((iNbRead = aInputStream.read(tByte, 0, tByte.length)) >= 0) { aOutputStream.write(tByte, 0, iNbRead); iTotal += iNbRead; } } aOutputStream.flush(); tByte = null; }
From source file:Files.java
/** * Copy all of the bytes from the input stream to the output stream * wrapping streams in buffers as needed. * * @param input Stream to read bytes from. * @param output Stream to write bytes to. * @return The total number of bytes copied. * * @throws IOException Failed to copy bytes. *///from w ww. ja v a2 s . c om public static long copyb(InputStream input, OutputStream output) throws IOException { if (!(input instanceof BufferedInputStream)) { input = new BufferedInputStream(input); } if (!(output instanceof BufferedOutputStream)) { output = new BufferedOutputStream(output); } long bytes = copy(input, output, DEFAULT_BUFFER_SIZE); output.flush(); return bytes; }
From source file:com.vk.crawler.core.util.FileUtils.java
public static boolean createFile(InputStream inStream, String fileName, String outFilePath) throws Exception { //TODO use apache common-io methods. UPDATE- Apache.io is less efficient than Java 7 OutputStream outStream = null; boolean rtn = false; try {/*w w w .jav a 2 s . c om*/ if (StringUtils.isEmpty(outFilePath) || StringUtils.isEmpty(fileName)) { return rtn; } Files.createDirectories(Paths.get(outFilePath)); logger.info("writing file " + fileName + " to directory " + outFilePath); outStream = new FileOutputStream(new File(outFilePath, fileName)); int read = 0; byte[] bytes = new byte[1024]; while ((read = inStream.read(bytes)) != -1) { outStream.write(bytes, 0, read); } logger.info("file " + fileName + " saved successfully."); rtn = true; } catch (Exception e) { logger.error("saving document " + e); } finally { if (outStream != null) { try { outStream.flush(); outStream.close(); } catch (Exception e) { logger.error("Error in finally."); } } } return rtn; }
From source file:com.fjn.helper.common.io.file.common.FileUpAndDownloadUtil.java
public static void downloadToBrowser(String filename, String realpath, HttpServletRequest request, HttpServletResponse response) {/* w w w . j a v a 2 s . c om*/ // ?MIME TYPE? //try { // mimetypesMap=new MimetypesFileTypeMap(request.getSession().getServletContext().getRealPath("WEB-INF/my.mime.types")); mimetypesMap = new MimetypesFileTypeMap(); //} catch (IOException e1) { // e1.printStackTrace(); //} // response.setCharacterEncoding("UTF-8"); response.setContentType(mimetypesMap.getContentType(filename)); response.addHeader("Content-Disposition", "attachment;filename=" + toUTF8(filename)); FileInputStream in = null; OutputStream out = null; try { /* ???? BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(new File(realpath)))); BufferedWriter out = new BufferedWriter(new OutputStreamWriter( response.getOutputStream())); int length = -1; char[] chs = new char[1024]; while ((length = in.read(chs)) != -1) { out.write(chs, 0, length); } */ in = new FileInputStream(new File(realpath)); out = response.getOutputStream(); int length = -1; byte[] bs = new byte[1024]; while ((length = in.read(bs)) != -1) { out.write(bs, 0, length); } out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (Exception ex) { } } }
From source file:biz.varkon.shelvesom.util.ImageUtilities.java
/** * Loads an image from the specified URL with the specified cookie. * // w w w . j a v a 2 s .c om * @param url * The URL of the image to load. * @param cookie * The cookie to use to load the image. * * @return The image at the specified URL or null if an error occured. */ public static ExpiringBitmap load(String url, String cookie) { ExpiringBitmap expiring = new ExpiringBitmap(); final HttpGet get = new HttpGet(url); if (cookie != null) get.setHeader("cookie", cookie); HttpEntity entity = null; try { final HttpResponse response = HttpManager.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { setLastModified(expiring, response); entity = response.getEntity(); InputStream in = null; OutputStream out = null; try { in = entity.getContent(); if (FLAG_DECODE_BITMAP_WITH_SKIA) { expiring.bitmap = BitmapFactory.decodeStream(in); } else { final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, IOUtilities.IO_BUFFER_SIZE); IOUtilities.copy(in, out); out.flush(); final byte[] data = dataStream.toByteArray(); final double ratio = getScale(in); final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio); bitmapOptions.inDither = true; bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; expiring.bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, bitmapOptions); } } catch (IOException e) { android.util.Log.e(LOG_TAG, "Could not load image from " + url, e); } catch (OutOfMemoryError oom) { expiring.bitmap = null; } finally { IOUtilities.closeStream(in); IOUtilities.closeStream(out); } } } catch (IOException e) { android.util.Log.e(LOG_TAG, "Could not load image from " + url, e); } finally { if (entity != null) { try { entity.consumeContent(); } catch (IOException e) { android.util.Log.e(LOG_TAG, "Could not load image from " + url, e); } } } return expiring; }
From source file:httpurlconnection.JsonBody.java
@Override public void writeTo(OutputStream out) throws IOException { out.write(data); out.flush(); }
From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java
/** * Convenient way of sending data from an input stream to an output stream * in the most efficient way possible//w ww . j a va 2 s.com * If the bCloseOutput flag is false, then the output stream remains open * so that further writes can be made to the stream * * @param in Input stream to read from * @param out Output stream to write to * @param closeOutput True if the output stream should be closed on exit * @param ignoreErrors True if this method must not throw any socket errors * * @throws IOException if an error occurs */ public static void pipeInputToOutputStream(InputStream in, OutputStream out, boolean closeOutput, boolean ignoreErrors) throws IOException { OutputStream bufferedOut = out; InputStream bufferedIn = in; if (in != null && out != null) { try { // Buffer the streams if they aren't already if (!bufferedOut.getClass().equals(BufferedOutputStream.class)) { bufferedOut = new BufferedOutputStream(bufferedOut, DEFAULT_BUFFER_SIZE); } if (!bufferedIn.getClass().equals(BufferedInputStream.class)) { bufferedIn = new BufferedInputStream(bufferedIn, DEFAULT_BUFFER_SIZE); } // Push the data int iTmp; while ((iTmp = bufferedIn.read()) != -1) { bufferedOut.write((byte) iTmp); } bufferedOut.flush(); out.flush(); } catch (IOException e) { if (!ignoreErrors && !(e instanceof java.net.SocketException)) { logger.error(e.getMessage()); throw e; } else { logger.debug(e.getMessage()); } } finally { bufferedIn.close(); if (closeOutput) { bufferedOut.close(); } } } }