Object, byte [], bytebuffer conversion.

Source: Internet
Author: User
Tags object serialization

New Version

Package CN. vicky. chapt13; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. io. serializable; import Java. NIO. bytebuffer; import Java. NIO. charset. charset;/*** Object serialization and deserialization (serialization object to byte [], bytebuffer, byte [] convert object ** @ author Vicky * @ email eclipser@163.com */public class byteutil {public static byte [] getbytes (serializable OBJ) throws ioexception {bytearrayoutputstream bout = new bytearrayoutputstream (); objectoutputstream out = new objectoutputstream (bout); out. writeobject (OBJ); out. flush (); byte [] bytes = bout. tobytearray (); bout. close (); out. close (); Return bytes;} public static int sizeof (serializable OBJ) throws ioexception {return getbytes (OBJ ). length;} public static object GetObject (byte [] bytes) throws ioexception, classnotfoundexception {bytearrayinputstream Bi = new bytearrayinputstream (bytes); objectinputstream OI = new objectinputstream (BI ); object OBJ = Oi. readobject (); bi. close (); Oi. close (); Return OBJ;} public static object GetObject (bytebuffer) throws classnotfoundexception, ioexception {inputstream input = new bytearrayinputstream (bytebuffer. array (); objectinputstream OI = new objectinputstream (input); object OBJ = Oi. readobject (); input. close (); Oi. close (); bytebuffer. clear (); Return OBJ;} public static bytebuffer getbytebuffer (serializable OBJ) throws ioexception {byte [] bytes = byteutil. getbytes (OBJ); bytebuffer buff = bytebuffer. wrap (bytes); Return Buff;} public static void main (string [] ARGs) throws ioexception, classnotfoundexception {system. out. println (byteutil. sizeof (New player1 (); system. out. println (byteutil. sizeof (New player2 (); system. out. println (byteutil. sizeof (New player3 (); system. out. println (byteutil. sizeof (New player4 (); system. out. println (byteutil. sizeof (New player5 (); system. out. println ("---------"); player5 P5 = new player5 (); system. out. println (byteutil. sizeof (P5); p5.id1 = 100000; p5.id2 = 200000; system. out. println (byteutil. sizeof (P5); p5.name = "ooxx"; system. out. println (byteutil. sizeof (P5); p5.name = "ooxxooxx"; system. out. println (byteutil. sizeof (P5); system. out. println ("---------"); byte [] bytes = byteutil. getbytes (P5); player5 p5_2 = (player5) byteutil. getObject (bytes); system. out. println (p5_2.id1); system. out. println (p5_2.id2); system. out. println (p5_2.name); system. out. println ("---------"); system. out. println (byteutil. sizeof (New player6 (); player6 P6 = new player6 (); system. out. println (byteutil. sizeof (P6); p6.id1 = 100000; p6.id2 = 200000; system. out. println (byteutil. sizeof (P6); p6.setname ("Vicky"); system. out. println (byteutil. sizeof (P6); p6.setname ("Chinese name"); system. out. println (byteutil. sizeof (P6); bytes = byteutil. getbytes (P6); player6 p6_2 = (player6) byteutil. getObject (bytes); system. out. println (p6_2.id1); system. out. println (p6_2.id2); system. out. println (p6_2.getname () ;}} class player1 implements serializable {int id1;} class player2 extends player1 {int Id2;} class player3 implements serializable {int id1; int Id2 ;} class player4 extends player3 {string name;} class player5 implements serializable {int id1; int Id2; string name;} class player6 implements serializable {final static charset chrarset = charset. forname ("UTF-8"); int id1; int Id2; private byte [] Name = new byte [20]; Public String getname () {return new string (name, chrarset);} public void setname (string name) {This. name = Name. getbytes (chrarset);} // public void setname (string name) {// byte [] tmpbytes = Name. getbytes (chrarset); // For (INT I = 0; I <tmpbytes. length; I ++) {// This. name [I] = tmpbytes [I]; //}

 

 

 

 

Old Version

Package CN. vicky. utils; </P> <p> Import Java. io. bytearrayinputstream; <br/> Import Java. io. bytearrayoutputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. objectinputstream; <br/> Import Java. io. objectoutputstream; <br/> Import Java. NIO. bytebuffer; </P> <p> Import Org. apache. mina. core. buffer. iobuffer; </P> <p>/** <br/> * Object serialization and deserialization (serialization object to byte [], bytebuffer, byte [] convert object <br/> * @ author Vicky <br/> * @ email eclipser@163.com <br/> */<br/> public class byteutil {</P> <p> Public static byte [] getbytes (Object OBJ) throws ioexception {<br/> bytearrayoutputstream bout = new bytearrayoutputstream (); <br/> objectoutputstream out = new objectoutputstream (bout); <br/> out. writeobject (OBJ); <br/> out. flush (); <br/> byte [] bytes = bout. tobytearray (); <br/> bout. close (); <br/> out. close (); <br/> return bytes; <br/>}</P> <p> Public static object GetObject (byte [] bytes) throws ioexception, classnotfoundexception {<br/> bytearrayinputstream Bi = new bytearrayinputstream (bytes); <br/> objectinputstream OI = new objectinputstream (BI); <br/> Object OBJ = Oi. readobject (); <br/> bi. close (); <br/> Oi. close (); <br/> return OBJ; <br/>}</P> <p> Public static object GetObject (bytebuffer) throws classnotfoundexception, ioexception {<br/> // iobuffer that requires the Mina framework <br/> iobuffer buffer = iobuffer. allocate (bytebuffer. capacity ()). setautoexpand (true); // automatically expand <br/> for (INT I = 0; I <bytebuffer. capacity (); I ++) {<br/> bytebuffer. position (I); <br/> buffer. put (bytebuffer. get (); <br/>}< br/> buffer. position (0); <br/> inputstream input = buffer. asinputstream (); <br/> objectinputstream OI = new objectinputstream (input); <br/> Object OBJ = Oi. readobject (); <br/> input. close (); <br/> Oi. close (); <br/> return OBJ; <br/>}</P> <p> Public static bytebuffer getbytebuffer (Object OBJ) throws ioexception {<br/> byte [] bytes = byteutil. getbytes (OBJ); <br/> bytebuffer buff = bytebuffer. wrap (bytes); <br/> return Buff; <br/>}</P> <p >}< br/>

 

The preceding Code implements conversion between Java and data transmission and conversion between Java and ActionScript. The main idea is not discussed above...

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.