1

I'm running Java code in python using PY4J (http://py4j.sourceforge.net/).

My java function returns an InputStream and I would like to manipulate it in my python code:

Java code:

public InputStream getPCAP(key) {
        InputStream inputStream = cyberStore.getPCAP(pcapStringKey);
        return inputStream;
}

Python code:

from py4j.java_gateway import JavaGateway

gateway = JavaGateway()
input_stream = PY4J_GateWay.getPCAP(key); 
...

How can I get the InputStream in python?

Should I convert it to something else in the java code before returning it to python?

4

1 回答 1

1

我猜你想从输入流中读取。

Java 的 API 允许您从InputStream一个byte数组中读取。没有简单的方法bytearray通过引用传递 Pythonic,但是您可以轻松添加一个从 an 读取InputStream并返回byte数组的方法:

public byte[] read(InputStream stream, int count) throws IOException {
    byte[] bytes = new byte[count];
    stream.read(bytes);
    return bytes;
}

然后,在 Python 中,您可以调用此方法来读取InputStream

gateway = JavaGateway()
input_stream = gateway.getPCAP(key)
data = gateway.read(input_stream, 1000) # reads 1000 bytes from input_stream
于 2014-09-22T08:08:10.630 回答