ECMA Script: IOUtils toInputStream

Hi,

since ‘org.apache.commons.*’ dependency was removed in 2020
https://github.com/openhab/openhab-addons/issues/7722

this line will not work

var input = org.apache.commons.io.IOUtils.toInputStream(content, "UTF-8");

java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils.toInputStream

How can I call Java function toInputStream in ECMA Script?

Frank

There a probably several ways to fix this. However, without knowing the context why you need an inputstream an answer might be might be sub optimal. So can you provide some more context on how you use the inputstream?

Hi @hilbrand,

I’m using

var response = HttpUtil.executeUrl(httpMethod, url , timeout);

for the connection to my web service.
But I need

var response = HttpUtil.executeUrl(httpMethod, url , input, contentType, timeout);

to pass the form parameters too. ‘input’ is a string variable, so I have to convert it with toInputStream(input) to a stream, because the 3rd parameter is of type stream

possible alternative

var stream = new java.io.ByteArrayInputStream(content.getBytes("UTF-8"))
var response = HttpUtil.executeUrl(httpMethod, url , stream, contentType, timeout);

Ah make sense. I see you already found a solution with ByteArrayInputStream.