2012-07-30 90 views
5

我正在使用Jython进行开发,我需要使用需要byte[]作为参数的Java方法。在Jython中创建byte []

我想:

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

但我得到这个错误:

TypeError: methodThatNeedsBytesArrays(): 1st arg can't be coerced to byte[] 

回答

3

有时你需要一个字节数组传递给一个函数以便该函数将填充结果的字节数组。在这种情况下,发送Python字符串将不起作用,因为Python字符串是不可变的。相反,创建一个Java字节数组与jarray module

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