Importing dynamic content from a resource [message #1857804] |
Mon, 27 February 2023 03:35 |
|
I have a case where I import a clients text elements from a resource, piping them into the output. I am using something like the below, but it seems quite ugly and clunky. Is there a solution for this in EGL?
final InputStream inputStream = URI.create(path).toURL().openStream();
new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
[Updated on: Wed, 01 March 2023 02:39] Report message to a moderator
|
|
|
|
|
Re: Importing dynamic content from a resource [message #1857827 is a reply to message #1857825] |
Tue, 28 February 2023 02:47 |
|
Actually, the code above does not even work, because the String constructor is hidden by EOL's String semantic. So I have now resorted to the monster below.
var URI = Native("java.net.URI");
var inputStream = URI.create(uri).toURL().openStream();
var bytes = inputStream.readAllBytes();
var baos = new Native("java.io.ByteArrayOutputStream");
var Array = Native("java.lang.reflect.Array");
baos.write(bytes,0,Array.getLength(bytes));
var Charset = Native("java.nio.charset.Charset");
return baos.toString(Charset.forName("UTF-8"));
|
|
|
|
|
Re: Importing dynamic content from a resource [message #1857848 is a reply to message #1857840] |
Wed, 01 March 2023 14:27 |
|
Thanks for sharing this! Given that this functionality can be implemented with a handful of lines of code that can be tucked away into an EOL operation, I think I'd prefer that over a tool, but of course if one starts needing richer support (e.g. for different encodings, types of sources) this can tip the scale.
Thanks,
Dimitris
|
|
|
Powered by
FUDForum. Page generated in 0.03281 seconds