Class: Twilic::Core::Wire::Reader
- Inherits:
-
Object
- Object
- Twilic::Core::Wire::Reader
- Defined in:
- lib/twilic/core/wire.rb
Instance Attribute Summary collapse
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Instance Method Summary collapse
- #eof? ⇒ Boolean
-
#initialize(input) ⇒ Reader
constructor
A new instance of Reader.
- #position ⇒ Object
- #read_bitmap ⇒ Object
- #read_bytes ⇒ Object
- #read_exact(n) ⇒ Object
- #read_f64_le ⇒ Object
- #read_i64_zigzag ⇒ Object
- #read_string ⇒ Object
- #read_u64_le ⇒ Object
- #read_u8 ⇒ Object
- #read_varuint ⇒ Object
Constructor Details
#initialize(input) ⇒ Reader
Returns a new instance of Reader.
59 60 61 62 |
# File 'lib/twilic/core/wire.rb', line 59 def initialize(input) @input = input.b @offset = 0 end |
Instance Attribute Details
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
57 58 59 |
# File 'lib/twilic/core/wire.rb', line 57 def offset @offset end |
Instance Method Details
#eof? ⇒ Boolean
68 69 70 |
# File 'lib/twilic/core/wire.rb', line 68 def eof? @offset >= @input.bytesize end |
#position ⇒ Object
64 65 66 |
# File 'lib/twilic/core/wire.rb', line 64 def position @offset end |
#read_bitmap ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/twilic/core/wire.rb', line 120 def read_bitmap bit_count = read_varuint byte_count = (bit_count + 7) / 8 bytes = read_exact(byte_count) bits = Array.new(bit_count) bit_count.times do |i| bits[i] = ((bytes.getbyte(i / 8) >> (i % 8)) & 1) == 1 end bits end |
#read_bytes ⇒ Object
107 108 109 110 |
# File 'lib/twilic/core/wire.rb', line 107 def read_bytes n = read_varuint read_exact(n) end |
#read_exact(n) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/twilic/core/wire.rb', line 80 def read_exact(n) raise Errors.unexpected_eof if @offset + n > @input.bytesize slice = @input.byteslice(@offset, n) @offset += n slice end |
#read_f64_le ⇒ Object
136 137 138 |
# File 'lib/twilic/core/wire.rb', line 136 def read_f64_le [read_u64_le].pack("Q<").unpack1("E") end |
#read_i64_zigzag ⇒ Object
102 103 104 105 |
# File 'lib/twilic/core/wire.rb', line 102 def read_i64_zigzag encoded = read_varuint Wire.decode_zigzag(encoded) end |
#read_string ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/twilic/core/wire.rb', line 112 def read_string n = read_varuint bytes = read_exact(n) raise Errors.utf8_error unless bytes.valid_encoding? && bytes.force_encoding(Encoding::UTF_8).valid_encoding? bytes.force_encoding(Encoding::UTF_8) end |
#read_u64_le ⇒ Object
131 132 133 134 |
# File 'lib/twilic/core/wire.rb', line 131 def read_u64_le b = read_exact(8) b.unpack1("Q<") end |
#read_u8 ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/twilic/core/wire.rb', line 72 def read_u8 raise Errors.unexpected_eof if @offset >= @input.bytesize b = @input.getbyte(@offset) @offset += 1 b end |
#read_varuint ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/twilic/core/wire.rb', line 88 def read_varuint shift = 0 result = 0 loop do raise Errors.invalid_data("varuint too large") if shift >= 64 b = read_u8 result |= (b & 0x7F) << shift return result if (b & 0x80).zero? shift += 7 end end |