Serializing/deserializing a SIPDIalog - java

I was trying to serialize and deserialize a gov.nist.javax.sip.stack.SIPDialog object into Cassandra. But the equals comparison on the deserialized object fails when I compare it with the original SIPDialog object I serialized. SO looks like I am missing something here in serialisation. I am using a ByteArraySerializer to read/write the bytes into Cassandra.
//Saving Dialog
MutationBatch mutationBatch = createMutator();
byte[] dialogBytes = SIPDialogEntity.serializeDialog(dialog);
mutationBatch.withRow(SIPDIALOGS, dialogId)
.putColumn("dialog".getBytes(),dialogBytes,null);
mutationBatch.execute();
public static byte[] serializeDialog(SIPDialog dialog) throws IOException {
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bStream);
oos.writeObject(dialog);
oos.close();
byte[] bytes = bStream.toByteArray();
bStream.close();
return bytes;
}
//Reading Dialog
Column<byte[]> result;
result = getKeySpace().prepareQuery(SIPDIALOGS).getKey(dialogId).getColumn("dialog").execute().getResult();
sipDialog = SIPDialogEntity.deserializeDialog(result.getByteArrayValue());
public static SIPDialog deserializeDialog(byte[] byteArrayDialog) throws IOException, ClassNotFoundException {
System.out.println("DEBUG Reading Dialog Bytes:" + byteArrayDialog );
ByteArrayInputStream bStream = new ByteArrayInputStream(byteArrayDialog);
ObjectInputStream ois = new ObjectInputStream(bStream);
SIPDialog dialog = (SIPDialog) ois.readObject();
ois.close();
bStream.close();
return dialog;
}

The SIPDialog class doesn't override the equals method which is why it fails the comparison. Please open an issue in jain sip at http://java.net/jira/browse/JSIP

hmmmm, If SipDialog is your class, you could just skip all the work and use PlayOrm for cassandra ;). Then you don't need to deal with serializing/deserializing.
If it is not your class, I think I will get them to add a way to add 3rd party beans to be converted to an entity much like Guice does in a binding file so it can bind to an entity that can be saved by PlayOrm. IF you open a ticket on PlayOrm with a request, we could get the feature in probably in as little as 1 week.

Related

passing a serialized object

In xmlrpc objects need to be serialized before they can be transmitted across a network, so this is what I am trying to do.
addAuthorName = txtAddAuthorName.getText();
int addArticleNumber = Integer.parseInt(txtAddArticleNumber.getText());
newArticle = new Article(addAuthorName, addArticleNumber);
ObjectOutputStream oos;
oos = new ObjectOutputStream(
new ByteArrayOutputStream());
oos.writeObject(newArticle);
Vector<Object> addArticleArglist = new Vector<Object>();
addArticleArglist.addElement(oos);
System.out.println(oos);
// make the call
String callit = ("GetSize.addHash");
articleID = (Integer) client.execute(callit, addArticleArglist);
The problem I am getting is that my program will not accept the outputstream that is contained in the vector the error given is
unsupported Java type: class java.io.ObjectOutputStream
You can only serialize objects that implement the java.io.Serializable interface. Below statement adds ObjectOutputStream to the Vector which doesn't implement that interface.
addArticleArglist.addElement(oos);
I doesn't see any reason why you have to add that to Vector.
The serialization happens automatically. You don't need to undertake further action to serialize the object—just use it as a param, and it will automatically be serialized.

How to get a byte[] object from another class object in J2ME?

I know that in J2ME I can get a byte[] object from a String object by using the getBytes() method. My question is : is it possible to get a byte[] object from any other class type ? In addition : is it possible to get a byte[] object from a user-defined class object ?
Is it possible to get a byte[] object from any other class type ?
Some classes may implement a simular service.
Is it possible to get a byte[] object from a user-defined class object ?
Not without you writing the conversion yourself.
Example how to do it yourself (just note that the DataOutputStream handles the conversion, for example which byte order that is used):
ByteArrayOutputStream out = new ByteArrayOutputStream();
{
// conversion from "yourObject" to byte[]
DataOutputStream dos = new DataOuputStream(out);
dos.writeInt(yourObject.intProperty);
dos.writeByte(yourObject.byteProperty);
dos.writeFloat(yourObject.floatProperty);
dos.writeChars(yourObject.stringProperty);
dos.close();
}
byte[] byteArray = out.toByteArray();

Hessian deserializes java.lang.Character as a String

I've used Hessian for a while but just noticed the following behavior. If you serialize a java.lang.Character in Hessian, it deserializes as a String.
public class TestHessianChar {
public static void main(String... args) throws IOException {
// Serialize
Character c = new Character('x');
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Hessian2Output hessian2Output = new Hessian2Output( byteArrayOutputStream );
hessian2Output.startMessage();
hessian2Output.writeObject(c);
hessian2Output.completeMessage();
hessian2Output.close();
byte[] dataBytes = byteArrayOutputStream.toByteArray();
// Deserialize
Hessian2Input hessian2Input = new Hessian2Input( new ByteArrayInputStream( dataBytes ) );
hessian2Input.startMessage();
Object o = hessian2Input.readObject();
hessian2Input.completeMessage();
hessian2Input.close();
System.out.println(o.getClass().getName());
}
}
Output for this code is:
java.lang.String
I'm assuming it has something to do with language-independent serialization of primitives, but it's rather annoying. I am writing a JMS driver and need to distinguish between char and String, since the spec demands different behavior. I'm considering writing my own class to represent a char (and give up auto-boxing), but I wanted to know if there is a proper way to get Hessian to treat a Character as a Character before I start going through such contortions.

How do I Serialize Object to Database for Hibernate to read in Java

I'm currently writing a tool to plug into an existing enterprise application that uses Hibernate. My tool at install time needs to write some values into the database where one of the columns is a serialized version of a setting descriptor object. This object has two lists of objects and a few primitive types.
My current approach is to create a ByteArrayOutputStream and an ObjectOutputStream and then write the ObjectOutputStream to the ByteArrayOutputStream, then passing the resulting byte array into the sql with Spring's 1SimpleJdbcTemplate1. My current issue with this approach is that when the enterprise tool pulls my rows it fails to de-serialze the column with the following:
org.springframework.orm.hibernate3.HibernateSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
I feel I may need to serialize the inner objects, but have no clue how to do that and keep everything together.
Ended up solving my own problem. In the hibernate API there is a class called SerializationHelper that has a static function serialize(Serializable obj) which I was able to use to serialize my object and then insert it into the database. Hibernate was then able to read it in the enterprise app.
You can serealize a Java object into bytes and then store it in a BLOB.
Serialize:
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
objOut.writeObject(object);
objOut.close();
byteOut.close();
byte[] bytes = byteOut.toByteArray()
Deserialize:
public <T extends Serializable> T getObject(Class<T> type) throws IOException, ClassNotFoundException{
if(bytes == null){
return null;
}
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
ObjectInputStream in = new ObjectInputStream(byteIn);
T obj = (T) in.readObject();
in.close();
return obj;
}

java serialized messages dont contain the correct data in socket-client-server model

hello i have a basic client-server system running using java sockets.
my problem is, that an object that i send from the client to the server does not contain the correct data after it has been sent once.
the first time i send it, it arrives with the correct values, but when i send it another time with different values, it still arrives at the server with the same values as the first time. it also happens if i send a completely different instance of that class. it always arrives with the data, which have been sent the very first time.
when i try this with other objects like java.lang.String it seems to work.
the problematic class looks like this:
public class Vector3f implements Serializable {
private static final long serialVersionUID = 2838034155614698213L;
public float x, y, z;
}
i use objectinputstream and objectoutputstream on both the server and the client to send and receive objects.
let me know, if you need any more information about the system.
thanks!
My guess is that you're changing the values of the fields and then retransmitting the same object. The ObjectOutputStream will notice that it's already sent the original object, and just send a reference the second time.
You could avoid this by calling reset() on the ObjectOutputStream - but I'd be tempted to just use separate instances anyway, possibly even making the class immutable. (Public mutable fields are almost never a good idea.)
The best way in case of serialization you should convert the object into a byte array object and then write into the socket.
// Serialize to a file
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
out.writeObject(object);
out.close();
// Serialize to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(object);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
// Deserialize from a file
File file = new File("filename.ser");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
// Deserialize the object
Object obj = (Object) in.readObject();
in.close();
// Get some byte array data
byte[] bytes = getBytesFromFile(file);
// see Reading a File into a Byte Array for the implementation of this method
// Deserialize from a byte array
in = new ObjectInputStream(new ByteArrayInputStream(bytes));
in.close();

Categories

Resources