-6ren">
gpt4 book ai didi

java - FindBugs:解决 "Method ignores results of InputStream.skip()"的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-30 08:39:57 30 4
gpt4 key购买 nike

我有以下代码:

final Response res = this.origin.act(req);
for (int count = req.body().available(); count > 0;
count = req.body().available()) {
body().skip(count);
}
return res;

FindBugs 在 body().skip(count) 中报告了这个问题:

FindBugs: L B RR: ignores result of java.io.InputStream.skip(long)

解决这个问题的最佳方法是什么?

谢谢

最佳答案

This method ignores the return value of java.io.InputStream.skip() which can skip multiple bytes. If the return value is not checked, the caller will not be able to correctly handle the case where fewer bytes were skipped than the caller requested. This is a particularly insidious kind of bug, because in many programs, skips from input streams usually do skip the full amount of data requested, causing the program to fail only sporadically. With Buffered streams, however, skip() will only skip data in the buffer, and will routinely fail to skip the requested number of bytes.

为了避免 findbugs 警告,从而掩盖忽略返回值的错误,您需要检查返回值是否与您请求跳过的数字相匹配;例如

final Response res = this.origin.act(req);
for (int count = req.body().available(); count > 0;
count = req.body().available()) {
long skippedBytes = body().skip(count);
if (skippedBytes != count) {
// do something
}
}
return res;

如果它们不匹配,您应该做的“某事”是您需要根据使用情况做出的选择;你可能想抛出一个异常,你可能想登录并继续,或者你可能想执行某种回退等等

关于java - FindBugs:解决 "Method ignores results of InputStream.skip()"的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35914034/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com