3

I'm trying to:

  • Write an object (or a series of objects of different types/classes) into a file
  • Read them back
  • Check the instances and cast them into objects of their same type/class again

I could find these two classes, and this is how I use them. But the data[] array doesn't make much sense to me. Why do you have to put an empty array of data into the deserialize method?

public static byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(out);
    os.writeObject(obj);
    return out.toByteArray();
}
public static Object deserialize(byte[] data)
        throws IOException, ClassNotFoundException {
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    ObjectInputStream is = new ObjectInputStream(in);
    return is.readObject();
}

public static void main(String[] args) {

    try {
        Thing p = new Thing(2,4);

        byte[]data = new byte[10240];
        serialize(p);
        Object des = deserialize(data);

    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(Pruebiña.class.getName())
            .log(Level.SEVERE, null, ex);
    }

}

How can I fix this? Now I'm having the following error, when the program reaches the deserialize line:

java.io.StreamCorruptedException: invalid stream header: 00000000
     at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)

What can I do to fix this, and being able to write and read the objects back? And yes, the class Thing is Serializable.

1
  • Well, you don't have to create an empty array, of course, it's just a misunderstanding on your part, that is exposed by the StreamCorruptedException. You have to pass an array full of data, that you got from the incoming ByteBuffer.. Commented Sep 4, 2015 at 5:08

2 Answers 2

2

If you want to write to a File you don't need the byte arrays at all use FileInputStream and FileOutputStream eg.

public static void serialize(Object obj, File f) throws IOException {
    try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f))) {
       out.writeObject(obj);
    }
}

public static Object deserialize(File f)
        throws IOException, ClassNotFoundException {
    try (ObjectInputStream is = new ObjectInputStream(new FileInputStream(f))) {
        return is.readObject();
    }
}

static class Thing implements Serializable {
    int a,b,c;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {

    File f = new File("object.dat");
    Thing orig = new Thing();
    serialize(orig, f);
    Thing back = (Thing) deserialize(f);
}
Sign up to request clarification or add additional context in comments.

3 Comments

VG, I agree, but there are resource leaks here.
@EJP If by resource leaks you mean I didn't close my file streams, hopefully it is fixed now. If I missed something else let me know.
You missed closing the ObjectOutputStream.
2

You create the array in serialize, you don't need to create your own array.

Just do this:

    byte[] data = serialize(p);

Instead of this:

    byte[]data = new byte[10240];
    serialize(p);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.