Skip to content
Snippets Groups Projects
Commit 829a474a authored by Alexander Scheel's avatar Alexander Scheel Committed by Alexander Scheel
Browse files

Add helper methods for referencing jbyteArrays


Adds a JSS_RefByteArray method to read the jbyte contents of a jbyteArray
and a JSS_DerefByteArray method to release the resources.

Signed-off-by: default avatarAlexander Scheel <ascheel@redhat.com>
parent 5946034e
No related branches found
No related tags found
No related merge requests found
......@@ -609,6 +609,69 @@ jbyteArray JSS_ToByteArray(JNIEnv *env, const void *data, int length)
return byteArray;
}
/************************************************************************
** JSS_RefByteArray.
**
** References the contents of a Java ByteArray into *data, and optionally
** records length information to *lenght. Must be dereferenced via calling
** JSS_DerefByteArray.
**
** Returns
** bool - whether or not the operation succeeded. The operation succeeds if
** array is NULL, it has zero length, or if *data is successfully referenced.
*/
bool JSS_RefByteArray(JNIEnv *env, jbyteArray array, jbyte **data,
jsize *length)
{
bool ret = false;
jsize array_length = 0;
if (env == NULL || data == NULL) {
goto done;
}
*data = NULL;
if (array == NULL) {
/* When there is no input array, return true: no work to do. */
ret = true;
goto done;
}
array_length = (*env)->GetArrayLength(env, array);
if (array_length < 0) {
goto done;
} else if (array_length == 0) {
/* A zero-length array is perfectly valid. */
ret = true;
goto done;
}
*data = (*env)->GetByteArrayElements(env, array, NULL);
if (*data != NULL) {
ret = true;
}
done:
if (length != NULL) {
*length = array_length;
}
return ret;
}
/************************************************************************
** JSS_DerefByteArray.
**
** Dereferences the specified ByteArray and passed reference. mode is the
** same as given to (*env)->ReleaseByteArrayElements: 0 for copy and free,
** JNI_COMMIT for copy without freeing, and JNI_ABORT for free-only.
**
*/
void JSS_DerefByteArray(JNIEnv *env, jbyteArray array, jbyte *data, jint mode) {
if (env == NULL || array == NULL || data == NULL) {
return;
}
(*env)->ReleaseByteArrayElements(env, array, data, mode);
}
/************************************************************************
** JSS_RefJString
**
......
......@@ -296,6 +296,29 @@ int JSS_ConvertNativeErrcodeToJava(int nativeErrcode);
*/
jbyteArray JSS_ToByteArray(JNIEnv *env, const void *data, int length);
/************************************************************************
** JSS_RefByteArray.
**
** References the contents of a Java ByteArray into *data, and optionally
** records length information to *lenght. Must be dereferenced via calling
** JSS_DerefByteArray.
**
** Returns
** bool - whether or not the operation succeeded.
*/
bool JSS_RefByteArray(JNIEnv *env, jbyteArray array, jbyte **data,
jsize *length);
/************************************************************************
** JSS_DerefByteArray.
**
** Dereferences the specified ByteArray and passed reference. mode is the
** same as given to (*env)->ReleaseByteArrayElements: 0 for copy and free,
** JNI_COMMIT for copy without freeing, and JNI_ABORT for free-only.
**
*/
void JSS_DerefByteArray(JNIEnv *env, jbyteArray array, jbyte *data, jint mode);
/************************************************************************
** JSS_RefJString
**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment