ilpizze
ilpizze

Reputation: 1

NotSerializableException problem

the following code throws the exception:

public class TestObject implements Serializable{

    private static final long serialVersionUID = 1L;

    public String value;

}


DbByteArrayOutputStream out = new DbByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(objOut);
objOut.flush();
out.writeTo(file);
objOut.close();
out.close();

public class DbByteArrayOutputStream extends ByteArrayOutputStream {
    public DbByteArrayOutputStream() {
     super();
 }

 public synchronized void writeTo (RandomAccessFile file) throws IOException {
     byte[] data = super.buf;
     int l = super.size();
     file.writeInt(l);
     file.write(data, 0, l);
   }
}

Why? Thanks.

Upvotes: 0

Views: 160

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503889

This is the problem:

ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(objOut);

You're trying to serialize the stream to itself. That makes no sense at all. I suspect you meant:

objOut.writeObject(new TestObject());

or something similar.

Upvotes: 6

Related Questions