2015-05-01 21:57:22 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import absolute_import
|
2013-06-24 07:24:37 +00:00
|
|
|
import unittest
|
|
|
|
from jnius import autoclass
|
|
|
|
|
|
|
|
class StringArgumentForByteArrayTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_string_arg_for_byte_array(self):
|
|
|
|
# the ByteBuffer.wrap() accept only byte[].
|
|
|
|
ByteBuffer = autoclass('java.nio.ByteBuffer')
|
2015-05-01 21:57:22 +00:00
|
|
|
self.assertIsNotNone(ByteBuffer.wrap(b'hello world'))
|
2015-02-06 17:34:50 +00:00
|
|
|
|
|
|
|
def test_string_arg_with_signed_char(self):
|
|
|
|
ByteBuffer = autoclass('java.nio.ByteBuffer')
|
2015-05-01 21:57:22 +00:00
|
|
|
self.assertIsNotNone(ByteBuffer.wrap(b'\x00\xffHello World\x7f'))
|
2015-02-07 18:31:04 +00:00
|
|
|
|
|
|
|
def test_fill_byte_array(self):
|
|
|
|
arr = [0, 0, 0]
|
|
|
|
Test = autoclass('org.jnius.BasicsTest')()
|
|
|
|
Test.fillByteArray(arr)
|
2015-12-10 12:20:39 +00:00
|
|
|
# we don't received signed byte, but unsigned in python.
|
2020-04-03 21:48:26 +00:00
|
|
|
self.assertEqual(
|
2015-02-07 18:31:04 +00:00
|
|
|
arr,
|
2015-12-10 12:20:39 +00:00
|
|
|
[127, 1, 129])
|
2015-12-09 23:55:08 +00:00
|
|
|
|
|
|
|
def test_create_bytearray(self):
|
|
|
|
StringBufferInputStream = autoclass('java.io.StringBufferInputStream')
|
|
|
|
nis = StringBufferInputStream("Hello world")
|
2015-12-10 12:20:39 +00:00
|
|
|
barr = bytearray("\x00" * 5, encoding="utf8")
|
2020-04-03 21:48:26 +00:00
|
|
|
self.assertEqual(nis.read(barr, 0, 5), 5)
|
|
|
|
self.assertEqual(barr, b"Hello")
|
2015-12-09 23:55:08 +00:00
|
|
|
|
2015-12-10 12:20:39 +00:00
|
|
|
def test_bytearray_ascii(self):
|
|
|
|
ByteArrayInputStream = autoclass('java.io.ByteArrayInputStream')
|
2015-12-10 13:14:48 +00:00
|
|
|
s = b"".join(bytes(x) for x in range(256))
|
2015-12-10 12:20:39 +00:00
|
|
|
nis = ByteArrayInputStream(s)
|
|
|
|
barr = bytearray("\x00" * 256, encoding="ascii")
|
2020-04-03 21:48:26 +00:00
|
|
|
self.assertEqual(nis.read(barr, 0, 256), 256)
|
|
|
|
self.assertEqual(barr[:256], s[:256])
|
2015-12-10 12:20:39 +00:00
|
|
|
|
2017-04-12 13:59:07 +00:00
|
|
|
def test_empty_bytearray(self):
|
|
|
|
Test = autoclass('org.jnius.BasicsTest')()
|
|
|
|
arr = Test.methodReturnEmptyByteArray()
|
2020-04-03 21:48:26 +00:00
|
|
|
self.assertEqual(len(arr), 0)
|
2017-04-12 13:59:07 +00:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
arr[0]
|
2020-04-03 21:48:26 +00:00
|
|
|
self.assertEqual(arr, [])
|
|
|
|
self.assertEqual(arr[:1], [])
|
|
|
|
self.assertEqual(arr.tostring(), b'')
|