# frozen_string_literal: true require 'forwardable' module Steam # Reads bytes from a given IO object. class ByteReader extend Forwardable attr_reader :io # Create a ByteReader object # # @param io [:read] an io object def initialize(io) @io = io end # Reads an unsigned 64 bit integer from the stream # # @return [Integer] The 64 bit integer def read_int64 io.read(8).unpack('C*').each_with_index.reduce(0) do |sum, (byte, index)| sum + byte * (256**index) end end alias int64 read_int64 # Reads an unsigned short from the stream # # @return [Integer] The short def read_short io.read(2).unpack('S*').first end alias short read_short # Reads a string of a given length from the stream # # @return [String] The read string def string(len) io.read(len) end # Reads a single bytes from the stream # # @return [Integer] The byte def byte io.read(1).ord end # Reads an unsigned 32 bit integer from the stream # # @return [Integer] The 32 bit integer def unsigned_int32 io.read(4).unpack('