我有以下代码:
if (maxLength>=0 && (++totalLength > maxLength))
throw new IllegalStateException("Form too large");
在从字节数组输入流中读取字节的循环中。 maxLength
设置为 Integer.MAX_VALUE
所以我认为条件永远不会为真(而且我不是在谈论输入流中字节数组的大小我绝对确定时间不够长)。但是我从该行抛出了 IllegalStateException
!现在真正令人失望的是,当我在该 throw 线上放置一个断点时,一切正常。这怎么可能?
编辑:
- 两个变量都是int类型
- totalLength为局部变量,maxLength为参数
- 当该抛出行上有断点时,调试器不会停在那里并且根本不会抛出异常。
- 我其实不知道为什么我怀疑并行性,只是因为它是 web 应用程序
- 我承认使用 MAX_VALUE 是非常冒险的(在下一步中我将尝试降低此限制),但我希望除了 if 语句的成功分支中的执行之外还有一些其他的执行。而且输入流中使用的字节数组真的不够长。这在 JVM 中显然是不可能的情况:-)。
上面的代码在 UrlEncoded 类的 jetty-util-7.1.5.v20100705.jar 中,我通过调用来使用它
byte[] decodedBytes; byte[] encodedBytes; // v pripade url encoded requestu je potreba pouze odriznout // jmeno falesneho parametru nesouciho kodovany blok encodedBytes = Arrays.copyOfRange(content, "encdata=".length(), content.length); decodedBytes = decodeBytes(request, encodedBytes); // priprav desifrovany text jako vstupni proud decodedInputStream = new ByteArrayInputStream(decodedBytes); // pokud je request url encoded je potreba jej manualne parsovat // pro potreby funkci vracejicich parametry UrlEncoded.decodeTo(decodedInputStream, parameters, request.getCharacterEncoding(), Integer.MAX_VALUE);
最佳答案
Are there any parallel issues with Integer.MAX_VALUE
不,这很可能不是由于任何竞争条件(除非您有其他线程修改 maxLength
左右)。
根据 JLS 上的整数大于 Integer.MAX_VALUE
所以这要么是由于您的 VM 中的错误,要么是基于 maxLength
确实是的错误假设Integer.MAX_VALUE
。
The maxLength is set to Integer.MAX_VALUE so I think that the condition could never be true
确保 totalLength
不是 long
。
(下面的代码片段总是抛出异常。)
int maxLength = Integer.MAX_VALUE;
long totalLength = 2147483647;
if (maxLength>=0 && (++totalLength > maxLength))
throw new IllegalStateException("Form too large");
如果调试器无法重现它,只需为异常提供更多信息性错误消息,例如:“Form too large: "+ totalLength + "is larger than "+ maxLength
关于java - Integer.MAX_VALUE 是否存在任何并行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5181880/