Convert data from UTF8 to GB2312 format in Java

Source: Internet
Author: User

UTF8 to GB2312 when we get data or send requests in HTTP-based JSP or Servlet applications, JVM will encode the transmitted data into UTF8 format. If we extract Chinese data directly from the HTTP stream, the extracted result is "???" (May be more question mark), in order to convert into Chinese characters we can understand, we need to convert UTF8 into GB2312, with the help of ISO-8859-1 standard encoding can be easily implemented, the following code implements this function:

Byte [] B;
String utf8_value;
Utf8_value = request. getParameter ("NAME"); // obtain the UTF8 data of "NAME" from the HTTP stream
B = utf8_value.getBytes ("8859_1"); // use the ISO-8859-1 in the middle of the transition
String name = new String (B, "GB2312"); // convert to GB2312

When the stream length is known, the input stream is converted into a byte array. The input stream abstract class InputStream in Java has the int read (byte [] B, int off, int len) method, in the parameter, byte [] B is used to store data read from InputStream. int off specifies the offset address of array B, that is, the starting subscript of array B, int len specifies the length to be read. The method returns the actual number of bytes read.

A friend who just learned Java may want to say: first define a byte array with the stream length, call the read method, specify the starting subscript as 0, and specify the read length and array length, can it be read at once? I tried to read the data in this way, but it is not safe to read network data later. We think it may not be so smooth to obtain data on the network, data streams may be transmitted intermittently, so it cannot be guaranteed that all data can be read at a time, especially when reading large data volumes. Therefore, we must check the actual read length when reading data, if you have not read data of a known length, read the data again until the actual read length is accumulated and the known length is equal. The following code implements this function:

ServletInputStream inStream = request. getInputStream (); // retrieves the HTTP request stream
Int size = request. getContentLength (); // get the HTTP request stream Length
Byte [] buffer = new byte [size]; // used to cache data read each time
Byte [] in_ B = new byte [size]; // array used to store results
Int count = 0;
Int rbyte = 0;
While (count <size ){
// Read cyclically
Rbyte = inStream. read (buffer); // The actual read length is stored in rbyte.
For (int I = 0; I <rbyte; I ++ ){
In_ B [count + I] = buffer [I];
}
Count + = rbyte;
}

If you do not know the stream length, convert the input stream to a byte array. The previous section describes the conversion method when the stream length is known. When we do not know how long the stream is, that is to say, it cannot be determined how large the converted byte array is. How can this problem be solved? After reading the JDK documentation, I found that ByteArrayOutputStream has a byte [] toByteArray () method. This method automatically creates a byte array and returns it. Therefore, ByteArrayOutputStream is cleverly used for intermediate transition to implement conversion. Other processing is similar to the known length described above. Suppose the stream to be converted has already been placed in inStream. We can use the following code to implement this function:

ByteArrayOutputStream swapStream = new ByteArrayOutputStream ();
Byte [] buff = new byte [100]; // buff is used to store temporary data read cyclically.
Int rc = 0;

While (rc = inStream. read (buff, 0,100)> 0 ){
SwapStream. write (buff, 0, rc );
}
Byte [] in_ B = swapStream. toByteArray (); // The converted result is in_ B.

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.