Implement a new `readByte()` static method on the `Stdin` class that reads one byte from standard input, blocking the current fiber until a byte is received. The method returns the byte value as a number or `null` if stdin is closed. Refactor the internal buffering mechanism by introducing a shared `read_()` helper method that both `readByte()` and `readLine()` use, replacing the previous per-method buffering logic. The `readByte()` implementation peels off the first byte from the internal `__buffered` string using `bytes[0]` and slicing. Add two test files: `read_byte.wren` verifying byte-by-byte reading of "first" and "second" input lines, and `read_byte_eof.wren` confirming that reading after stdin is closed returns the expected error message.
22 lines
291 B
Plaintext
22 lines
291 B
Plaintext
import "io" for Stdin
|
|
|
|
for (i in 1...13) {
|
|
System.print(Stdin.readByte())
|
|
}
|
|
|
|
// stdin: first
|
|
// expect: 102
|
|
// expect: 105
|
|
// expect: 114
|
|
// expect: 115
|
|
// expect: 116
|
|
// expect: 10
|
|
|
|
// stdin: second
|
|
// expect: 115
|
|
// expect: 101
|
|
// expect: 99
|
|
// expect: 111
|
|
// expect: 110
|
|
// expect: 100
|