Hi Les,
I have read Josh Bloch's stuff and he is usually a good source. I don't
have this book, but I did run a simple test regarding this scenario. The
class basically serializes itself and reports the serialized size.
If out.defaultWriteObject() is in the code, then the size is 2079.
If out.defaultWriteObject() is commented out, then the size is 1056.
The class has a byte [1000] field which accounts for most of the size.
I think this demonstrates that out.defaultWriteObject() is causing a field
to be serialized twice. In Shiro, it makes the SimpleSession serialized
data twice as big and twice as slow as necessary.
Best,
Dan
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class TestSerialization implements Serializable
{
private final byte [] data = new byte[1000];
public static void main(String[] args)
{
TestSerialization ts = new TestSerialization();
try
{
System.out.println("Size=" + getSerializedSize(ts));
}
catch (IOException e) { }
}
private void writeObject(ObjectOutputStream out) throws IOException
{
out.defaultWriteObject();
out.write(data);
}
public static int getSerializedSize(Object obj) throws IOException {
// serialize the object..
final ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
int size = 0;
try
{
oos = new ObjectOutputStream(out);
oos.writeObject(obj);
final byte[] seralized = out.toByteArray();
size = seralized.length;
} finally {
try
{
if(oos != null) {
oos.close();
}
out.close();
} catch (IOException e) { }
}
return size;
}
}
--
View this message in context:
http://shiro-user.582556.n2.nabble.com/SimpleSession-serialization-tp6826037p6832665.html
Sent from the Shiro User mailing list archive at Nabble.com.