22 #ifndef jtie_tconv_ptrbybb_impl_hpp
23 #define jtie_tconv_ptrbybb_impl_hpp
29 #include "jtie_tconv_ptrbybb.hpp"
30 #include "jtie_stdint.h"
31 #include "jtie_tconv_impl.hpp"
32 #include "jtie_tconv_idcache_impl.hpp"
33 #include "jtie_tconv_utils_impl.hpp"
34 #include "helpers.hpp"
41 JTIE_DEFINE_METHOD_MEMBER_INFO(_ByteBuffer_isReadOnly)
45 typedef JniMemberId< WEAK_CACHING, _ByteBuffer_isReadOnly >
46 ByteBuffer_isReadOnly;
51 JTIE_DEFINE_METHOD_MEMBER_INFO(_ByteBuffer_asReadOnlyBuffer)
55 typedef JniMemberId< WEAK_CACHING, _ByteBuffer_asReadOnlyBuffer >
56 ByteBuffer_asReadOnlyBuffer;
61 JTIE_DEFINE_METHOD_MEMBER_INFO(_ByteBuffer_remaining)
65 typedef JniMemberId< WEAK_CACHING, _ByteBuffer_remaining >
71 JTIE_DEFINE_METHOD_MEMBER_INFO(_ByteBuffer_position)
75 typedef JniMemberId< WEAK_CACHING, _ByteBuffer_position >
95 template< typename J >
97 wrapAddressAsByteBuffer(const
void * c, JNIEnv * env);
99 template< typename J >
101 wrapByteBufferAsReadOnly(J * jbb, JNIEnv * env);
106 template< typename J, typename C >
111 TRACE(
"C * ByteBufferPtrParam.convert(cstatus &, jtie_j_n_ByteBuffer, JNIEnv *)");
122 && (ensureMutableBuffer(j, env) != 0)) {
125 if (ensureMinBufferSize< J::capacity >(j, env) != 0) {
128 assert(env->GetDirectBufferCapacity(j) >= J::capacity);
129 void * a = getByteBufferAddress(j, env);
135 c =
static_cast< C *
>(a);
145 TRACE(
"void ByteBufferPtrParam.release(C *, jtie_j_n_ByteBuffer, JNIEnv *)");
146 (void)c; (void)j; (void)env;
151 template<
typename J,
typename C >
154 convert(C * c, JNIEnv * env) {
155 TRACE(
"J * ByteBufferPtrResult.convert(C *, JNIEnv *)");
163 J * jbb = wrapAddressAsByteBuffer< J >(c, env);
167 assert(env->GetDirectBufferCapacity(jbb) == J::capacity);
172 J * jrobb = wrapByteBufferAsReadOnly(jbb, env);
179 env->DeleteLocalRef(jbb);
198 jclass cls = ByteBuffer_isReadOnly::getClass(env);
203 jmethodID mid = ByteBuffer_isReadOnly::getId(env, cls);
207 jboolean ro = env->CallBooleanMethod(jbb, mid);
208 if (env->ExceptionCheck() != JNI_OK) {
212 const char * c =
"java/nio/ReadOnlyBufferException";
214 const char * m = NULL;
218 registerException(env, c, m);
226 ByteBuffer_isReadOnly::releaseRef(env, cls);
239 jlong bc = env->GetDirectBufferCapacity(jbb);
243 const long long n = N;
244 const long long BC = bc;
246 sprintf(m,
"JTie: failed to retrieve java.nio.ByteBuffer's"
247 " capacity (perhaps, a direct buffer or an unaligned"
250 sprintf(m,
"JTie: java.nio.ByteBuffer's capacity is too small"
251 " for the mapped parameter;"
252 " required: %lld, found: %lld.", n, BC);
254 const char * c =
"java/lang/IllegalArgumentException";
255 registerException(env, c, m);
259 jclass cls = ByteBuffer_remaining::getClass(env);
264 jmethodID mid = ByteBuffer_remaining::getId(env, cls);
268 jint r = env->CallIntMethod(jbb, mid);
269 if (env->ExceptionCheck() != JNI_OK) {
275 const long long n = N;
276 const long long R = r;
277 sprintf(m,
"JTie: too few remaining elements of"
278 " java.nio.ByteBuffer for mapped parameter;"
279 " required: %lld, found: %lld", n, R);
280 const char * c =
"java/lang/IllegalArgumentException";
281 registerException(env, c, m);
289 ByteBuffer_remaining::releaseRef(env, cls);
307 jclass cls = ByteBuffer_position::getClass(env);
312 jmethodID mid = ByteBuffer_position::getId(env, cls);
316 jint p = env->CallIntMethod(jbb, mid);
317 if (env->ExceptionCheck() != JNI_OK) {
325 ByteBuffer_position::releaseRef(env, cls);
336 char * a =
static_cast< char *
>(env->GetDirectBufferAddress(jbb));
338 #ifndef JTIE_BYTEBUFFER_NO_ZERO_CAPACITY_MAPPING
340 if (env->GetDirectBufferCapacity(jbb) != 0) {
341 #endif // JTIE_BYTEBUFFER_NO_ZERO_CAPACITY_MAPPING
343 const char * m = (
"JTie: cannot get the java.nio.ByteBuffer's"
344 " internal address (perhaps, not a direct buffer"
345 " or its memory region is undefined)");
346 const char * c =
"java/lang/IllegalArgumentException";
347 registerException(env, c, m);
348 #ifndef JTIE_BYTEBUFFER_NO_ZERO_CAPACITY_MAPPING
353 #endif // JTIE_BYTEBUFFER_NO_ZERO_CAPACITY_MAPPING
355 #ifndef JTIE_BYTEBUFFER_MAPS_TO_BASE_ADDRESS
356 int32_t p = getBufferPosition(jbb, env);
363 #endif // JTIE_BYTEBUFFER_MAPS_TO_BASE_ADDRESS
369 template<
typename J >
371 wrapAddressAsByteBuffer(
const void * c, JNIEnv * env) {
373 void * mc =
const_cast< void *
>(c);
374 jobject jo = env->NewDirectByteBuffer(mc, J::capacity);
375 return static_cast< J *
>(jo);
379 template<
typename J >
381 wrapByteBufferAsReadOnly(J * jbb, JNIEnv * env) {
386 jclass cls = ByteBuffer_asReadOnlyBuffer::getClass(env);
391 jmethodID mid = ByteBuffer_asReadOnlyBuffer::getId(env, cls);
396 jobject jo = env->CallObjectMethod(jbb, mid);
397 if (env->ExceptionCheck() != JNI_OK) {
402 = (
"JTie: invalid NULL return from"
403 " java.nio.ByteBuffer.asReadOnlyBuffer()");
404 const char * c =
"java/lang/AssertionError";
405 registerException(env, c, m);
407 j =
static_cast< J *
>(jo);
412 ByteBuffer_asReadOnlyBuffer::releaseRef(env, cls);
422 template<
typename C >
425 template<
typename C >
428 template<
typename J,
typename C >
431 template<
typename J,
typename C >
440 template<
typename J,
typename C >
443 template<
typename J,
typename C >
452 template<
typename C >
455 template<
typename C >
461 #endif // jtie_tconv_ptrbybb_impl_hpp