Conversion between byte, String, and InputStream in java

Source: Internet
Author: User

[Html] <pre class = "html" name = "code"> import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. inputStream; public class InputStreamUtils {final static int BUFFER_SIZE = 4096; /*** convert InputStream to String * @ param in InputStream * @ return String * @ throws Exception **/public static String InputStreamTOString (InputStream in) throws into t Ion {ByteArrayOutputStream outStream = new ByteArrayOutputStream (); byte [] data = new byte [BUFFER_SIZE]; int count =-1; while (count = in. read (data, 0, BUFFER_SIZE ))! =-1) outStream. write (data, 0, count); data = null; return new String (outStream. toByteArray (), "ISO-8859-1 ");} /*** convert InputStream to a character-encoded String * @ param in * @ param encoding * @ return * @ throws Exception */public static String InputStreamTOString (InputStream in, string encoding) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream (); byte [] data = new byte [BUFFER_SIZE]; I Nt count =-1; while (count = in. read (data, 0, BUFFER_SIZE ))! =-1) outStream. write (data, 0, count); data = null; return new String (outStream. toByteArray (), "ISO-8859-1");}/*** convert String to InputStream * @ param in * @ return * @ throws Exception */public static InputStream StringTOInputStream (String in) throws Exception {ByteArrayInputStream is = new ByteArrayInputStream (in. getBytes ("ISO-8859-1"); return is;}/*** converts InputStream to a byte array * @ param in InputStrea M * @ return byte [] * @ throws IOException */public static byte [] InputStreamTOByte (InputStream in) throws IOException {ByteArrayOutputStream outStream = new ByteArrayOutputStream (); byte [] data = new byte [BUFFER_SIZE]; int count =-1; while (count = in. read (data, 0, BUFFER_SIZE ))! =-1) outStream. write (data, 0, count); data = null; return outStream. toByteArray ();}/*** converts the byte array to InputStream * @ param in * @ return * @ throws Exception */public static InputStream byteTOInputStream (byte [] in) throws Exception {ByteArrayInputStream is = new ByteArrayInputStream (in); return is ;} /*** convert byte array to String * @ param in * @ return * @ throws Exception */www.2cto.com public static String byteTOString (byte [] in) throws Exception {InputStream is = byteTOInputStream (in); return InputStreamTOString (is) ;}</pre> <br> <pre> </pre> <br>

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.