diff --git a/bindings/java/c/evmc-vm.c b/bindings/java/c/evmc-vm.c index 84ba5385e..0da90b5be 100644 --- a/bindings/java/c/evmc-vm.c +++ b/bindings/java/c/evmc-vm.c @@ -95,9 +95,8 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv, (void)jcls; struct evmc_message* msg = (struct evmc_message*)(*jenv)->GetDirectBufferAddress(jenv, jmsg); assert(msg != NULL); - size_t code_size = (size_t)(*jenv)->GetDirectBufferCapacity(jenv, jcode); - const uint8_t* code = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, jcode); - assert(code != NULL); + size_t code_size; + const uint8_t* code = GetDirectBuffer(jenv, jcode, &code_size); struct evmc_host_context context = {jcontext_index}; struct evmc_vm* evm = (struct evmc_vm*)(*jenv)->GetDirectBufferAddress(jenv, jevm); assert(evm != NULL); diff --git a/bindings/java/c/host.c b/bindings/java/c/host.c index 36ac7af74..b64eb05a7 100644 --- a/bindings/java/c/host.c +++ b/bindings/java/c/host.c @@ -37,15 +37,14 @@ static jbyteArray CopyDataToJava(JNIEnv* jenv, const void* ptr, size_t size) static void CopyFromByteBuffer(JNIEnv* jenv, jobject src, void* dst, size_t size) { - size_t src_size = (size_t)(*jenv)->GetDirectBufferCapacity(jenv, src); + size_t src_size; + const void* ptr = GetDirectBuffer(jenv, src, &src_size); if (src_size != size) { jclass exception_class = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException"); assert(exception_class != NULL); (*jenv)->ThrowNew(jenv, exception_class, "Unexpected length."); } - void* ptr = (*jenv)->GetDirectBufferAddress(jenv, src); - assert(ptr != NULL); memcpy(dst, ptr, size); } diff --git a/bindings/java/c/host.h b/bindings/java/c/host.h index 6f8dd075c..d6f4345a8 100644 --- a/bindings/java/c/host.h +++ b/bindings/java/c/host.h @@ -19,6 +19,17 @@ struct evmc_host_context int evmc_java_set_jvm(JNIEnv*); const struct evmc_host_interface* evmc_java_get_host_interface(); +static inline void* GetDirectBuffer(JNIEnv* jenv, jobject buf, size_t* size) +{ + void* ret = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, buf); + assert(ret != NULL); + jlong jsize = (*jenv)->GetDirectBufferCapacity(jenv, buf); + assert(jsize != -1); + if (size) + *size = (size_t)jsize; + return ret; +} + #ifdef __cplusplus } #endif