0

I want to convert an object into a byte array and convert that array back into an object. Everything works fine, besides that the method which converts the array back into an object doesn't print the right object out.

public static List<Person> list_interviewees;
    main { 
        list_interviewees = new ArrayList<Person>();
        Person p = new Person();
        p.setAge(20);
        p.setDegree("bhd");
        p.setLastJob("asd");
        p.setLastName("Test");
        p.setName("Tester");
        list_interviewees.add(p);
        Person p2 = new Person();
        p2.setAge(20);
        p2.setDegree("bhd");
        p2.setLastJob("asd");
        p2.setLastName("QWEQW");
        p2.setName("ASAD");
        list_interviewees.add(p2);
        transferBytes();
        }

    public static void sendBytes(byte[] b) {
        System.out
                .println("++++++ This method simulates sending information to other site and reads the data");
        System.out.println("Received bytes" + b);
        System.out.println("Total bytes" + b.length);

        Person p = null;

        try (ByteArrayInputStream bis = new ByteArrayInputStream(b);
                ObjectInputStream in = new ObjectInputStream(bis);) {
            p = (Person) in.readObject();
            System.out.println(p.getName() + " " + p.getLastName());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static void transferBytes() {

        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(bos);) {

            for (Person p : list_interviewees) {
                
                out.writeObject(p);
                sendBytes(bos.toByteArray());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

If I run the code above I get the output

++++++ This method simulates sending information to other site and reads the data

Received bytes[B@4aa298b7

Total bytes158

Tester Test

++++++ This method simulates sending information to other site and reads the data

Received bytes[B@4554617c

Total bytes195

Tester Test

Why do I get the name Tester Test twice there? I checked the person which is send through the debugger, it is the correct one, so why does the sendBytes method receive the wrong array?.

Community
  • 1
  • 1
Ihara
  • 176
  • 2
  • 14
  • Also, you never add `p2` to the `list_interviewees` – Elliott Frisch May 18 '15 at 16:24
  • @ElliottFrisch - is an array send/receive methodology the same as that for a class? – KevinDTimm May 18 '15 at 16:26
  • OP is printing array reference addresses. Since we can't run the code OP should probably tell us what those array(s) actually contain. – Elliott Frisch May 18 '15 at 16:28
  • To the OP, I can't understand how you get those results with that code. You only add one object to `list_interviewees` (as already mentioned) and your print statements don't seem to correspond with your results (printing the name at the end when it seems it's the first thing you do in `transferBytes` and then you call `sendBytes`. Puzzling. – KevinDTimm May 18 '15 at 16:32
  • Sry, I forgot to copy the code where I add p2 to the list, I hope its a bit clearer now. My only question is why sendBytes prints the Tester name twice, instead of the next object from the arraylist. – Ihara May 18 '15 at 16:47

0 Answers0