package org.jboss.invocation;
import java.io.DataOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Arrays;
public class MarshalledValue
implements java.io.Externalizable
{
private static final long serialVersionUID = -1527598981234110311L;
private byte[] serializedForm;
private int hashCode;
public MarshalledValue()
{
super();
}
public MarshalledValue(Object obj) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MarshalledValueOutputStream mvos = new MarshalledValueOutputStream(baos);
mvos.writeObject(obj);
mvos.flush();
serializedForm = baos.toByteArray();
mvos.close();
int hash = 0;
for (int i = 0; i < serializedForm.length; i++)
{
hash = 31 * hash + serializedForm[i];
}
hashCode = hash;
}
public Object get() throws IOException, ClassNotFoundException
{
if (serializedForm == null)
return null;
ByteArrayInputStream bais = new ByteArrayInputStream(serializedForm);
MarshalledValueInputStream mvis = new MarshalledValueInputStream(bais);
Object retValue = mvis.readObject();
mvis.close();
return retValue;
}
public byte[] toByteArray()
{
return serializedForm;
}
public int size()
{
int size = serializedForm != null ? serializedForm.length : 0;
return size;
}
public int hashCode()
{
return hashCode;
}
public boolean equals(Object obj)
{
if( this == obj )
return true;
boolean equals = false;
if( obj instanceof MarshalledValue )
{
MarshalledValue mv = (MarshalledValue) obj;
if( serializedForm == mv.serializedForm )
{
equals = true;
}
else
{
equals = Arrays.equals(serializedForm, mv.serializedForm);
}
}
return equals;
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
int length = in.readInt();
serializedForm = null;
if( length > 0 )
{
serializedForm = new byte[length];
in.readFully(serializedForm);
}
hashCode = in.readInt();
}
public void writeExternal(ObjectOutput out) throws IOException
{
int length = serializedForm != null ? serializedForm.length : 0;
out.writeInt(length);
if( length > 0 )
{
out.write(serializedForm);
}
out.writeInt(hashCode);
}
}