Class: Protocol::MQTT::Codec::Reader
- Inherits:
-
Object
- Object
- Protocol::MQTT::Codec::Reader
- Defined in:
- lib/protocol/mqtt/codec.rb
Overview
Cursor reader over a byte string.
Instance Attribute Summary collapse
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
- #eof? ⇒ Boolean
-
#initialize(bytes) ⇒ Reader
constructor
A new instance of Reader.
- #read(n) ⇒ Object
- #read_binary ⇒ Object
- #read_rest ⇒ Object
- #read_string_pair ⇒ Object
- #read_u16 ⇒ Object
- #read_u32 ⇒ Object
- #read_u8 ⇒ Object
- #read_utf8 ⇒ Object
- #read_vbi ⇒ Object
- #remaining ⇒ Object
Constructor Details
#initialize(bytes) ⇒ Reader
Returns a new instance of Reader.
17 18 19 20 |
# File 'lib/protocol/mqtt/codec.rb', line 17 def initialize(bytes) @bytes = bytes.b @pos = 0 end |
Instance Attribute Details
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
14 15 16 |
# File 'lib/protocol/mqtt/codec.rb', line 14 def pos @pos end |
Instance Method Details
#eof? ⇒ Boolean
28 29 30 |
# File 'lib/protocol/mqtt/codec.rb', line 28 def eof? @pos >= @bytes.bytesize end |
#read(n) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/protocol/mqtt/codec.rb', line 33 def read(n) raise MalformedPacket, "truncated body: need #{n}, have #{remaining}" if remaining < n chunk = @bytes.byteslice(@pos, n) @pos += n chunk end |
#read_binary ⇒ Object
67 68 69 70 |
# File 'lib/protocol/mqtt/codec.rb', line 67 def read_binary len = read_u16 read(len) end |
#read_rest ⇒ Object
78 79 80 |
# File 'lib/protocol/mqtt/codec.rb', line 78 def read_rest read(remaining) end |
#read_string_pair ⇒ Object
73 74 75 |
# File 'lib/protocol/mqtt/codec.rb', line 73 def read_string_pair [read_utf8, read_utf8] end |
#read_u16 ⇒ Object
46 47 48 |
# File 'lib/protocol/mqtt/codec.rb', line 46 def read_u16 read(2).unpack1("n") end |
#read_u32 ⇒ Object
51 52 53 |
# File 'lib/protocol/mqtt/codec.rb', line 51 def read_u32 read(4).unpack1("N") end |
#read_u8 ⇒ Object
41 42 43 |
# File 'lib/protocol/mqtt/codec.rb', line 41 def read_u8 read(1).unpack1("C") end |
#read_utf8 ⇒ Object
61 62 63 64 |
# File 'lib/protocol/mqtt/codec.rb', line 61 def read_utf8 len = read_u16 read(len).force_encoding(Encoding::UTF_8) end |
#read_vbi ⇒ Object
56 57 58 |
# File 'lib/protocol/mqtt/codec.rb', line 56 def read_vbi VBI.decode(self) end |
#remaining ⇒ Object
23 24 25 |
# File 'lib/protocol/mqtt/codec.rb', line 23 def remaining @bytes.bytesize - @pos end |