|
|
|
@ -4292,25 +4292,12 @@ class JniUtil { |
|
|
|
|
* @param bytes The bytes to copy |
|
|
|
|
* |
|
|
|
|
* @return the Java byte[] or nullptr if an exception occurs |
|
|
|
|
*
|
|
|
|
|
* @throws RocksDBException thrown
|
|
|
|
|
* if memory size to copy exceeds general java specific array size limitation. |
|
|
|
|
*/ |
|
|
|
|
static jbyteArray copyBytes(JNIEnv* env, std::string bytes) { |
|
|
|
|
const jsize jlen = static_cast<jsize>(bytes.size()); |
|
|
|
|
|
|
|
|
|
jbyteArray jbytes = env->NewByteArray(jlen); |
|
|
|
|
if(jbytes == nullptr) { |
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
env->SetByteArrayRegion(jbytes, 0, jlen, |
|
|
|
|
const_cast<jbyte*>(reinterpret_cast<const jbyte*>(bytes.c_str()))); |
|
|
|
|
if(env->ExceptionCheck()) { |
|
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException
|
|
|
|
|
env->DeleteLocalRef(jbytes); |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return jbytes; |
|
|
|
|
return createJavaByteArrayWithSizeCheck(env, bytes.c_str(), bytes.size()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -4473,26 +4460,39 @@ class JniUtil { |
|
|
|
|
|
|
|
|
|
return jbyte_strings; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copies bytes from a rocksdb::Slice to a jByteArray |
|
|
|
|
* |
|
|
|
|
* @param env A pointer to the java environment |
|
|
|
|
* @param bytes The bytes to copy |
|
|
|
|
* |
|
|
|
|
* @return the Java byte[] or nullptr if an exception occurs |
|
|
|
|
*/ |
|
|
|
|
static jbyteArray copyBytes(JNIEnv* env, const Slice& bytes) { |
|
|
|
|
const jsize jlen = static_cast<jsize>(bytes.size()); |
|
|
|
|
|
|
|
|
|
* Copies bytes to a new jByteArray with the check of java array size limitation. |
|
|
|
|
* |
|
|
|
|
* @param bytes pointer to memory to copy to a new jByteArray |
|
|
|
|
* @param size number of bytes to copy |
|
|
|
|
* |
|
|
|
|
* @return the Java byte[] or nullptr if an exception occurs |
|
|
|
|
*
|
|
|
|
|
* @throws RocksDBException thrown
|
|
|
|
|
* if memory size to copy exceeds general java array size limitation to avoid overflow. |
|
|
|
|
*/ |
|
|
|
|
static jbyteArray createJavaByteArrayWithSizeCheck(JNIEnv* env, const char* bytes, const size_t size) { |
|
|
|
|
// Limitation for java array size is vm specific
|
|
|
|
|
// In general it cannot exceed Integer.MAX_VALUE (2^31 - 1)
|
|
|
|
|
// Current HotSpot VM limitation for array size is Integer.MAX_VALUE - 5 (2^31 - 1 - 5)
|
|
|
|
|
// It means that the next call to env->NewByteArray can still end with
|
|
|
|
|
// OutOfMemoryError("Requested array size exceeds VM limit") coming from VM
|
|
|
|
|
static const size_t MAX_JARRAY_SIZE = (static_cast<size_t>(1)) << 31; |
|
|
|
|
if(size > MAX_JARRAY_SIZE) { |
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, "Requested array size exceeds VM limit"); |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const jsize jlen = static_cast<jsize>(size); |
|
|
|
|
jbyteArray jbytes = env->NewByteArray(jlen); |
|
|
|
|
if(jbytes == nullptr) { |
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
env->SetByteArrayRegion(jbytes, 0, jlen, |
|
|
|
|
const_cast<jbyte*>(reinterpret_cast<const jbyte*>(bytes.data()))); |
|
|
|
|
const_cast<jbyte*>(reinterpret_cast<const jbyte*>(bytes))); |
|
|
|
|
if(env->ExceptionCheck()) { |
|
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException
|
|
|
|
|
env->DeleteLocalRef(jbytes); |
|
|
|
@ -4502,6 +4502,21 @@ class JniUtil { |
|
|
|
|
return jbytes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copies bytes from a rocksdb::Slice to a jByteArray |
|
|
|
|
* |
|
|
|
|
* @param env A pointer to the java environment |
|
|
|
|
* @param bytes The bytes to copy |
|
|
|
|
* |
|
|
|
|
* @return the Java byte[] or nullptr if an exception occurs |
|
|
|
|
*
|
|
|
|
|
* @throws RocksDBException thrown
|
|
|
|
|
* if memory size to copy exceeds general java specific array size limitation. |
|
|
|
|
*/ |
|
|
|
|
static jbyteArray copyBytes(JNIEnv* env, const Slice& bytes) { |
|
|
|
|
return createJavaByteArrayWithSizeCheck(env, bytes.data(), bytes.size()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Helper for operations on a key and value |
|
|
|
|
* for example WriteBatch->Put |
|
|
|
|