Sha256: c1b473fb4e25b8b9cc677c384651345d779e5e0ff0352eaf698af37b7ccc96a5

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

# encoding: binary

require 'amq/endianness'

# Ruby doesn't support pack to/unpack from
# 64bit string in network byte order.
module AMQ
  module Hacks
    UINT64 = "Q".freeze
    INT16  = "c".freeze

    if Endianness.big_endian?
      def self.pack_uint64_big_endian(long_long)
        [long_long].pack(UINT64)
      end

      def self.unpack_uint64_big_endian(data)
        data.unpack(UINT64)
      end

      def self.pack_int16_big_endian(short)
        [long_long].pack(INT16)
      end

      def self.unpack_int16_big_endian(data)
        data.unpack(INT16)
      end
    else
      def self.pack_uint64_big_endian(long_long)
        result = [long_long].pack(UINT64)
        result.bytes.to_a.reverse.map(&:chr).join
      end

      def self.unpack_uint64_big_endian(data)
        data = data.bytes.to_a.reverse.map(&:chr).join
        data.unpack(UINT64)
      end

      def self.pack_int16_big_endian(short)
        result = [long_long].pack(INT16)
        result.bytes.to_a.reverse.map(&:chr).join
      end

      def self.unpack_int16_big_endian(data)
        data = data.bytes.to_a.reverse.map(&:chr).join
        data.unpack(INT16)
      end
    end
  end
end

# AMQ::Hacks.pack_uint64_big_endian(17)
# AMQ::Hacks.unpack_uint64_big_endian("\x00\x00\x00\x00\x00\x00\x00\x11")

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
amq-protocol-1.5.0 lib/amq/hacks.rb
amq-protocol-1.4.0 lib/amq/hacks.rb
amq-protocol-1.3.0 lib/amq/hacks.rb