Is it possible to convert a java stream directly to a javascript array without looping through each element like this and ideally use the .addAll() method?
var ArrayList = Java.type("java.util.ArrayList");
var fileList = new ArrayList();
var stream = Files.list(Paths.get("/etc/..."));
stream.forEach((file) => {
fileList.add(file);
});
stream.close();
Stream, no I don’t think so. You’ll need to collapse the Stream first using a Collector.
Once you have a List or Set or the like, there are some utilities in openhab-js that can convert between Java Lists and JS Arrays and back.
But most of these are just one-liners so you don’t necessarily need to use the openhab-js utils.
It’s pretty miserable stuff trying to mix Java streams API and JS so I avoid it as much as possible. For something like this I’d either install the fs or file-system node modules and remain using pure JS end-to-end, or I’d use executeCommandLine and parse the String returned by ls.
var fileList = actions.Exec.executeCommandLine(time.Duration.ofSeconds(1), 'ls', '/etc').split('\n');