serialization - Deserialisation issue - java.io.StreamCorruptedException: invalid type code: 00 -
I am writing a Java file-transfer app, and I have some problems, which are self-defined with distillation Message from Class Datagram
Stack overflow has problems similar to other topics, but I did not find anything useful from there, so I have already been forgiven if I miss it.
So, the class message is:
import java.io.Serializable; Public class message implementation serializable {Private Static Finals Long Serial Weerission UID = 1 L; Private int Segment ID; Private byte [] packet; Private Ent Bittsovrite; Public message () {segmentID = -1; } Public Message (Int Segment ID, Byte [] Packet, Int Bites Towright) {this.segmentID = segmentID; This.packet = packet; This.bytesToWrite = bytesToWrite; } Public int getBytesToWrite () {return bytesToWrite; } Get public intGraham () {Return Segment ID; } Public Byte [] getPacket () {Return Packet; }}
I do not have anything special serialize serialize (message obj) IOException by serial
public byte {bytereunputstream bytestream = new byteOnputstream () ; ObjectOutputStream Object Stream = New ObjectOutputStream (ByteStream); ObjectStream.flush (); ObjectStream.writeObject (obj); ObjectStream.flush (); Return byteStream.toByteArray (); }
And in fact, send it via Datagram
msg = new byte [512]; Int bytes read; BytesRead = fileReader.read (msg); // fileReader is FileInputStream Message Message = New Message (segmentID, msg, bytes read); Byte [] test = serialize (message); Datagram socket. Send (new datagram packet (test, test.labby, datagramasket.gate iNetAdder (), datagramasket.teport ()));
I get it from elsewhere
byte [] buffer = new byte [8192]; Datagram packet received packet = new datagram packet (buffer, buffer lamps); Socket.recieve (recievedPacket); // It is already linked to the init packet MSG = (message) deserialize (receivedPacket.getData (), received packet.gateoffset (), received Packet.getLength ());
Where (try / catch block has been removed here)
Personal object deserialize (byte [] bytes, int offset, int length {ByteArrayInputStream byteStream = New BytereinestSTream (Bytes, Offset, Length); Objectinputstream objectstream = new object intestream (new bufferInput speed (byte storm)); Object tmpMsg = new object (); TmpMsg = objectStream.readObject (); // Here EOFException returns tmpMsg; }
Why can this happen? I did it according to advocacy - where and what did I do? I checked that the initial message passes (I have, but this is a small piece of my code) and the obtained data is not zero. In addition, the datagrams sent and received figures are the same length. thank you in advanced.
get MSG = (message) deserialize (receivedPacket.getData ());
You get it
get MSG = (message) deserialize (receivedPacket.getData ()), received packet.gateoffset (), Received packet.
And adjust your 'deserialise ()' method accordingly, accept the 'offset' and 'length' parameters, and use them when creating a byte ininstream.
Edit The code you posted does not compile: The expression that produces the outgoing Datagram packet is incorrect. It should be
new datagram packet (test, test.lambi, datagram.ctt.getgetInetAdder (), datagram.netport) ())
I do not know packet overhead
is considered, but you do not need it.
NB You do not need to create a new message ()
line before you call readObject ().
Comments
Post a Comment