Java codec technology, Netty NiO

Source: Internet
Author: User
Tags ming

For Java-provided object input and output streams ObjectInputStream and ObjectOutputStream, you can write Java objects directly to the file as a stored byte array, or you can transfer them to the network. For Java developers, the default JDK serialization mechanism avoids manipulating the underlying byte array, which can improve development efficiency.

1. Why serialization is required

Network Transmission and serialization of Objects

What the 2.java codec technology means

Netty NiO is based on network transmission, when making a remote cross-process service call, the transmitted object needs to be encoded as a byte array or Bytebuffer object. When the remote service reads to the Bytebuffer object or byte array, it needs to decode the Java object when it is sent. This is the encoding and decoding technique for Java objects.

Disadvantages of 3.java Original ecological serialization

(1) cannot cross the language, this is the most deadly problem. When a cross-process service is called, the rest of the language, such as when interacting with C + +, is difficult for Java serialization to perform. Java serialization is using the Java internal Private protocol

(2) The code stream is too large after serialization

Such as:

1  PackageCom.ming.netty.code;2 3 Importjava.io.Serializable;4 ImportJava.nio.ByteBuffer;5 6 7 /**8 * An ordinary student class9  * @authorMinggeTen  * One  */ A  Public classStudentImplementsserializable{ -     Private Static Final LongSerialversionuid = 1L; -  the     PrivateString Stuname; -  -  -      PublicString Getstuname () { +         returnStuname; -     } +  A      Public voidsetstuname (String stuname) { at          This. Stuname =Stuname; -     } -      -      Public byte[] CodeC () { -Bytebuffer Buffer=bytebuffer.allocate (1024); -         byte[] value= This. Getstuname (). GetBytes (); in Buffer.putint (value.length); - buffer.put (value); to Buffer.flip (); +Value=NULL; -         byte[] result=New byte[Buffer.remaining ()]; the buffer.get (result); *         returnresult; $     }Panax Notoginseng      -}
1  PackageCom.ming.netty.code;2 3 ImportJava.io.ByteArrayOutputStream;4 Importjava.io.IOException;5 ImportJava.io.ObjectOutputStream;6 7  Public classteststudent {8 9      Public Static voidMain (string[] args)throwsIOException {TenStudent s=NewStudent (); OneS.setstuname ("Zhang San"); ABytearrayoutputstream bos=NewBytearrayoutputstream (); -ObjectOutputStream os=NewObjectOutputStream (BOS); - Os.writeobject (s); the Os.flush (); - os.close (); -         byte[] b=Bos.tobytearray (); -System.out.println ("JDK Serialization length:" +b.length); + bos.close (); -System.out.println ("Binary serialization length:" +S.codec (). length); +     } A}

First create a generic class called student and serialize it, then test it.

The results are as follows:

JDK Serialization Length: 88
Binary serialization Length: 8

The conclusion is that the performance of serialization is lower than the binary encoding. It is obvious that network traffic consumes data and affects the throughput of the entire program.

Obviously we do not usually choose the Java serialization as a remote cross-borrowing point to call the coding framework, so how, in the study of the chant.

Learn every day, progress every day ... Apply

Java codec technology, Netty NiO

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.