hsl-experimental
hsl-experimental copied to clipboard
[IO] add stream/handle copy function
while using HH\Lib\Experimental\IO
i came across multiple situations where i needed to copy a read handle content to a write handle content.
example :
- moving uploaded file content ( from temporary file )
- writing response body to
IO\request_output
(php://memory
->php://output
)
the propose of this issue is to propose adding a copy
function to copy a read handle content to a write handle.
copy
pseudo implementation :
function copy(
IO\ReadHandle $from, IO\WriteHandle $to, int $maxLength = 8192
): Awaitable<int> {
while from.isNotEof
content = from.read max_length
to.write content
}
copy_range
pseudo implementation :
function copy_range(
IO\ReadHandle $from, IO\WriteHandle $to, int $length, int $maxLength = 8192
): Awaitable<void> {
while length >= max_length and from.isNotEof
content = from.read max_length
length -= content.length
to.write content
if length > 0 and from.isNotEof
content = from.read length
to.write content
}
edited title to make it clear that this is distinct to a Filesystem\copy or OS\copy, covered by #29