Search code examples
javaobjectoutputstream

partial serialization of object to byte array


I am setting up a hash function that takes the MD5 of an object and tacks on the first four bytes of the object to prevent collisions. These objects can be quite large so I'd prefer to avoid serializing the entire object. What is the most space/time efficient way I can do this?

I've been looking at ObjectOutputStream and while it appears that there is a partial write function, it seems to require that I've already converted the object into a byte array.


Solution

  • I am setting up a hash function that takes the MD5 of an object and tacks on the first four bytes of the object to prevent collisions.

    But tacking on the 'first four bytes of the object' won't prevent collisions. They aren't unique. MD5 is already almost certainly strong enough.

    These objects can be quite large so I'd prefer to avoid serializing the entire object. What is the most space/time efficient way I can do this?

    I've been looking at ObjectOutputStream and while it appears that there is a partial write function, it seems to require that I've already converted the object into a byte array.

    You won't get the first four bytes of the object via serialization easily, as there is a stream header, serialization protocol tags, etc. to be navigated first. Not that there any point.

    Re your comments, 'prevent collisions' and 'space efficient' are contrary goals.

    You don't need this.