views:

199

answers:

8

i want to cast byte[] which i got from socket a class which I write it (Message). I tried

byte[] data=new btyte[100];
.
.
.
Message m=(Message)data;

but it did not work and give me error what should I do?

+2  A: 

You're probably better off writing a function to do it.

Message parseDataFromSocket(byte[] bytes);
Seth
The implementation of this function whould be a better answer.
Fortega
No, it wouldn’t because the OP has never specified in which format the data is stored—any answer would be quite wrong unless Seth can read minds or guesses correctly.
Bombe
actually this is a good answer since you can hide how the "parse" works - if could be really parsing or it could be unpickling a serialized object... best thing is that the user of the method would not really care how it gets done, just that it gets done.
TofuBeer
+1  A: 

Try using an ObjectInputStream with a ByteArrayInputStream.

It looks like your trying to read a serialized class.

Edited to ByteArrayInputStream as mention above.

Gordon
+1  A: 

You need to create a Message constructor that takes a byte[] (and uses it to somehow initialise the object). You can then do:

Message m = new Message(data);
William
+15  A: 

Assuming you are talking about a serialized object:

try this:

ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
Message message = (Message) in.readObject();
in.close();

In case you are serializing the Message in some other way, you will have to parse it yourself, using a parametrized constructor or a static method that returns a new instance of the type.

Yannick M.
+1  A: 

If the byte array was serialized normally, you can use a ByteArrayInputStream, pass that into an ObjectInputStream and cast the resulting Object into your Message class.

MarkPowell
+1  A: 

It depends on how the byte array was created... You could try:

ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data))
Message m = (Message) in.readObject();
Fortega
+4  A: 

You cannot cast arbitrary binary data (in Java).

What's that byte[] supposed to contain? If it's a serialized instance of the class, you'll have to wrap the socket's InputStream in an ObjectInputStream. If it's aother well-defined binary format, you'll have to parse it manually (Note: dumping C structs does not constitute a well-defined binary format and should be avoided).

Michael Borgwardt
+2  A: 

In Java it is not possible to perform such casts (in C++, for example, one can define conversion constructors and the likes).

You've tagged the question with 'serialization'. Assuming you are using the Java serialization API, then as you were already directed, you should feed the data array through an ObjectInputStream in order for it to deserialize the array into an object. More information can be found here.

Furthermore, in case you've used some other sort of serialization (for example, using XStream to serialize to XML) then you should deserialize (unmarshalling is another common name for this) the data using the same API that was used to serialize it in the first place.

abyx