简体   繁体   中英

De-serializing an object to class object

I'm working on a Token Ring network implementation, I'm trying to passing a token frame from the station to the ring using a DatagramPacket . I convert the frame object to a byte array using this:

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

and then converting it back using this:

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

Each station is running two threads.

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();
}       
}

..and they all receive from one address - the ring.

Now in the Ring class, I'm trying to convert the byte array but it doesn't do anything, I'm running a while loop but it doesnt loop around once it tries to convert a byte array to a the Frame object.

 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){  

        }
    }

How can I fix this please? Thank you.

You need to deserialize (packet.getData(), 0, packet.getLength()).

In other words adjust your API so that these extra offset and length parameters are passed to your deserialize() method, and are used when constructing your ByteArrayInputStream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM