Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to InputStream in Groovy

Given a string:

String xml = "<test/>";

How do I convert it to an InputStream in Groovy?

Currently I use:

IOUtils.toInputStream(xml, StandardCharsets.UTF_8)

It works, but I'm looking for some shorter and dependency-free way of doing that in Groovy.

Of course I know answer for Java, but it involves ugly creation of ByteArrayInputStream. I'm looking for GDK way of solving that.

like image 337
Michal Kordas Avatar asked Dec 13 '22 16:12

Michal Kordas


1 Answers

Has not much to do with Groovy, plain java:

InputStream stream = new ByteArrayInputStream( xml.getBytes( 'UTF-8' ) )
like image 108
injecteer Avatar answered Dec 26 '22 09:12

injecteer