Question:
the Java class Class implements Serializable. What are the contents of the serialized form?
?
2011-08-09 10:25:57 UTC
I want to store references to a Java class is a datastore. The datastore supports serialized objects as attributes. How much data do I use if I serialize the Class object, as opposed to, say, only a fully qualified name?
Three answers:
McFate
2011-08-09 11:31:00 UTC
java.lang.Object.class is 37 bytes in serialized form. That's about twice as long as the String "java.lang.Object" and it is also binary crackers in your datastore (which means it might be harder to review manually if there are data issues).



Here's a program that will let you test serialization in both compressed and uncompressed form (compressed tends to only "win" for relatively larger things):



=================================

package ya;



import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import java.util.zip.GZIPOutputStream;



public class YA110809 {

public static void main(String[] args) {

try {

Class c = Object.class;

byte[] b = serializeObjectToBytes(c, false);

System.out.println("uncompressed = " + b.length);

b = serializeObjectToBytes(c, true);

System.out.println("compressed = " + b.length);

} catch (Throwable th) {

th.printStackTrace();

}

}



static byte[] serializeObjectToBytes(Object anObject, boolean aCompress)

throws IOException {

ByteArrayOutputStream bos = new ByteArrayOutputStream(4096);

ObjectOutputStream oos;

OutputStream os;



if (aCompress) {

os = new GZIPOutputStream(bos);

} else {

os = bos;

}



oos = new ObjectOutputStream(os);

oos.writeObject(anObject);

oos.flush();

oos.close();



if (aCompress) {

os.flush();

os.close();

}

return bos.toByteArray();

}

}

=================================
deonejuan
2011-08-09 11:31:52 UTC
Serialization, for practical purposes, is Object Streams.



The standard classes of Java, but not all, they have add the obj.equals(), obj.hashcode(). A Custom class you have to write those. (see first link). I mentioned that because I don't know if we have to cast with the wildcard to start using the Object.



You say 'store references'. Serialization doesn't do that. You do however deserialize to a reference. Therefore, If we deserialize once to varA, and we do it again to varB, we have two references to the same Object. If we have two Streams that did varA and again varB, we have two references to two Objects.
?
2016-10-18 04:18:12 UTC
Serializable is definitely an interface. that is an unusual interface in that it has no individuals, so which you assert "implements Serializable" yet do no longer then could do any further artwork. the objective is to tell the article serialization code that gadgets of this classification shoud be serialized. gadgets no longer marked as Serializable are no longer serialized. So whilst an merchandise of sophistication A is serialized, if it has 2 individuals b and c of sophistication B and C, yet merely B is serializable, merely the member b would be serialized.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...