Using Serialization over piped streams I believe I can perform deep copy (cloning).
Is this correct? Here is a code snippet:
Twisting together piped streams in the same thread is prone to deadlock as per javadoc.
But that aside, "cloneObj" is a clone of "originalObj" in 100% everyway, right?
Should this be considered a valid technique in production code? If not, why not?
A senior developer informed me that the fathers of Java intended serialization to only be used to persist an object outside the jvm. Using the twisted pipes technique was wrong because it violates the "intended use" of serialization and he refused to discuss any further.
What says the Java Ranch? Serialization is deep copy. Clone is deep copy. I can serialize to perform cloning. right?
Masa Saito wrote:Serialization is deep copy. Clone is deep copy. I can serialize to perform cloning. right?
By default, no - clone() uses shallow copy. If you want deep copy, using serialization is worth considering. It you don't want to carefully modify the clone() method of each class accordingly, and you haven't yet determined that performance is poor and this is the primary bottleneck, then this is a good way to achieve your objectives, in my opinion.
As for your use of piped streams, it seems like this is an unrelated consideration. On one hand, you can choose between using clone() or using serialization. On another hand, completely independent, you can choose between writing to a piped stream, or a ByteArrayOutputStream, or a file, or something else. The first is a choice of what format to use; the latter is a choice of destination. The first is what to write, and the latter is where to write it. As far as I can see here, the two choices are independent and unrelated.