Java deep cloning

Source: Internet
Author: User
Tags object serialization
First:/*** clone the object ** @ author Jianhua. fengjh */@ suppresswarnings ("all") public class ucloneutil {/*** deep clone through serialization ** deep clone through JDK native Object serialization stream, although simple, however, the performance is very low. * @ Todo follow-up changes to recursive cloning to implement ** @ Param SRC * @ return * @ throws ioexception * @ throws classnotfoundexception */@ deprecated public static <t> T deepclone (T SRC) throws ioexception, classnotfoundexception {T = NULL; objectoutputstream OOS = NULL; objectinputstream OIS = NULL; try {javasbaos = new transform (); OOS = new objectoutputstream (baos); OOS. writeobject (SRC); OOS. FL Ush (); ois = new objectinputstream (New bytearrayinputstream (baos. tobytearray (); t = (t) Ois. readobject ();} finally {If (OIS! = NULL) {Ois. Close ();} If (OOS! = NULL) {OOS. close () ;}} return t ;}} second import COM. thoughtworks. xstream. xstream; @ suppresswarnings ("all") public class xcloneutil {Private Static xstream = new xstream (); public static <t> T deepclone (t) {return (t) xstream. fromxml (xstream. toxml (t) ;}} third public class fastcloneutil {public static <t> T deepclone (t src) throws ioexception, classnotfoundexception {T = NULL; objectin Putstream OIS = NULL; objectoutputstream OOS = NULL; try {fastbytearrayoutputstream FBOs = new fastbytearrayoutputstream (); OOS = new objectoutputstream (FBOs); OOS. writeobject (SRC); OOS. flush (); ois = new objectinputstream (FBOs. getinputstream (); t = (t) Ois. readobject ();} finally {If (OIS! = NULL) {Ois. Close ();} If (OOS! = NULL) {OOS. close () ;}} return t ;}} import Java. io. inputstream;/*** bytearrayinputstream implementation that does not synchronize methods. */public class fastbytearrayinputstream extends inputstream {/*** our byte buffer */protected byte [] Buf = NULL; /*** number of bytes that we can read from the buffer */protected int COUNT = 0;/*** number of bytes that have been read from the buffer */Protected int Pos = 0; Public fastbytearrayinputstream (byte [] Buf, int count) {This. buf = Buf; this. count = count;} public final int available () {return count-Pos;} public final int read () {return (Pos <count )? (BUF [POS ++] & 0xff):-1;} public final int read (byte [] B, int off, int Len) {If (Pos> = count) {return-1;} If (Pos + Len)> count) {Len = (count-Pos);} system. arraycopy (BUF, POs, B, off, Len); POS + = Len; return Len;} public final long SKIP (long n) {If (Pos + n)> count) {n = count-Pos;} If (n <0) {return 0;} POS + = N; return n ;}} import Java. io. inputstream; import Java. io. outputstream;/*** bytearrayoutputstream implementation that doesn't synchronize methods and doesn't copy the data on tobytearray (). */public class fastbytearrayoutputstream extends outputstream {/*** buffer and size */protected byte [] Buf = NULL; protected int size = 0; /*** constructs a stream with buffer capacity size 5 K */Public fastbytearrayoutputstream () {This (5*1024 );} /*** constructs a stream with the given initial size */Public fastbytearrayoutputstream (INT initsize) {This. size = 0; this. buf = new byte [initsize];}/*** ensures that we have a large enough buffer for the given size. */private void verifybuffersize (INT sz) {If (SZ> Buf. length) {byte [] Old = Buf; Buf = new byte [math. max (SZ, 2 * Buf. length)]; system. arraycopy (old, 0, Buf, 0, old. length); old = NULL;} public int getsize () {return size;}/*** returns the byte array containing the written data. note that this array will almost always be larger than the * Amount of data actually written. */Public byte [] getbytearray () {return Buf;} public final void write (byte B []) {verifybuffersize (size + B. length); system. arraycopy (B, 0, Buf, size, B. length); size + = B. length;} public final void write (byte B [], int off, int Len) {verifybuffersize (size + Len); system. arraycopy (B, off, Buf, size, Len); size + = Len;} public final void write (int B) {verifybuffersize (size + 1 ); buf [size ++] = (byte) B;} public void reset () {size = 0 ;} /*** returns a bytearrayinputstream for reading back the written data */Public inputstream getinputstream () {return New fastbytearrayinputstream (BUF, size) ;}} fourth import COM. rits. cloning. cloner; public class veryfastcloneutil {public static <t> T deepclone (t src) {cloner = new cloner (); Return cloner. deepclone (SRC );}}
 
The fastest is the fourth type. You can understand the technical code at a glance.

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.