douyanpeng0748
2016-08-20 04:44
浏览 855
已采纳

从Golang中的io.Reader到io.Writer读取/复制一定数量的字节,或者如果超过一定字节数限制,则返回错误?

I have an io.Reader in Golang and I want to double-check that the size of its data is below a predetermined maximum before or while running io.Copy() to save it to disk using io.Writer.

Since the file data in io.Reader could theoretically be quite large, I want to minimize memory usage and processing here if avoidable.
I don't think there's a function that's like io.CopyLessThanOrEqualToThisManyBytesOrReturnError(), but I did notice that io.ReadFull() can do the opposite to return an error if not enough bytes are there to fill the provided buffer.

Does anyone have a solution to this?


EDIT:
To clarify, copying a fraction of the data is not OK. It either needs to fail if it's over the threshold, or work if it's under.

图片转代码服务由CSDN问答提供 功能建议

我在Golang中有一个 io.Reader </ code>,我想仔细检查一下 在运行 io.Copy()</ code>以使用 io.Writer </ code>将其保存到磁盘之前或之时,其数据大小低于预定的最大值。</ p>

由于 io.Reader </ code>中的文件数据理论上可能很大,因此,如果可以避免的话,我想在这里将内存使用和处理减到最少。
我不认为有一个类似于 io.CopyLessThanOrEqualToThisManyBytesOrReturnError()</ code>,但是我确实注意到,如果没有足够的字节来填充提供的缓冲区, io.ReadFull()</ code>可以相反地返回错误。 </ p>

有人对此有解决方案吗?</ p>


EDIT:</ strong>
\ n为明确起见,复制一部分数据是不正确的。 </ p> </ div>如果它超过阈值,则需要失败,否则,则无法工作。</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题