jpype
jpype copied to clipboard
Any way to convert IOBase into InputStream?
... or TextIOBase
into Reader
?
It would be very nice to open a file with open()
and pass the returned IOBase to a Java method accepting an InputStream
or a Reader
As of JPype 1.3 it is not possible.
It is technically possible but the Python to Java bridge would need to be used to make it easy. Because then Java InputStream can be directly implemented in Java.
There is another way to achieve the result without the reverse bridge, but it would require another support class in org.jpype. It would require an interface in Java which provides services that Python can use JProxy to implement. The wrapper can then forward to InputStream requests to IOBase. Thus it requires 3 pieces. It would be a 2 to 4 hour task.
Maybe I see your point. In the next days, I'll try to implement the second solution without affecting org.jpype (i.e. with some external jar). Maybe you could then integrate that in org.jpype?
Sure that would be a great PR. It shouldn't be too difficult. You just need to
- Define an interface with all the methods where Java needs to call the IOBase methods required for InputStream. (IOBaseProxy)
- Create a Java implementation (IOBaseInputStream) with a constructor that takes a IOBaseProxy and uses it to implement InputStream.
- Implement the interface using JProxy in Python.
- Write a conversion method in Python that takes an IOBase creates the JProxy instance, then returns the implementation IOBaseInputStream(IOBaseProxy)
- (Optional) register the conversion with JPype so that any method which takes input stream will now take an IOBase.
Any update with this ?