public static void putObject(final ByteBuffer buffer,Serializable o){
  try (ByteArrayOutputStream bos=new ByteArrayOutputStream()){
    try (ObjectOutput oout=new ObjectOutputStream(bos)){
      oout.writeObject(o);
      byte[] laneBytes=bos.toByteArray();
      buffer.putInt(laneBytes.length);
      for (int i=0; i < laneBytes.length; i++) {
        buffer.put(laneBytes[i]);
      }
    }
   }
 catch (  IOException e) {
    e.printStackTrace();
  }
}

public static void putObject ( final ByteBuffer buffer , Serializable o ) { try ( ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ) { try ( ObjectOutput oout = new ObjectOutputStream ( bos ) ) { oout . writeObject ( o ) ; byte [ ] laneBytes = bos . toByteArray ( ) ; buffer . putInt ( laneBytes . length ) ; for ( int i = 0 ; i < laneBytes . length ; i ++ ) { buffer . put ( laneBytes [ i ] ) ; } } } catch ( IOException e ) { e . printStackTrace ( ) ; } }
writes the given serializable to the bytebuffer . first writes the length of the serializable as int , then writes the single bytes of the serialized object . the bytebuffer ' s position is incremented according to the length of the serializable .
