File.writeChunk
func File.writeChunk(f: File.Handle, bytes: Bytes): Result[void, Error]File.writeChunk writes bytes to f as they are.
Details
- The bytes go out exactly as they are: no encoding, no line end added. For text, use File.writeText or File.writeLine.
- Before it returns,
File.writeChunkhands all of the bytes to the OS, or isErr; nothing needs a flush. On a handle from File.buffered the bytes go into its buffer instead. - A write after File.close is
Err.
Basic examples
Write bytes to a new file, then read them back:
let f = try File.create("out.bin")
defer { File.close(f) }
try File.writeChunk(f, Bytes.fromArray([0x89, 0x50, 0x4e, 0x47]))
try f.close()
println(try File.readBytes("out.bin"))Bytes(89 50 4e 47)Each call writes after the last one, so a file can be built a piece at a time:
let f = try File.create("parts.txt")
defer { File.close(f) }
for part in ["ant", "bee", "cat"] {
try File.writeChunk(f, "${part}\n".toBytes())
}
try f.close()
print(try File.read("parts.txt"))ant
bee
catScope
Bytes that are not valid UTF-8 are written as they are:
let f = try File.create("raw.bin")
defer { File.close(f) }
try f.writeChunk(Bytes.fromArray([0xff, 0x00, 0x41]))
try f.close()
let bytes = try File.readBytes("raw.bin")
println(bytes)
println(String.fromBytes(bytes))Bytes(ff 00 41)
nullOn a handle from File.append, the bytes go after what the file holds:
let f = try File.append("log.txt")
defer { File.close(f) }
try f.writeChunk("second\n".toBytes())
try f.close()
print(try File.read("log.txt"))first
secondStandard output is a handle too:
try Os.stdout().writeChunk("héllo\n".toBytes())hélloA connection from Net.dial is a handle, and the peer reads the bytes with File.readExactly:
let listener = try Net.listen("127.0.0.1:0")
defer { Net.close(listener) }
let received: Channel[Bytes | null] = Channel.new()
go {
let peer = Net.accept(listener).unwrap()
defer { File.close(peer) }
Channel.send(received, peer.readExactly(3).unwrap())
}
let conn = try Net.dial(listener.address())
defer { File.close(conn) }
try conn.writeChunk(Bytes.fromArray([1, 2, 3]))
println(Channel.receive(received))Bytes(01 02 03)On a handle from File.buffered, the bytes wait in the buffer until a flush or a close:
let out = File.buffered(try File.create("held.bin"))
defer { File.close(out) }
try out.writeChunk("held".toBytes())
println(try File.readBytes("held.bin") | Bytes.length())
try out.flush()
println(try File.readBytes("held.bin") | Bytes.length())0
4Applications
Copy a file of any size through a small buffer, with File.readChunk:
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 lineWrite length-prefixed messages: one byte of length, then the message, so a message is at most 255 bytes. The reader takes the length with File.readExactly and then exactly that many bytes:
func writeMessage(f: File.Handle, text: string): Result[void, Error] {
let body = text.toBytes()
if body.length() > 255 {
return Result.fail("message too long")
}
try f.writeChunk(Bytes.fromArray([body.length()]))
f.writeChunk(body)
}
let out = try File.create("messages.bin")
defer { File.close(out) }
try writeMessage(out, "hi")
try writeMessage(out, "ant")
try out.close()
let input = try File.open("messages.bin")
defer { File.close(input) }
while true {
let size = try input.readExactly(1) ?? break
let body = try input.readExactly(int(size.toArray()[0])) ?? break
println(String.fromBytes(body))
}hi
antProperties and relations
File.writeChunk of a string's UTF-8 bytes writes what File.writeText writes for the string:
let text = "naïve café"
let a = try File.create("a.txt")
defer { File.close(a) }
try a.writeText(text)
let b = try File.create("b.txt")
defer { File.close(b) }
try b.writeChunk(text.toBytes())
assert(try File.readBytes("a.txt") == try File.readBytes("b.txt"))Writing chunks one after another gives the same file as writing their concatenation at once with File.writeBytes:
let chunks = ["one,".toBytes(), Bytes.fromArray([0x00]), "two".toBytes()]
let f = try File.create("chunks.bin")
defer { File.close(f) }
for chunk in chunks {
try f.writeChunk(chunk)
}
try f.close()
try File.writeBytes("whole.bin", Bytes.concat(chunks))
assert(try File.readBytes("chunks.bin") == try File.readBytes("whole.bin"))Possible issues
A write after File.close is Err:
let f = try File.create("closed.bin")
try f.close()
println(f.writeChunk("late".toBytes()))Err{ error: Error{ message: "the file is closed" } }See also
File.writeText · File.writeLine · File.writeBytes · File.readChunk · File.readExactly · File.buffered · File.pipe
