Android converts inputstream to string and byte []

Source: Internet
Author: User

Why do I need to convert inputstream to string? To implement the encryption function, the input of the encryption function is string.

Public static string inputstream2string (inputstream in) throws ioexception {stringbuffer out = new stringbuffer (); byte [] B = new byte [4096]; int N; while (n = in. read (B ))! =-1) {out. append (new string (B, 0, n);} log. I ("String Length", new INTEGER (Out. length ()). tostring (); Return out. tostring ();}

Through various getinputstream methods, you can convert httpurlconnection, input text stream, and so on to string. Of course, you can also convert it to byte []

Public static byte [] inputstreamtobyte (inputstream is) throws ioexception {bytearrayoutputstream bytestream = new bytearrayoutputstream (); int ch; while (CH = is. Read ())! =-1) {bytestream. Write (CH);} byte imgdata [] = bytestream. tobytearray (); bytestream. Close (); Return imgdata ;}

Related Article

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.