【发布时间】:2021-06-16 15:24:51
【问题描述】:
当尝试从/dev/input/event16 读取 input_events 时,我注意到我正在读取的缓冲区的大小可能会导致异常。这是我写的代码:
public static void main(String[] args) throws IOException{
FileInputStream is = new FileInputStream("/dev/input/event16");
byte[] three_bytes = new byte[3];
byte[] twentyfour_bytes = new byte[24];
is.read(three_bytes); // fails
is.read(twentyfour_bytes); // does not fail
}
我最初的实验表明缓冲区需要至少一个完整的input_event 结构的容量。但我找不到原因。
问题是is.read(three_bytes); 行导致以下异常:
Exception in thread "main" java.io.IOException: Invalid argument
at java.base/java.io.FileInputStream.readBytes(Native Method)
at java.base/java.io.FileInputStream.read(FileInputStream.java:249)
at main.Test.main(Test.java:11)
我想弄清楚为什么 is.read(three_bytes); 行会抛出异常,而is.read(twentyfour_bytes); 会按预期读取数据
【问题讨论】:
-
我不确定我是否理解正确。您想以编程方式获取
/dev/input/event16的大小吗? -
不,我尝试读取文件的内容
-
我仍然不清楚你会发现什么。看来你可以用 24 字节数组读取文件,为什么不直接使用更大的数组呢?
-
我很好奇为什么我需要更大的数组。
标签: java linux-device-driver inputstream input-devices