// Functions for interacting with `io.Reader` & `io.Writer` objects. module util import io import os // reader_to_writer tries to consume the entire reader & write it to the writer. pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ! { mut buf := []u8{len: 8192} for { bytes_read := reader.read(mut buf) or { break } mut bytes_written := 0 for bytes_written < bytes_read { c := writer.write(buf[bytes_written..bytes_read]) or { break } bytes_written += c } } } // reader_to_file writes the contents of a BufferedReader to a file pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ! { mut file := os.create(path)! defer { file.close() } mut buf := []u8{len: 8192} mut bytes_left := length // Repeat as long as the stream still has data for bytes_left > 0 { // TODO check if just breaking here is safe bytes_read := reader.read(mut buf) or { break } bytes_left -= bytes_read mut to_write := bytes_read for to_write > 0 { // TODO don't just loop infinitely here bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue } // file.flush() to_write = to_write - bytes_written } } }