Advertisement
Brord

JNI java to c bundle

Sep 2nd, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. bundle_transactions_t *bundleFromJavaBundle(JNIEnv *env, jobject javaBundle){
  2.     bundle_transactions_t *bundle = NULL;
  3.  
  4.     bundle_transactions_new(&bundle);
  5.    
  6.     jclass bundleClass = (*env)->FindClass(env, "org/iota/jota/model/Bundle" );
  7.     jclass transactionClass = (*env)->FindClass(env, "org/iota/jota/model/Transaction" );
  8.     jmethodID getTransactions = (*env)->GetMethodID(env, bundleClass , "getTransactions", "()Ljava/util/List;" );
  9.     jmethodID toTrytes = (*env)->GetMethodID(env, transactionClass , "toTrytes", "()java/lang/String;" );
  10.    
  11.     jobject txList = (*env)->CallObjectMethod(env, javaBundle, getTransactions );
  12.    
  13.     jclass listClass = (*env)->FindClass(env, "java/util/List" );    
  14.     jmethodID getMethodID = (*env)->GetMethodID(env, listClass, "get", "(I)Ljava/lang/Object;" );
  15.     jmethodID sizeMethodID = (*env)->GetMethodID(env, listClass, "size", "()I" );
  16.     int listItemsCount = (int)(*env)->CallIntMethod(env, txList, sizeMethodID );
  17.     for( int i=0; i<listItemsCount; ++i ){
  18.         jobject javaTx = (*env)->CallObjectMethod(env, txList, getMethodID, i - 1 );
  19.         jstring txTrytes = (*env)->CallObjectMethod(env, javaTx, toTrytes);
  20.        
  21.         jboolean tx_isCopy;
  22.         tryte_t *c_tx_trytes = (tryte_t *) (*env)->GetStringUTFChars(env, txTrytes, &tx_isCopy);
  23.         size_t trytes_len = strlen(c_tx_trytes);
  24.        
  25.         flex_trit_t *flex_trits = malloc(numOfFlexTritsForTxTrits * sizeof(flex_trit_t));
  26.        
  27.         flex_trits_from_trytes(flex_trits, numOfFlexTritsForTxTrits, c_tx_trytes, trytes_len, trytes_len);
  28.        
  29.         iota_transaction_t *c_transaction = transaction_deserialize(flex_trits, false);
  30.         bundle_transactions_add(bundle, c_transaction);
  31.        
  32.         if (tx_isCopy == JNI_TRUE) {
  33.             (*env)->ReleaseStringUTFChars(env, c_tx_trytes, txTrytes);
  34.         }
  35.     }
  36.    
  37.     return bundle; 
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement