Making jbox2D World Serializable / Externalizable

I'm currently using jbox2d as physics library for an android project. Android requires objects to be serializable themselves to be put in a parcel and then saved, so i had to make World serializable.
I made World implement Externalizable (and thereby Serializable) and added the following lines of code:

Code: Select all

   @Override
   public void writeExternal(final ObjectOutput out) throws IOException {
      PbSerializer serial = new PbSerializer();
      SerializationResult result = serial.serialize(this);
      if (out instanceof OutputStream) {
         result.writeTo((OutputStream) out);
      } else {
         result.writeTo(new OutputStream() {
            @Override
            public void write(int b) throws IOException {
               out.write(b);
            }
         });
      }
   }

   @Override
   public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
      PbDeserializer deserial = new PbDeserializer();
      if (in instanceof InputStream) {
         deserial.deserializeWorld((InputStream) in, this);
      } else {
         deserial.deserializeWorld(new InputStream() {
            @Override
            public int read() throws IOException {
               return in.read();
            }
         }, this);
      }
   }

When deserializing, I'm writing the read data to the current World instance, so I don't get any problems with sub-classes. To do that, I replaced PbDeserializer.deserializeWorld(PbWorld argWorld) by

Code: Select all

   public World deserializeWorld(PbWorld argWorld) {
      return deserializeWorld(argWorld, new World(new Vec2(), false));
   }

   public World deserializeWorld(PbWorld argWorld, World world) {
      world.setGravity(pbToVec(argWorld.getGravity()));
      world.setAllowSleep(argWorld.getAllowSleep());

      world.setAutoClearForces(argWorld.getAutoClearForces());
      // ... old method content
   }

Sorry for not uploading my changes via svn, I'm quite new to it and didn't want to mess with google code.

Hope it helps!
NCode

Original thread: https://box2d.org/forum/viewtopic.php?t=8862