code: public static byte[] serialize( AbstractMessage message , int typicalSize )
      {
          RawDataBuffer rawMsg = message.getRawMessage();
          if (rawMsg != null)
              return rawMsg.toByteArray();
  
          RawDataBuffer buffer = new RawDataBuffer(typicalSize);
          buffer.writeByte(message.getType());
          message.serializeTo(buffer);
          
          return buffer.toByteArray();
      }
docs: Serialize a message