StreamCorruptedException发送通过蓝牙序列化对象时, [英] StreamCorruptedException when sending Serialized objects via Bluetooth

查看:141
本文介绍了StreamCorruptedException发送通过蓝牙序列化对象时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要通过蓝牙发送为对象的序列化类,也实现Runnable。因此,我首先设置几个变量,然后将其发送以另一个Android设备,这将然后设置其结果到一个变量被执行对象,并返回相同的对象,其结果的变量之一。我一直在使用以下两种方法序列化我的对象,并通过我的BluetoothSocket的OutputStream中把他们之前得到一个字节数组,并反序列化的ByteArray找回我的对象​​。

I have a Serialized class that I need to send as an object via Bluetooth, and also implements Runnable. As such, I set a few variables first, then send it as an object to be executed by another Android device, which will then set its result into a variable, and send back the same object with the result in one of the variables. I have been using the following two methods to serialize my object and get a ByteArray before sending them through the OutputStream of my BluetoothSocket, and to deserialize that ByteArray to get back my object.

public static byte[] serializeObject(Object o) throws IOException { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    ObjectOutput out = new ObjectOutputStream(bos);
    out.writeObject(o);  
    out.flush();
    out.close();

    // Get the bytes of the serialized object 
    byte[] buf = bos.toByteArray(); 

    return buf; 
} 

public static Object deserializeObject(byte[] b) throws OptionalDataException, ClassNotFoundException, IOException { 
    ByteArrayInputStream bis = new ByteArrayInputStream(b);

    ObjectInputStream in = new ObjectInputStream(bis); 
    Object object = in.readObject(); 
    in.close(); 

    return object; 
}

我还接到了这两种方法同样的错误,所以我试图用我用通过的BluetoothSocket的OutputStream的送我的ByteArray方法进行合并,如下图所示。

I still got the same error from these two methods, so I tried to merge it with the methods I use to send my ByteArray via BluetoothSocket's OutputStream, as shown below.

public void sendObject(Object obj) throws IOException{
    ByteArrayOutputStream bos = new ByteArrayOutputStream() ; 
    ObjectOutputStream out = new ObjectOutputStream( bos );
    out.writeObject(obj); 
    out.flush();
    out.close();

    byte[] buf = bos.toByteArray();
    sendByteArray(buf);
}
public void sendByteArray(byte[] buffer, int offset, int count) throws IOException{
    bluetoothSocket.getOutputStream().write(buffer, offset, count);
}

public Object getObject() throws IOException, ClassNotFoundException{
    byte[] buffer = new byte[10240];
    Object obj = null;
    if(bluetoothSocket.getInputStream().available() > 0){
        bluetoothSocket.getInputStream().read(buffer);

        ByteArrayInputStream b = new ByteArrayInputStream(buffer);
        ObjectInputStream o = new ObjectInputStream(b);
        obj = o.readObject();
    }
    return obj;
}

在最后,我在接收装置反序列化对象时,得到了同样的错误,如下图所示。

In the end, I get the same error when deserializing the object at the receiving device, as shown below.

java.io.StreamCorruptedException
at java.io.ObjectInputStream.readStreamHeader (ObjectInputStream.java:2102)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
and so on...

任何人都可以请帮助?我绝望了,这已经杀了我好几个星期,我需要这在几天的工作。 :•

Can anybody please help? I'm desperate, this has been killing me for weeks, and I need this to work in a few days. :S

推荐答案

据的这个和<一href="http://stackoverflow.com/questions/2393179/streamcorruptedexception-invalid-type-$c$c-ac">this主题:

您应该使用单独的的ObjectOutputStream 的ObjectInputStream 套接字的生活,并且不使用任何其他流的插座中。

You should use single ObjectOutputStream and ObjectInputStream for the life of the socket, and don't use any other streams on the socket.

其次,使用 ObjectOutputStream.reset()来清除previous值。

Secondly, use ObjectOutputStream.reset() to clear previous values.

让我知道如果这个作品!

Let me know if this works !

这篇关于StreamCorruptedException发送通过蓝牙序列化对象时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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