Sha256: 46fa7ab68aa9cc460f0437b7b005d7332dfc54a1fcae4eec07697f404fcc7845

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true
require 'forwardable'

module Steam
  # Writes bytes to a given io object
  class ByteWriter
    extend Forwardable

    # Wrap an Int64 allowing the lo and hi
    # 32 bits to be extracted
    class Int64Type
      # Create an Int64Type instance
      #
      # @param value [Integer] the 64 bit int
      def initialize(value)
        @value = value
      end

      # The low 32 bits
      #
      # @return [Integer]
      def lo
        @value & 0xFFFFFFFF
      end

      # The high 32 bits
      #
      # @return [Integer]
      def hi
        (@value >> 32) & 0xFFFFFFFF
      end

      # An array of the low and high bits
      #
      # @return [Integer]
      def int32s
        [lo, hi]
      end
    end

    def initialize
      @io = StringIO.new
      @io.set_encoding('BINARY')
    end

    # Rewind the stream
    def rewind
      @io.rewind
    end

    # Writes an unsigned 32 bit integer from the stream
    def write_unsigned_int32(value)
      @io.write([value].pack('<I'))
    end

    # Writes a 32 bit intger to the stream
    def write_int32(value)
      @io.write([value].pack('<l'))
    end

    # Writes a 64 bit intger to the stream
    def write_int64(value)
      int64 = Int64Type.new(value)

      int64.int32s.each do |int|
        write_int32(int)
      end
    end

    # Writes the given bytes to the stream
    def write(bytes)
      @io.write(bytes)
    end

    # The byte representation of this writer object
    #
    # @return [String] byte representation of this writer object
    def string
      @io.rewind
      @io.string
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
steamrb-0.1.3 lib/steam/byte_writer.rb
steamrb-0.1.2 lib/steam/byte_writer.rb
steamrb-0.1.1 lib/steam/byte_writer.rb