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