Conversion between inputstream, string, and byte

Source: Internet
Author: User
Tags array to string

This article describes the conversion between inputstream and string and byte. ToCodeTo describe:
[HTML] view plain
Copy

Import java. Io. bytearrayinputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;

/**
*
* @ Author Andy. Chen
* @ Mail Chenjunjun.ZJ@gmail.com
*
*/
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 exception {

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];
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 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;
}

/**
* Convert inputstream to byte array
* @ Param in inputstream
* @ 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 ();
}

/**
* Convert 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
*/
Public static string bytetostring (byte [] In) throws exception {

Inputstream is = bytetoinputstream (in );
Return inputstreamtostring (is );
}

}

Http://blog.csdn.net/cjjky/article/details/6892443

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.