File.readChunk
func File.readChunk(f: File.Handle, max: int, ctx: Context | null = null): Result[Bytes | null, Error]File.readChunk reads at least 1 and at most max bytes from f, returning as soon as there are any. It returns null at end of file and is Err when max is below 1. It is the read for a stream whose pieces have no fixed size.
Details
File.readChunkreturns as soon as there are any bytes, so a chunk can be shorter thanmaxeven before the end. Use File.readExactly when you need a given number of bytes.- At the end of the input it is
Okwithnull. - It shares the handle's read buffer with File.readLine and the other reads, so it continues where the last read stopped.
- With
ctx, a read of a connection, a pipe, or a terminal gives up when the context is done, and loses nothing. A read of a regular file looks at the context only before it begins.
Basic examples
Read a file at most 4 bytes at a time, until File.readChunk returns null:
let f = try File.open("hello.txt")
defer { File.close(f) }
while true {
let chunk = try File.readChunk(f, 4) ?? break
println(chunk)
}Bytes(48 65 6c 6c)
Bytes(6f 2c 20 41)
Bytes(6e 74 0a)At the end of the file, the read is Ok with null:
let f = try File.open("one.txt")
defer { File.close(f) }
println(File.readChunk(f, 10))
println(File.readChunk(f, 10))Ok{ value: Bytes(78 0a) }
Ok{ value: null }Scope
An empty file gives null on the first read:
let f = try File.open("empty.txt")
defer { File.close(f) }
println(f.readChunk(1024))Ok{ value: null }The bytes come back as they are, including bytes that are not valid UTF-8:
try File.writeBytes("raw.bin", Bytes.fromArray([0xff, 0x00, 0x41]))
let f = try File.open("raw.bin")
defer { File.close(f) }
println(try f.readChunk(16))Bytes(ff 00 41)A chunk read continues after a line read on the same handle. Read a header line as text, then the body as bytes:
let f = try File.open("message.txt")
defer { File.close(f) }
println(try f.readLine())
println(try f.readChunk(100))SIZE 3
Bytes(61 62 63 0a)On a connection, File.readChunk returns what has arrived and does not wait for max bytes. Here the peer sends ab, and sends cd only after the first chunk is read:
let listener = try Net.listen("127.0.0.1:0")
defer { Net.close(listener) }
let next: Channel[bool] = Channel.new()
go {
let peer = Net.accept(listener).unwrap()
defer { File.close(peer) }
peer.writeText("ab").unwrap()
Channel.receive(next)
peer.writeText("cd").unwrap()
}
let conn = try Net.dial(listener.address())
defer { File.close(conn) }
println(try conn.readChunk(4))
Channel.send(next, true)
println(try conn.readChunk(4))
println(conn.readChunk(4))Bytes(61 62)
Bytes(63 64)
Ok{ value: null }Standard input is a handle too:
println(try Os.stdin().readChunk(1024))Bytes(70 69 70 65 64 20 69 6e 0a)Named arguments
ctx bounds the wait for a connection that sends nothing. The read is Err with the context's reason, and a later read still gets what arrives:
let listener = try Net.listen("127.0.0.1:0")
defer { Net.close(listener) }
go {
let peer = Net.accept(listener).unwrap()
defer { File.close(peer) }
Time.sleep(200ms)
peer.writeLine("late").unwrap()
}
let conn = try Net.dial(listener.address())
defer { File.close(conn) }
let ctx = Context.new(timeout: 50ms)
println(conn.readChunk(100, ctx: ctx))
println(try conn.readChunk(100))Err{ error: Error{ message: "timeout" } }
Bytes(6c 61 74 65 0a)A regular file never waits, but a context that is already done stops the read before it begins:
let f = try File.open("data.txt")
defer { File.close(f) }
let ctx = Context.new()
ctx.cancel()
println(f.readChunk(4, ctx: ctx))Err{ error: Error{ message: "cancelled" } }Applications
Copy a file of any size through a small buffer with File.writeChunk:
func copyFile(from: string, to: string): Result[void, Error] {
let src = try File.open(from)
defer { File.close(src) }
let dst = try File.create(to)
defer { File.close(dst) }
while true {
let chunk = try src.readChunk(8) ?? break
try dst.writeChunk(chunk)
}
dst.close()
}
try copyFile("source.txt", "copy.txt")
print(try File.read("copy.txt"))first line
second lineCount the bytes and the lines of standard input without holding it in memory:
var bytes = 0
var lines = 0
let input = Os.stdin()
while true {
let chunk = try input.readChunk(4096) ?? break
bytes += chunk.length()
lines += chunk.toArray().countWhere({ x == byte(10) })
}
println("${lines} lines, ${bytes} bytes")3 lines, 14 bytesProperties and relations
The chunks of a file, joined with Bytes.concat, are the bytes File.readBytes gives, and each chunk holds 1 to max bytes:
let f = try File.open("text.txt")
defer { File.close(f) }
let chunks: Array[Bytes] = []
while true {
let chunk = try f.readChunk(5) ?? break
assert(chunk.length() >= 1 && chunk.length() <= 5)
chunks.push(chunk)
}
assert(Bytes.concat(chunks) == try File.readBytes("text.txt"))Possible issues
A max below 1 is Err, not an empty chunk:
let f = try File.open("small.txt")
defer { File.close(f) }
println(f.readChunk(0))Err{ error: Error{ message: "the most to read is 0; it must be at least 1" } }A chunk ends at a byte count, not at a character, so it can split a multi-byte character. Decoding each chunk on its own then fails; collect the chunks and decode them together:
let f = try File.open("accent.txt")
defer { File.close(f) }
let chunks: Array[Bytes] = []
while true {
let chunk = try f.readChunk(4) ?? break
println("${chunk}: ${String.fromBytes(chunk)}")
chunks.push(chunk)
}
println(String.fromBytes(Bytes.concat(chunks)))Bytes(63 61 66 c3): null
Bytes(a9 0a): null
caféSee also
File.readExactly · File.readLine · File.readAll · File.readBytes · File.writeChunk · File.pipe · Net.dial
