# Copyright (c) 2023 Yuki Kimoto
# MIT License
class File::Copy {
version "0.023";
use Sys;
use IO::File;
static method copy : void ($from : string, $to : string, $size : int = -1) {
my $from_io_file = IO::File->new($from, "<");
my $to_io_file = IO::File->new($to, ">");
my $fstat_from = Sys->fstat($from_io_file->fileno);
my $size_max = 1024 * 1024 * 2;
if ($size < 0) {
my $st_size = $fstat_from->st_size;
if ($st_size < 512) {
$size = 1024;
}
if ($st_size > $size_max) {
$size = $size_max;
}
}
my $buffer = (mutable string)new_string_len $size;
my $offset = 0;
while (1) {
my $read_length = $from_io_file->read($buffer, $size);
if ($read_length == 0) {
last;
}
$to_io_file->write($buffer, $read_length);
}
$from_io_file->close;
$to_io_file->close;
}
static method move : void ($from : string, $to : string) {
Sys->rename($from, $to);
}
}