Often Serialization and Deserialization is considered the most frequently asked questions in Java. In Java8, Serialization & Deserialization are treated no differently. The only difference you will notice is the gravity of complex questions asked in the interview Process. Specially if you are interviewing for a company that deals with massive amounts of data transfer over the wire AND OFCourse, uses java.
Have a look at the following Serialization/ Deserialization code that is supposed to be working but freaked out because of the set being invariant. In case you can not figure out what is the problem, stay tuned for the correct solution.
Have a look at the following Serialization/ Deserialization code that is supposed to be working but freaked out because of the set being invariant. In case you can not figure out what is the problem, stay tuned for the correct solution.
package com.java8.examples; import java.io.*; import java.util.HashSet; import java.util.Set; /** * Created by sonis on 6/21/14. */ public class SerialKiller { public static void main(String[] args) throws Exception{ SerialObjectsClassType obj1 = new SerialObjectsClassType(111); obj1.checkIfSerialSetInvariant(); /** * lets make a serialized copy of the invariants */ try { SerialObjectsClassType serialCopy_of_Obj1 = (SerialObjectsClassType) makeSerialCopy(obj1); serialCopy_of_Obj1.checkIfSerialSetInvariant(); } catch (Exception e) { e.printStackTrace(); } obj1.checkIfSerialSetInvariant(); } // Making a generic function that copies its arguments via Serialization public static Object makeSerialCopy(Object obj) throws IOException, ClassNotFoundException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); new ObjectOutputStream(byteArrayOutputStream).writeObject(obj); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); return new ObjectInputStream(byteArrayInputStream).readObject(); } } /* A class for keeping sets of Seralized objects */ class SerialSet implements Serializable { final Setserialized_Object_Sets = new HashSet (); } final class SerialObjectsClassType extends SerialSet { private int id; public SerialObjectsClassType(int id) { this.id = id; serialized_Object_Sets.add(this); } @Override public boolean equals(Object obj) { return (obj instanceof SerialObjectsClassType) && (id == ((SerialObjectsClassType) obj).id); } @Override public int hashCode() { return id; } public void checkIfSerialSetInvariant() throws Exception{ if (!serialized_Object_Sets.contains(this)) throw new AssertionError("Serial set is not invariant. Recheck Serialization. SERIAL KILLER !!!"); } }
The system fails with the following error: java.lang.Exception: Serial set is not invariant. Recheck Serialization. SERIAL KILLER !!! at com.java8.examples.SerialObjectsClassType.checkIfSerialSetInvariant(SerialKiller.java:77) at com.java8.examples.SerialKiller.main(SerialKiller.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
No comments:
Post a Comment