From 5bb7e6cfdb89a88c2168833bddbfb358da426e76 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Thu, 10 Dec 2015 00:55:08 +0100 Subject: [PATCH] supports bytearray as argument for byte[]. Useful for socket/bluetooth reading (no need to read one by one) --- jnius/jnius_conversion.pxi | 2 +- jnius/jnius_utils.pxi | 2 +- tests/test_bytearray.py | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/jnius/jnius_conversion.pxi b/jnius/jnius_conversion.pxi index fc451d0..1127a35 100644 --- a/jnius/jnius_conversion.pxi +++ b/jnius/jnius_conversion.pxi @@ -97,7 +97,7 @@ cdef void populate_args(JNIEnv *j_env, tuple definition_args, jvalue *j_args, ar if isinstance(py_arg, ByteArray) and argtype != '[B': raise JavaException( 'Cannot use ByteArray for signature {}'.format(argtype)) - if not isinstance(py_arg, (list, tuple, ByteArray)): + if not isinstance(py_arg, (bytearray, list, tuple, ByteArray)): raise JavaException('Expecting a python list/tuple, got ' '{0!r}'.format(py_arg)) j_args[index].l = convert_pyarray_to_java( diff --git a/jnius/jnius_utils.pxi b/jnius/jnius_utils.pxi index 3c6b122..a6dbd91 100644 --- a/jnius/jnius_utils.pxi +++ b/jnius/jnius_utils.pxi @@ -332,7 +332,7 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *: score += 10 continue - if r == '[B' and isinstance(arg, ByteArray): + if r == '[B' and isinstance(arg, (bytearray, ByteArray)): score += 10 continue diff --git a/tests/test_bytearray.py b/tests/test_bytearray.py index 226289d..9e9b37c 100644 --- a/tests/test_bytearray.py +++ b/tests/test_bytearray.py @@ -19,3 +19,11 @@ class StringArgumentForByteArrayTest(unittest.TestCase): self.assertEquals( arr, [127, 1, -127]) + + def test_create_bytearray(self): + StringBufferInputStream = autoclass('java.io.StringBufferInputStream') + nis = StringBufferInputStream("Hello world") + barr = bytearray("\x00" * 5) + self.assertEquals(nis.read(barr, 0, 5), 5) + self.assertEquals(barr, "Hello") +