This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IO] add handle copy function ( closes #44 )
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?hh | ||
/* | ||
* Copyright (c) 2004-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace HH\Lib\IO; | ||
|
||
use namespace HH\Lib\Str; | ||
|
||
/** | ||
* Copies content from the given $source read handle to the $target write handle | ||
* until the eol of $source is reached. | ||
* | ||
* If `$chunk_size` is `null`, there is no limit on the chunk size - this function will | ||
* copy the content of $source all at once. | ||
*/ | ||
async function copy( | ||
<<__AcceptDisposable>> ReadHandle $source, | ||
<<__AcceptDisposable>> WriteHandle $target, | ||
?int $chunk_size = null, | ||
?float $timeout_seconds = null, | ||
): Awaitable<bool> { | ||
if (!$source->isEndOfFile()) { | ||
$content = await $source->readAsync($chunk_size, $timeout_seconds); | ||
await $target->writeAsync($content, $timeout_seconds); | ||
} | ||
|
||
if (!$source->isEndOfFile()) { | ||
await copy($source, $target, $chunk_size, $timeout_seconds); | ||
} | ||
|
||
return true; | ||
} |