Class: MessagePackPure::Unpacker
- Inherits:
-
Object
- Object
- MessagePackPure::Unpacker
- Defined in:
- lib/msgpack_pure/unpacker.rb,
lib/msgpack_pure/unpacker.rb
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Instance Method Summary collapse
-
#initialize(io) ⇒ Unpacker
constructor
A new instance of Unpacker.
- #read ⇒ Object
- #read_array(size) ⇒ Object
- #read_hash(size) ⇒ Object
- #unpack_double ⇒ Object
- #unpack_float ⇒ Object
- #unpack_int16 ⇒ Object
- #unpack_int32 ⇒ Object
- #unpack_int64 ⇒ Object
- #unpack_int8 ⇒ Object
- #unpack_uint16 ⇒ Object
- #unpack_uint32 ⇒ Object
- #unpack_uint64 ⇒ Object
- #unpack_uint8 ⇒ Object
Constructor Details
#initialize(io) ⇒ Unpacker
Returns a new instance of Unpacker.
12 13 14 |
# File 'lib/msgpack_pure/unpacker.rb', line 12 def initialize(io) @io = io end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
16 17 18 |
# File 'lib/msgpack_pure/unpacker.rb', line 16 def io @io end |
Instance Method Details
#read ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/msgpack_pure/unpacker.rb', line 18 def read type = self.unpack_uint8 case when (type & 0b10000000) == 0b00000000 # positive fixnum return type when (type & 0b11100000) == 0b11100000 # negative fixnum return (type & 0b00011111) - (2 ** 5) when (type & 0b11100000) == 0b10100000 # fixraw size = (type & 0b00011111) return @io.read(size) when (type & 0b11110000) == 0b10010000 # fixarray size = (type & 0b00001111) return self.read_array(size) when (type & 0b11110000) == 0b10000000 # fixmap size = (type & 0b00001111) return self.read_hash(size) end case type when 0xC0 # nil return nil when 0xC2 # false return false when 0xC3 # true return true when 0xCA # float return self.unpack_float when 0xCB # double return self.unpack_double when 0xCC # uint8 return self.unpack_uint8 when 0xCD # uint16 return self.unpack_uint16 when 0xCE # uint32 return self.unpack_uint32 when 0xCF # uint64 return self.unpack_uint64 when 0xD0 # int8 return self.unpack_int8 when 0xD1 # int16 return self.unpack_int16 when 0xD2 # int32 return self.unpack_int32 when 0xD3 # int64 return self.unpack_int64 when 0xDA # raw16 size = self.unpack_uint16 return @io.read(size) when 0xDB # raw32 size = self.unpack_uint32 return @io.read(size) when 0xDC # array16 size = self.unpack_uint16 return self.read_array(size) when 0xDD # array32 size = self.unpack_uint32 return self.read_array(size) when 0xDE # map16 size = self.unpack_uint16 return self.read_hash(size) when 0xDF # map32 size = self.unpack_uint32 return self.read_hash(size) else raise("Unknown Type -- #{'0x%02X' % type}") end end |
#read_array(size) ⇒ Object
87 88 89 |
# File 'lib/msgpack_pure/unpacker.rb', line 87 def read_array(size) return size.times.map { self.read } end |
#read_hash(size) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/msgpack_pure/unpacker.rb', line 91 def read_hash(size) return size.times.inject({}) { |memo,| memo[self.read] = self.read memo } end |
#unpack_double ⇒ Object
139 140 141 |
# File 'lib/msgpack_pure/unpacker.rb', line 139 def unpack_double return @io.read(8).unpack("G")[0] end |
#unpack_float ⇒ Object
135 136 137 |
# File 'lib/msgpack_pure/unpacker.rb', line 135 def unpack_float return @io.read(4).unpack("g")[0] end |
#unpack_int16 ⇒ Object
110 111 112 113 |
# File 'lib/msgpack_pure/unpacker.rb', line 110 def unpack_int16 num = self.unpack_uint16 return (num < 2 ** 15 ? num : num - (2 ** 16)) end |
#unpack_int32 ⇒ Object
119 120 121 122 |
# File 'lib/msgpack_pure/unpacker.rb', line 119 def unpack_int32 num = self.unpack_uint32 return (num < 2 ** 31 ? num : num - (2 ** 32)) end |
#unpack_int64 ⇒ Object
130 131 132 133 |
# File 'lib/msgpack_pure/unpacker.rb', line 130 def unpack_int64 num = self.unpack_uint64 return (num < 2 ** 63 ? num : num - (2 ** 64)) end |
#unpack_int8 ⇒ Object
102 103 104 |
# File 'lib/msgpack_pure/unpacker.rb', line 102 def unpack_int8 return @io.read(1).unpack("c")[0] end |
#unpack_uint16 ⇒ Object
106 107 108 |
# File 'lib/msgpack_pure/unpacker.rb', line 106 def unpack_uint16 return @io.read(2).unpack("n")[0] end |
#unpack_uint32 ⇒ Object
115 116 117 |
# File 'lib/msgpack_pure/unpacker.rb', line 115 def unpack_uint32 return @io.read(4).unpack("N")[0] end |
#unpack_uint64 ⇒ Object
124 125 126 127 128 |
# File 'lib/msgpack_pure/unpacker.rb', line 124 def unpack_uint64 high = self.unpack_uint32 low = self.unpack_uint32 return (high << 32) + low end |
#unpack_uint8 ⇒ Object
98 99 100 |
# File 'lib/msgpack_pure/unpacker.rb', line 98 def unpack_uint8 return @io.read(1).unpack("C")[0] end |