Torrent::pieces PHP Method

pieces() private method

Build pieces depending on piece length from a file handler
private pieces ( $handle, $piece_length, $last = true ) : string
return string pieces
    private function pieces($handle, $piece_length, $last = true)
    {
        static $piece, $length;
        if (empty($length)) {
            $length = $piece_length;
        }
        $pieces = null;
        while (!feof($handle)) {
            if (($length = strlen($piece .= fread($handle, $length))) == $piece_length) {
                $pieces .= self::pack($piece);
            } elseif (($length = $piece_length - $length) < 0) {
                return self::set_error(new Exception('Invalid piece length!'));
            }
        }
        fclose($handle);
        return $pieces . ($last && $piece ? self::pack($piece) : null);
    }