File: hacks.rb

package info (click to toggle)
ruby-amq-protocol 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 392 kB
  • sloc: ruby: 4,212; python: 247; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 778 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# encoding: binary

# Ruby doesn't support pack to/unpack from
# 64bit string in network byte order.
module AMQ
  module Hacks
    BIG_ENDIAN = ([1].pack("s") == "\x00\x01")
    Q = "Q".freeze

    if BIG_ENDIAN
      def self.pack_64_big_endian(long_long)
        [long_long].pack(Q)
      end

      def self.unpack_64_big_endian(data)
        data.unpack(Q)
      end
    else
      def self.pack_64_big_endian(long_long)
        result = [long_long].pack(Q)
        result.bytes.to_a.reverse.map(&:chr).join
      end

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

# AMQ::Hacks.pack_64_big_endian(17)
# AMQ::Hacks.unpack_64_big_endian("\x00\x00\x00\x00\x00\x00\x00\x11")