7

I am developing with Jython and I need to use a Java method that requires a byte[] as a parameter.

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[]

1 Answer 1

5

Sometimes you need to pass a byte array to a function so that the function will fill the byte array with a result. In that case, sending a Python string won't 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)
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.