<?php


namespace Mainto\RpcServer\Util\IO;


use Mainto\RpcServer\Util\Bytes\Bytes;
use RuntimeException;

class IOUtil {
    public static function readAll (Reader $reader): string {
        $buffer = Bytes::newBuffer();

        $buffer->readFrom($reader);

        return $buffer->bytes();
    }

    public static function readFullSize(Reader $reader, $size): string {
        $buf = "";

        $allReader = 0;
        while (true) {
            $read = $reader->read($size - $allReader);

            $buf .= $read;
            $allReader += strlen($read);

            if ($allReader >= $size) {
                return $buf;
            }
        }
    }
}