Image

Imagedplass wrote in Imagejava_dev

Here's another one

Is there anything wrong with this code?
    public static byte[] serialize(Serializable ob) throws IOException {
        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();

        // Create a briefly existing object output stream to write the object
        // into an array of bytes.
        new ObjectOutputStream(byteOutStream).writeObject(ob);

        return byteOutStream.toByteArray();
    }


Specifically, the ObjectOutputStream -- will that get closed and GC'ed properly? I seem to remember some kind of JDK resource "leak" with code like this, whereby a temporary object is not cleaned up right. Is this true?

Alternatively, can anyone suggest a better way to serialize something to an array of bytes?