From c5863be424a58d1b212df06bb5497aeee1060148 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Thu, 10 Dec 2015 13:11:25 +0100 Subject: [PATCH] bytearray: fix bytes -128-127 from java to 0-255 python --- jnius/jnius_conversion.pxi | 10 +++++++--- jnius/jnius_nativetypes.pxi | 11 ++++++----- jnius/jnius_nativetypes3.pxi | 11 ++++++----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/jnius/jnius_conversion.pxi b/jnius/jnius_conversion.pxi index bb13184..0eb98d1 100644 --- a/jnius/jnius_conversion.pxi +++ b/jnius/jnius_conversion.pxi @@ -57,8 +57,12 @@ cdef void populate_args(JNIEnv *j_env, tuple definition_args, jvalue *j_args, ar j_args[index].l = NULL elif (isinstance(py_arg, basestring) or (PY_MAJOR_VERSION >=3 and isinstance(py_arg, str))) \ and jstringy_arg(argtype): - py_str = py_arg.encode('utf-8') - j_args[index].l = j_env[0].NewStringUTF(j_env, py_str) + try: + py_str = py_arg + j_args[index].l = j_env[0].NewStringUTF(j_env, py_str) + except UnicodeEncodeError: + py_str = py_arg.encode('utf-8') + j_args[index].l = j_env[0].NewStringUTF(j_env, py_str) elif isinstance(py_arg, JavaClass): jc = py_arg check_assignable_from(j_env, jc, argtype[1:-1]) @@ -492,7 +496,7 @@ cdef jobject convert_pyarray_to_java(JNIEnv *j_env, definition, pyarray) except if isinstance(pyarray, ByteArray): a_bytes = pyarray j_env[0].SetByteArrayRegion(j_env, - ret, 0, array_size, a_bytes._buf) + ret, 0, array_size, a_bytes._buf) else: for i in range(array_size): c_tmp = pyarray[i] diff --git a/jnius/jnius_nativetypes.pxi b/jnius/jnius_nativetypes.pxi index 279bbb1..f93f061 100644 --- a/jnius/jnius_nativetypes.pxi +++ b/jnius/jnius_nativetypes.pxi @@ -17,8 +17,8 @@ cdef python_op(int op, object a, object b): cdef class ByteArray: cdef LocalRef _jobject cdef long _size - cdef jbyte *_buf - cdef jbyte[:] _arr + cdef unsigned char *_buf + cdef unsigned char[:] _arr def __cinit__(self): self._size = 0 @@ -31,7 +31,8 @@ cdef class ByteArray: cdef JNIEnv *j_env if self._buf != NULL: j_env = get_jnienv() - j_env[0].ReleaseByteArrayElements(j_env, self._jobject.obj, self._buf, 0) + j_env[0].ReleaseByteArrayElements( + j_env, self._jobject.obj, self._buf, 0) self._buf = NULL self._jobject = None @@ -41,8 +42,8 @@ cdef class ByteArray: self._jobject = LocalRef() self._jobject.create(env, obj) self._size = size - self._buf = buf - self._arr = self._buf + self._buf = buf + self._arr = self._buf def __str__(self): return ''.format( diff --git a/jnius/jnius_nativetypes3.pxi b/jnius/jnius_nativetypes3.pxi index c205538..6993d0a 100644 --- a/jnius/jnius_nativetypes3.pxi +++ b/jnius/jnius_nativetypes3.pxi @@ -16,8 +16,8 @@ cdef python_op(int op, object a, object b): cdef class ByteArray: cdef LocalRef _jobject cdef long _size - cdef jbyte *_buf - cdef jbyte[:] _arr + cdef unsigned char *_buf + cdef unsigned char[:] _arr def __cinit__(self): self._size = 0 @@ -28,7 +28,8 @@ cdef class ByteArray: cdef JNIEnv *j_env if self._buf != NULL: j_env = get_jnienv() - j_env[0].ReleaseByteArrayElements(j_env, self._jobject.obj, self._buf, 0) + j_env[0].ReleaseByteArrayElements( + j_env, self._jobject.obj, self._buf, 0) self._buf = NULL self._jobject = None @@ -38,8 +39,8 @@ cdef class ByteArray: self._jobject = LocalRef() self._jobject.create(env, obj) self._size = size - self._buf = buf - self._arr = self._buf + self._buf = buf + self._arr = self._buf def __str__(self): return ''.format(