简体   繁体   English

从流中读取时出现IndexOutOfBoundsException

[英]IndexOutOfBoundsException when reading from stream

I wrote a Java method to send an instruction to a remote device via serial port and get a known number of bytes as the answer. 我编写了Java方法,以通过串行端口将指令发送到远程设备,并获得已知的字节数作为答案。 The code runs on RaspberryPi, using librxtx-java library. 该代码使用librxtx-java库在RaspberryPi上运行。 The remote device was verified to send the answer of expected length. 已验证远程设备已发送预期长度的答案。

The code below is the last part of this method where RaspberryPi waits for all the bytes of the answer for up to a given time "t_max". 下面的代码是该方法的最后一部分,其中RaspberryPi在给定时间“ t_max”内等待答案的所有字节。

The code as it is throws an IndexOutOfBoundsException during System.arraycopy . 代码本身在System.arraycopy期间引发IndexOutOfBoundsException。 If I wrap the arraycopy instruction by try...catch and print out the pointer variable at catch, there is indeed an index overflow. 如果我通过try ... catch包装arraycopy指令并在catch处打印出指针变量,则确实存在索引溢出。

However, if I uncomment the line which prints out the pointer value, there is no more exception. 但是,如果取消注释打印出指针值的行,就不会再有例外了。 Even replacing this line by System.out.println("X"); 甚至用System.out.println("X");替换此行System.out.println("X"); makes the exception gone, but not does the System.out.print("X"); 使异常消失,但System.out.print("X");则不这样做System.out.print("X"); for example. 例如。

I tried changing the variables to volatile but no more luck. 我尝试将变量更改为volatile,但是没有运气了。 How can printing out to terminal change the value of a variable? 如何在终端打印输出以更改变量的值?

long t0 = System.currentTimeMillis();
long t = t0;
byte[] answer = new byte[answerLength];
byte[] readBuffer = new byte[answerLength];
int numBytes = 0;
int answerPointer = 0;
while (t - t0 < t_max) {
    try {
        if (inputStream.available() > 0) {
            numBytes = inputStream.read(readBuffer);
        }
    } catch (Exception e) {
    }

    if (numBytes > 0) {
        // System.out.println("answerPointer="+answerPointer);
        System.arraycopy(readBuffer, 0, answer, answerPointer, numBytes);
        answerPointer = answerPointer + numBytes;
    }

    if (answerPointer == answerLength) {
        return (answer);
    }

    t = System.currentTimeMillis();
}

Have you tried verifying if the output stream and input stream are linked in any way? 您是否尝试验证输出流和输入流是否以任何方式链接? May be the input stream is reading from the output-stream and '\\n' (new line) is being used as the end of stream character. 可能是输入流正在从输出流中读取,并且'\\ n'(换行)被用作流字符的结尾。 Can you try printing out to a print-stream wrappend around byte-array-output-stream instead of standard-out and see if doing a ps.println("X") causes an exception? 您可以尝试打印到围绕字节数组输出流而不是标准输出的打印流包装中,并查看执行ps.println(“ X”)是否导致异常吗? If it does cause an exception then possibly the standard output and input stream are linked and that is why doing a System.out.println("X") makes the exception go away. 如果确实引起异常,则可能将标准输出流和输入流链接在一起,这就是为什么执行System.out.println(“ X”)会使异常消失的原因。

Also, volatile keyword is used in the context of threads. 另外,在线程上下文中使用volatile关键字。 It will not have any effect in a single thread environment. 它在单线程环境中不会有任何影响。

If the code inputStream.available() throws an exception on second iteration of while (t - t0 < t_max) variables numBytes and readBuffer stay initialized with old values. 如果代码inputStream.available()while (t - t0 < t_max)变量numBytesreadBuffer第二次迭代中引发异常, while (t - t0 < t_max)使用旧值进行初始化。 Try to wrap all code in block while (t - t0 < t_max) into try {} catch {} and don't hide an exception. 尝试将while (t - t0 < t_max)所有代码包装在try {} catch {} ,不要隐藏异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM