Given that Groovy does not have a do-while statement, how can I iterate over all bytes in an input stream?
Per a previous version of the Groovy user guide:
I know that's an old and already answered question. But it's the 1st what pops up for 'groovy do while' when googled.
I think general considerable do-while synonym in Groovy could be:
while ({
...
numRead > 0
}()) continue
Please consider the above example. Except some 'redundant' brackets it's rather well readable syntax.
And here's how does it work:
while
condition round brackets a closure is defined with curly bracket opencontinue
after while condition closing round bracket is only because there has to be 'something', any compilable statement. For example it could be 0
, though continue
seems to fit much better.EDIT: Not sure is it a newer version of Groovy or I've missed that before.
Instead continue
semicolon will do as well. Then it goes like:
while ({
...
numRead > 0
}());
The groovy (version 1.8+) way would be like this:
inputStream.eachByte(BUFFER_SIZE) { buffer, numRead ->
...
}
use this:
for(;;){ // infinite for
...
if( numRead == 0 ){ //condition to break, oppossite to while
break
}
}