反序列化问题 - java.io.StreamCorruptedException:类型代码无效:00 [英] Deserialisation issue - java.io.StreamCorruptedException: invalid type code: 00

查看:212
本文介绍了反序列化问题 - java.io.StreamCorruptedException:类型代码无效:00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个java文件传输应用程序,我有一些麻烦来反序列化我自己定义的类来自Datagramms的消息。

I'm writing a java file-transfer app, and i have some troubles with deserialisation myself-defined class Message from Datagramms.

StackOverflow的其他主题有类似的问题,但我没有从那里找到有用的东西,所以如果我错过了,我很抱歉。

Other topics at StackOverflow has similar issues, but i didn't found something helpful from there, so i'm sorry in advance if i missed it.

所以,课程信息是:

import java.io.Serializable;

public class Message implements Serializable {

    private static final long serialVersionUID = 1L;

    private int segmentID;
    private byte[] packet;
    private int bytesToWrite;

    public Message(){
        segmentID = -1;
    }

    public Message(int segmentID, byte[] packet, int bytesToWrite) {
        this.segmentID = segmentID;
        this.packet = packet;
        this.bytesToWrite = bytesToWrite;
    }

    public int getBytesToWrite() {
        return bytesToWrite;
    }

    public int getSegmentID() {
        return segmentID;
    }

    public byte[] getPacket() {
        return packet;
    }
}

没什么特别的。
i序列化

nothing special. i serialise it with

public byte[] serialize(Message obj) throws IOException
{
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
    objectStream.flush();
    objectStream.writeObject(obj);
    objectStream.flush();
    return byteStream.toByteArray();
}

实际上,通过数据报发送

and, actually, send it through datagram

msg = new byte[512];
int bytesRead;
bytesRead = fileReader.read(msg);//fileReader is FileInputStream
Message message = new Message(segmentID, msg, bytesRead);
byte[] test = serialize(message);
datagramSocket.send(new DatagramPacket(test, test.length, 
    datagramSocket.getInetAddress(), datagramSocket.getPort()));

我从其他地方收到

byte[] buffer = new byte[8192];
DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length);
socket.recieve(recievedPacket); //it's already connected by init packet
receiveMSG = (Message) deserialize(receivedPacket.getData(), 
    receivedPacket.getOffset(), receivedPacket.getLength());

其中(此处删除try / catch块)

where (try/catch blocks are deleted here)

private Object deserialize(byte[] bytes, int offset, int length)
{
    ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes, offset, length);
    ObjectInputStream objectStream = new ObjectInputStream(new BufferedInputStream(byteStream));
    Object tmpMsg = new Object();
        tmpMsg = objectStream.readObject();//here catches the EOFException
    return tmpMsg;
}

为什么会这样?我按照 http://goo.gl/Rq1rMm 上的说明进行了操作。

where and what我做错了?
我检查了初始消息是否通过(我有它们,但这里只是我的代码的一小部分)并且接收中的数据报不为空。而且,发送和接收的数据报具有相同的长度。
提前致谢。

why it can be? i did it according instructions at http://goo.gl/Rq1rMm
where and what i did wrong? i checked that initial messages passes (i have them, but here is only a little piece of my code) and datagram in receive is not null. Moreover, sent and received datagrams has equal length. Thanks in advance.

推荐答案


receiveMSG =(Message)deserialize(receivedPacket.getData ());

receiveMSG = (Message) deserialize(receivedPacket.getData());

您需要将此更改为

receiveMSG = (Message) deserialize(receivedPacket.getData(), receivedPacket.getOffset(), receivedPacket.getLength);

并相应调整'deserialise()'方法,接受'offset'和'length'参数,并在构造ByteArrayInputStream时使用它们。

and adjust your 'deserialise()' method accordingly, to accept 'offset' and 'length' parameters, and to use them when constructing the ByteArrayInputStream.

编辑您发布的代码无法编译:构造传出数据报包的表达式是不正确的。它应该是

EDIT The code you posted doesn't compile: the expression that constructs the outgoing datagram packet is incorrect. It should be

new DatagramPacket(test, test.length, datagramSocket.getInetAddress(), datagramSocket.getPort())

我不知道 packetOverhead 应该是什么,但你不需要它。

I have no idea what packetOverhead is supposed to be, but you don't need it.

注意你不需要创建新消息()调用 readObject()之前的行。

NB You don't need to create a new Message() the line before you call readObject().

这篇关于反序列化问题 - java.io.StreamCorruptedException:类型代码无效:00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆