Reputation: 2121
In my implementation, I am using Java serialization to send across a set of binary objects starting with a notification header which tells how many objects are going to be passed and what their respective sizes are. Just to start with simple test, I was able to transfer the header object successfully, and after that I am trying to send just one 1024 bytes data object. However after running the test, I observe following:
InvalidClassException
. Can someone please provide some pointers as to what is this issue and how to fix this problem?
The receiver thread run() method looks like this.
public void run() {
while (mRunning) {
try {
// Receive the serialized object
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.println("SERVER: data received: ");
// Display the serialized object.
byte[] data = packet.getData();
System.out.println(Arrays.toString(data));
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
try {
Object obj = is.readObject();
if (obj instanceof DatakNotification) {
System.out.println("Data notification received: ");
} else if (obj instanceof DataChunk) {
System.out.println("Data Chunk received: ");
}
System.out.println(obj);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
mRunning = false;
}
}
socket.close();
}
In the logs, I see as follows.
First object (header)
SERVER: data received:
[-84, -19, 0, 5, 115, 114, 0, 47, 99, 111, 109, 46, 99, 111, 110, 97, 46, 118, 105, 100, 101, 111, 99, 108, 105, 101, 11
0, 116, 46, 99, 104, 117, 110, 107, 103, 101, 110, 46, 66, 108, 111, 99, 107, 78, 111, 116, 105, 102, 105, 99, 97, 116,
105, 111, 110, -41, 62, 52, -41, -16, 78, 119, 0, 2, 0, 5, 73, 0, 10, 109, 66, 108, 111, 99, 107, 83, 105, 122, 101, 73,
0, 10, 109, 67, 104, 117, 110, 107, 83, 105, 122, 101, 73, 0, 14, 109, 76, 97, 115, 116, 67, 104, 117, 110, 107, 83, 10
5, 122, 101, 73, 0, 12, 109, 84, 111, 116, 97, 108, 67, 104, 117, 110, 107, 115, 76, 0, 8, 109, 66, 108, 111, 99, 107, 7
3, 100, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 120, 112, 0, 0, 10
1, 48, 0, 0, 4, 0, 0, 0, 1, 48, 0, 0, 0, 26, 116, 0, 36, 97, 53, 100, 48, 51, 54, 52, 55, 45, 54, 52, 51, 52, 45, 52, 55
, 98, 100, 45, 97, 101, 55, 51, 45, 55, 102, 57, 53, 49, 48, 52, 102, 55, 49, 98, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Block notification received: Block Id: a5d03647-6434-47bd-ae73-7f95104f71b8Block size: 25904 Chunk size: 1024 Last chunk size: 304 Total chunks: 26
Second object (1K data)
SERVER: data received: [-84, -19, 0, 5, 115, 114, 0, 39, 99, 111, 109, 46, 99, 111, 110, 97, 46, 118, 105, 100, 101, 111, 99, 108, 105, 101, 11 0, 116, 46, 99, 104, 117, 110, 107, 103, 101, 110, 46, 68, 97, 116, 97, 67, 104, 117, 110, 107, -95, -44, 27, 37, -39, - 34, 12, 17, 2, 0, 5, 73, 0, 7, 109, 76, 101, 110, 103, 116, 104, 90, 0, 7, 109, 77, 97, 114, 107, 101, 100, 73, 0, 12, 1 09, 83, 101, 113, 117, 101, 110, 99, 101, 78, 117, 109, 91, 0, 4, 109, 66, 117, 102, 116, 0, 2, 91, 66, 76, 0, 8, 109, 6 7, 104, 117, 110, 107, 73, 100, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103 , 59, 120, 112, 0, 0, 4, 0, 0, 0, 0, 0, 0, 117, 114, 0, 2, 91, 66, -84, -13, 23, -8, 6, 8, 84, -32, 2, 0, 0, 120, 112, 0 , 0, 4, 0, 70, 76, 86, 1, 1, 0, 0, 0, 9, 0, 0, 0, 0, 18, 0, 0, -73, 0, 0, 0, 0, 0, 0, 0, 2, 0, 10, 111, 110, 77, 101, 11 6, 97, 68, 97, 116, 97, 8, 0, 0, 0, 8, 0, 8, 100, 117, 114, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 119 <.....> -124, -7, -22, -22, -62, 86, 12, 1, 45, 29, -126, -128, 24, 6, -48, 96, 3, -53, -15, -95, -24, 48, 31, 1, 10, -4, 24, 7 9, -63, 41, 68, 6]
java.io.InvalidClassException: com.dataxfer.chunkgen.DataChunk; local class incompatible: stream classdesc serialVersionUID = -6785768889157546991, local class serialVersionUID = 2835159210520537273
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:617)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at com.dataxfer.transceiver.LocalUdpReceiver.run(LocalUdpReceiver.java:62)
Upvotes: 0
Views: 136
Reputation: 533530
It means the serialVersionUID
of your writer and reader don't match. This is because the code for this class on the writer and reader are different.
It means that the serialVersionUID of the read needs to be hardcoded to match the writer.
Try setting
private static final long serialVersionUID = -6785768889157546991L;
This will only work if you haven't changed the types of any field, or removed any.
Upvotes: 2