hsl-experimental icon indicating copy to clipboard operation
hsl-experimental copied to clipboard

[IO] add stream/handle copy function

Open azjezz opened this issue 5 years ago • 1 comments

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
}

azjezz avatar Apr 12 '19 12:04 azjezz

edited title to make it clear that this is distinct to a Filesystem\copy or OS\copy, covered by #29

fredemmott avatar Oct 28 '19 19:10 fredemmott