Groovy web console

subscribe to the feed Subscribe
to this

GString interpolating InputStreams - unexpected result

Published 1 decade ago by Dave Crane with tags GString interpolation InputStream gotcha
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
I have always naively assumed that groovy's GString interpolator ${myVariable} syntax could be
interchanged with toString() pretty much, but here's an important case where it can't be. Interpolating
an InputStream will spit out the entire contents of the stream, which can have some serious consequences!

Issue described in a bit more detail here:
def bytes=new Byte[5];
def is=new ByteArrayInputStream(bytes)

println "BEFORE: bytes available - ${is.available()}"
println is.toString()
println "AFTER toString(): bytes available - ${is.available()}"
println "stream ${is}"
println "AFTER interpolation: bytes available - ${is.available()}"