将对象反序列化为类对象

时间:2014-02-10 01:38:34

标签: java networking serialization udp

我正在使用令牌环网络实现,我正在尝试使用DatagramPacket将令牌帧从站传递到环。我使用这个将帧对象转换为字节数组:

public static byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(obj);
    return b.toByteArray();
}

然后使用它将其转换回来:

public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream b = new ByteArrayInputStream(bytes);
    ObjectInputStream o = new ObjectInputStream(b);
    return o.readObject();
}

每个电台都运行两个线程。

public void go() {

t.println("Sending to: " + main_host_address);
t.println("Station Address: " + station_address);
t.println("MAC Address: " + MAC_Address);


DatagramSocket socket;
InetSocketAddress saddr;

try {
        saddr= new InetSocketAddress("localhost", main_host_address);
        socket= new DatagramSocket(station_address); // Create a socket and a datagram from the buffer data

        (new Thread(new receivingThread(socket))).start();  // start threads for receiving and sending
        (new Thread(new sendingThread(socket, saddr, frame))).start();
    }catch(Exception e) {
        e.printStackTrace();
}       
}

..他们都从一个地址收到 - 戒指。

现在在Ring类中,我正在尝试转换字节数组但它没有做任何事情,我正在运行一个while循环,但它一旦尝试转换字节数组就不会循环到Frame对象。

 public void run() {
        byte [] buffer;
        DatagramPacket packet;
        //Receive the packet
        try{
            buffer = new byte[5];
            int i = 0;
            packet = new DatagramPacket(buffer, buffer.length);
            int recievedData = 0;

            while(true){
                t.println("Recieving....");
                socket.receive(packet);

                Thread.sleep(2000);

                recievedData = packet.getData()[0];
                t.println("Reciever: " + recievedData);

                //here I'm trying a different way of getting the byte array
                //Before, I had; byte [] data = packet.getData(); 
                //but it didnt work so i was trying this longer way
                //and still the same result.
                List<Byte> list = new ArrayList<Byte>();

                for(i = 0; i < packet.getData().length; i++){
                    t.print(packet.getData()[i] + " ");
                    list.add(packet.getData()[i]);
                }

                Byte [] d = list.toArray(new Byte[list.size()]);
                byte[] data = new byte[d.length];

                int j=0;
                for(Byte b: d){
                    data[j++] = b.byteValue();
                }
                frame = (Frame) deserialize(data); //I think the problem is here
                t.println("From frame: " + frame.SD[0]);
            }
        }catch(Exception e){  

        }
    }

我该如何解决这个问题?谢谢。

1 个答案:

答案 0 :(得分:0)

您需要反序列化(packet.getData(), 0, packet.getLength()).

换句话说,调整您的API,以便将这些额外的偏移和长度参数传递给您的deserialize()方法,并在构建ByteArrayInputStream.时使用