-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
63 lines (56 loc) · 1.24 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
/**
* @project Castor IO
* @link https://github.com/castor-labs/io
* @project castor/io
* @author Matias Navarro-Carter [email protected]
* @license BSD-3-Clause
* @copyright 2022 Castor Labs Ltd
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Castor\Io;
/**
* Reads from a reader until EndOfFile is reached and puts all the contents into
* memory.
*
* @psalm-param positive-int $chunk
*
* @throws Error
*/
function readAll(Reader $reader, int $chunk = 4096): string
{
$contents = '';
while (true) {
try {
$contents .= $reader->read($chunk);
} catch (EndOfFile $e) {
break;
}
}
return $contents;
}
/**
* Copies bytes from a reader to a writer.
*
* @psalm-param positive-int $chunk
*
* @return int The amount of bytes copied
*
* @throws Error
*/
function copy(Reader $reader, Writer $writer, int $chunk = 4096): int
{
$copied = 0;
while (true) {
try {
$bytes = $reader->read($chunk);
$copied += $writer->write($bytes);
} catch (EndOfFile $e) {
break;
}
}
return $copied;
}