Create Byte [] in Jython

I am developing Jython, and I need to use the Java method, which is required as a parameter byte[].

I tried:

def randomBytesArray(length):
    data = []
    for _ in xrange(length):
        data.append(chr(random.getrandbits(8)))
    methodThatNeedsBytesArrays(data)

But I get this error:

TypeError: methodThatNeedsBytesArrays(): 1st arg can't be coerced to byte[]
+5
source share
2 answers

Jython user guide answers it: use string

+3
source

Sometimes you need to pass an array of bytes to a function so that the function fills the byte array with the result. In this case, sending a Python string will not work, because Python strings are immutable. Instead, create a Java byte array with the jarray module :

import jarray
bytes = jarray.zeros(100, "b")
length = zlibDeflater.deflate(bytes)
...
+3
source

All Articles