Android JAVA serial port read and write available and read function match exception

problem: the project needs to develop 485 serial communication on the android panel. The serial port needs to be monitored after the read command is sent, and the communication is not considered to have failed after N time. Use java"s FutureTask with FileInputStream"s available and read to read the response. But the test found that after available returned num, the content read by read, was 0, which felt like available read the data away. If you block the read directly with read, you can read the response. Available can"t see the source code, he"s confused, and he doesn"t have a clue.
Code: boolean done = TimeoutUtil.execute (new Runnable () {

            @Override
            public void run() {
                try {
                    byte[] bytes = new byte[30];
                    for(int i=0;i<30;iPP){
                        bytes[i] = (byte)i;
                    }
                    while (!complete[0]) {
                        int num;
                        if ((num = input.available()) > 0) {
                            int len = input.read(bytes, 0, num);
                            if (len > 0) {
                                response.write(bytes, 0, len);
                                if (response.size() >= finalExpected_length) {
                                    break;
                                }
                            }
                        }
                        ThreadUtil.sleep(1);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }, timeout);
        
        TimeoutUtilFutureTaskavailable7read0read
Mar.23,2022
Menu