supports bytearray as argument for byte[]. Useful for socket/bluetooth reading (no need to read one by one)

This commit is contained in:
Mathieu Virbel 2015-12-10 00:55:08 +01:00
parent 5b4378ac85
commit 5bb7e6cfdb
3 changed files with 10 additions and 2 deletions

View File

@ -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(

View File

@ -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

View File

@ -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")