基本上,我为自己编写了一些 InputStream(进一步用作 System.in),它接受并使用 Swing 组件加载的缓冲区。注意:出于调试目的插入了打印语句。read() 方法:@Overridepublic int read() { System.out.print("[READ] Querying buffer... "); while(this.buffer.isEmpty()); int val = buffer.poll(); // Consumes the first element System.out.format("done - 0x%x\n", val); return val;}从 WORKS 流中读取原始字节:InputStream input = new TestStream();int chunk = 0;while(chunk != '\n') { chunk = input.read(); System.out.format("Byte: 0x%x", chunk);}input.close();输出:[Read] Querying buffer... done - 0x4cByte: 0x4c...Byte: 0x a这0x a是 NL 字符(是的,我在类 Unix 系统上)它在为流提供额外数据时输出消耗的字符,并在没有时阻塞。但这不起作用:InputStream input = new TestStream();Scanner scn = new Scanner(input);System.out.println("Line: " + scn.nextLine());scn.close();input.close();这种方法简单地消耗输入流中的所有可用数据,并阻塞直到到达流的末尾( read() 返回 -1 )。它似乎完全忽略了流中的 NL 字符,或者 NL 以错误的方式插入缓冲区(我非常怀疑是后者)如果出现问题,您认为是什么原因?
1 回答
弑天下
TA贡献1818条经验 获得超8个赞
k5_ 已经在他的评论中指出了答案:
扫描仪不使用
read()
方法。它使用read(byte[])
,您是否覆盖了它?
但没有将其重写为答案,所以我这样做了。所有功劳都归功于他。
添加回答
举报
0/150
提交
取消