Java Common Stream Processing tool streamtool Common InputStream flow string, byte array, etc.

Source: Internet
Author: User

Original: Java Common Stream Processing tool streamtool Common InputStream flow string, byte array, etc.

Source code Download: http://www.zuidaima.com/share/1550463397366784.htm

Ava Common Stream Processing tool streamtool, common InputStream flow string, byte array, etc.

* * Application Scenario:

* * 1. File Upload

2. Js/css/img and other files read output. Turn string output (JS & CSS), turn byte array output (IMG: swf etc ...)

3. Crawl the resources for the specified URL connection.

For example, read the HTML source code for the Javaniu home page

4. As you can see ....

/* Copyright 2012-2013 the Haohui Network Corporation */package com.zuidaima.haohui.common.utils;import Java.io.ByteA Rrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.file;import Java.io.FileOutputStream;import Java.io.ioexception;import java.io.inputstream;/** * <pre> * Java Common Stream Processing tool * </pre> * * @project Baidamei * @a Uthor Cevencheng <[email protected]> www.zuidaima.com * @create 2012-11-29 pm 5:17:32 */public class Streamtool {/** * Save stream as File * * @param is * @param outfile */public void Streamsaveasfile (InputStream is, file outfile) {Fi Leoutputstream fos = null;try {File File = Outfile;fos = new FileOutputStream (file); byte[] buffer = new Byte[1024];int len ; while (len = is.read (buffer)) > 0) {fos.write (buffer, 0, Len);}} catch (Exception e) {e.printstacktrace (); throw new RuntimeException (e);} finally {try {is.close (); Fos.close ();} catch ( Exception E2) {e2.printstacktrace (); throw new RuntimeException (E2);}} /** * Read an input stream intoA String * @param in * @return * @throws ioexception */static public string streamtostring (InputStream in) throws Ioexcept Ion {StringBuffer out = new StringBuffer (); byte[] B = new byte[4096];for (int n; (n = in.read (b))! =-1;) {Out.append (new String (b, 0, N));} return out.tostring ();} public static byte[] Stream2byte (InputStream is) throws IOException {Bytearrayoutputstream BAOs = new Bytearrayoutputstre AM (); int len = 0;byte[] B = new Byte[1024];while (len = is.read (b, 0, b.length))! =-1) {BAOS.WR ITE (b, 0, Len);} byte[] buffer = Baos.tobytearray (); return buffer;} /** * @ Method function InputStream to BYTE * @param inputstream * @return byte array * @throws Exception */P Ublic Static byte[] Inputstream2byte (InputStream instream) throws Exception {//Bytearrayoutputstre          Am Outsteam = new Bytearrayoutputstream ();          byte[] buffer = new byte[1024];          int len =-1; while (len = instream.read (buffer)) ! =-1) {//outsteam.write (buffer, 0, Len);          }//Outsteam.close ();          Instream.close ();          return Outsteam.tobytearray ();          int count = 0;          while (count = = 0) {count = Instream.available ();          } byte[] B = new Byte[count];          Instream.read (b);      return b;      }/** * @ Method function byte to InputStream * @param byte array * @return InputStream * @throws Exception */public static InputStream Byte2inputstream (byte[] b) throws Exception {InputStream is = new BYTEARRAYINP          Utstream (b);      return is; }/** * @ function short integer and byte conversion * @param short integer * @return two-bit byte array */public static byte[] Shorttobyte (s          Hort number) {int temp = number;          Byte[] B = new byte[2];              for (int i = 0; i < b.length; i++) {B[i] = new Integer (temp & 0xff). Bytevalue ();//Keep the lowest bit in the lowest bit temp = temp >> 8;      Move right 8 bits} return B; }/** * @ function byte conversion with short integer * @param two-bit byte array * @return short integer */public static shorter Bytetoshort (by          Te[] b) {short s = 0;          Short s0 = (short) (B[0] & 0xff);//lowest bit short S1 = (short) (B[1] & 0xff);          S1 <<= 8;          s = (short) (S0 | s1);      return s; }/** * @ Method function integer and byte array conversion * @param integer * @return four-bit byte array */public static byte[] Inttobyte (i          NT i) {byte[] bt = new BYTE[4];          Bt[0] = (byte) (0xFF & i);          BT[1] = (byte) ((0xff00 & i) >> 8);          BT[2] = (byte) ((0xff0000 & i) >> 16);          BT[3] = (byte) ((0xff000000 & i) >> 24);      Return BT; }/** * @ Method function byte array and integral type conversion * @param byte array * @return int * @author www.zuidaima.com */Publ         IC static int bytestoint (byte[] bytes) {int num = bytes[0] & 0xFF; Num |= ((bytes[1] << 8) & 0xff00);          Num |= ((bytes[2] <<) & 0xFF0000);          Num |= ((bytes[3] <<) & 0xff000000);      return num; }/** * @ Method function byte array and long integer conversion * @param byte array * @return Long integer */public static byte[] Longtobyte (l          Ong number) {Long temp = number;          Byte[] B = new Byte[8];              for (int i = 0; i < b.length; i++) {B[i] = new Long (temp & 0xff). Bytevalue ();              Save the lowest bit at the lowest bit temp = temp >> 8;      Move right 8 bits} return B; }/** * @ Method function byte array and long integer conversions * @param byte array * @return Long integer */public static long Bytetolong (byt          E[] b) {long s = 0;          Long S0 = b[0] & 0xff;//lowest bit long S1 = b[1] & 0xff;          Long s2 = b[2] & 0xff;          Long s3 = b[3] & 0xff;      Long S4 = b[4] & 0xff;//lowest bit long S5 = b[5] & 0xff;    Long s6 = b[6] & 0xff; Long s7 = b[7] & 0xff;          S0 unchanged S1 <<= 8;          S2 <<= 16;          S3 <<= 24;          S4 <<= 8 * 4;          S5 <<= 8 * 5;          S6 <<= 8 * 6;          S7 <<= 8 * 7; s = S0 | S1 | S2 | S3 | S4 | S5 | S6 |          S7;      return s;     }  }

Java Common Stream Processing tool streamtool Common InputStream flow string, byte array, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.