如何使用Groovy遍历inputStream中的所有字节,因为它缺少do-while语句? [英] How do I iterate over all bytes in an inputStream using Groovy, given that it lacks a do-while statement?

查看:395
本文介绍了如何使用Groovy遍历inputStream中的所有字节,因为它缺少do-while语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设Groovy没有do-while语句,我怎样才能遍历输入流中的所有字节?



每个以前版本的Groovy用户指南


现在还没有'do ... while()'语法。



由于含糊不清,我们还没有添加对do ..的支持Groovy


在Groovy中执行类似以下Java代码的最佳方法是什么? / p>

  def numRead = inputStream.read(fileBytes,0,fileBytes.length); 
do {

} while(numRead> 0);

(我知道我可以使用布尔值来做到这一点,我只想知道是否有一个Groovy 的方式)

解决方案

Groovy(版本1.8+)的方式是这样的:

  inputStream.eachByte(BUFFER_SIZE){buffer,numRead  - > 
...
}


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:

No 'do ... while()' syntax as yet.

Due to ambiguity, we've not yet added support for do .. while to Groovy

What would be the best way to do something like the following Java code in Groovy?

def numRead = inputStream.read(fileBytes, 0, fileBytes.length);
do{

} while(numRead > 0);

(I know I can do that using a boolean, I just want to know if there's a "Groovy" way of doing it)

解决方案

The groovy (version 1.8+) way would be like this:

inputStream.eachByte(BUFFER_SIZE) { buffer, numRead ->
    ...
}

这篇关于如何使用Groovy遍历inputStream中的所有字节,因为它缺少do-while语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆