Three special methods of Java parsing network data flow

Source: Internet
Author: User
Tags int size

Java, as the most open language, has become more and more popular with web programmers. But this favored tribe has the same experience--and has been bothered by the Java data format used to communicate on the Web.

The author is no exception, once consulted a lot of documents, after repeated testing to be resolved, now I have been engaged in many e-government projects in the smooth application. Today, I write this article, introduces the use of Java to parse network data three special methods, I hope to seek answers to this question to help readers.

UTF8 converted to GB2312 when we get data or send requests in a JSP or servlet application based on the HTTP protocol, the JVM encodes the transmitted data into UTF8 format. If we extract Chinese data directly from the HTTP stream, the result is "????" (possibly more question marks), we need to convert UTF8 into GB2312 to convert to Chinese characters we can understand, and with Iso-8859-1 Standard code can be easily implemented, the following code implements this function:

byte [] b;
String utf8_value;
utf8_value = request.getParameter("NAME");//从HTTP流中取"NAME"的UTF8数据
b = utf8_value.getBytes("8859_1"); //中间用ISO-8859-1过渡
String name = new String(b, "GB2312"); //转换成GB2312字符

When you know the length of the stream, change the input flow to an input stream abstract class in Java of byte array inputstream has int read (byte[] b, int off, int len) method, parameters in byte[] B is used to hold data read from InputStream, int off specifies the offset address of array B, which is the starting subscript for array B, int len Specifies the length to read, and the method returns the number of bytes actually read. Just learn Java friend may say: First define a byte array with the length of the stream, call the Read method, specify the starting subscript 0, specify the read length and length of the array, not all of a sudden can read it? That's right, I've tried to read the data like this, but it turned out to be unsafe when reading network data, and we thought that getting the data on the network might not be as smooth and that the data stream could be transmitted intermittently, so it was not guaranteed to read all the data at once, especially when reading large volumes of data, So we have to detect the actual length of the read when we read the data, and if we do not read the known length of the data, we should read it again, so that the loop is detected until the actual read length is equal to the known length, and the following code implements this functionality:

ServletInputStream inStream = request.getInputStream(); //取HTTP请求流
int size = request.getContentLength(); //取HTTP请求流长度
byte[] buffer = new byte[size]; //用于缓存每次读取的数据
byte[] in_b = new byte[size]; //用于存放结果的数组
int count = 0;
int rbyte = 0;
while (count < size) { //循环读取
rbyte = inStream.read(buffer); //每次实际读取长度存于rbyte中
for(int i=0;i<rbyte;i++) {
in_b[count + i] = buffer[i];
}
count += rbyte;
}

When you do not know the length of the stream in the case of converting the input stream into a byte array before the known flow length of the conversion method, so when we do not know how long the flow, that is, not sure how large the converted byte array, how to deal with it? After reviewing the JDK documentation, I found that Bytearrayoutputstream has a byte[] Tobytearray () method that automatically creates a byte array and then returns. So the ingenious use of bytearrayoutputstream for intermediate transitions to implement the conversion, the other processing with the known length described above is similar. Assuming that the stream that needs to be converted is already in the instream, we can use the following code to implement this function:

ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = inStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] in_b = swapStream.toByteArray(); //in_b为转换之后的结果

The above introduces three kinds of more practical Java Data conversion, interested friends can get in touch with the author through nbDeveloper@hotmail.com, further exchange other knowledge about XML application, programming pattern, Java EE Development and UML.

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.