Class: Kafka::Protocol::Decoder
- Inherits:
-
Object
- Object
- Kafka::Protocol::Decoder
- Defined in:
- lib/kafka/protocol/decoder.rb
Overview
A decoder wraps an IO object, making it easy to read specific data types from it. The Kafka protocol is not self-describing, so a client must call these methods in just the right order for things to work.
Instance Method Summary collapse
-
#array(&block) ⇒ Array
Decodes an array from the IO object.
-
#bytes ⇒ String
Decodes a list of bytes from the IO object.
-
#initialize(io) ⇒ Decoder
constructor
Initializes a new decoder.
-
#int16 ⇒ Integer
Decodes a 16-bit integer from the IO object.
-
#int32 ⇒ Integer
Decodes a 32-bit integer from the IO object.
-
#int64 ⇒ Integer
Decodes a 64-bit integer from the IO object.
-
#int8 ⇒ Integer
Decodes an 8-bit integer from the IO object.
-
#read(number_of_bytes) ⇒ String
Reads the specified number of bytes from the IO object, returning them as a String.
-
#string ⇒ String
Decodes a string from the IO object.
Constructor Details
#initialize(io) ⇒ Decoder
Initializes a new decoder.
12 13 14 |
# File 'lib/kafka/protocol/decoder.rb', line 12 def initialize(io) @io = io end |
Instance Method Details
#array(&block) ⇒ Array
Decodes an array from the IO object.
The provided block will be called once for each item in the array. It is the responsibility of the block to decode the proper type in the block, since there's no information that allows the type to be inferred automatically.
52 53 54 55 |
# File 'lib/kafka/protocol/decoder.rb', line 52 def array(&block) size = int32 size.times.map(&block) end |
#bytes ⇒ String
Decodes a list of bytes from the IO object.
73 74 75 76 77 78 79 80 81 |
# File 'lib/kafka/protocol/decoder.rb', line 73 def bytes size = int32 if size == -1 nil else read(size) end end |
#int16 ⇒ Integer
Decodes a 16-bit integer from the IO object.
26 27 28 |
# File 'lib/kafka/protocol/decoder.rb', line 26 def int16 read(2).unpack("s>").first end |
#int32 ⇒ Integer
Decodes a 32-bit integer from the IO object.
33 34 35 |
# File 'lib/kafka/protocol/decoder.rb', line 33 def int32 read(4).unpack("l>").first end |
#int64 ⇒ Integer
Decodes a 64-bit integer from the IO object.
40 41 42 |
# File 'lib/kafka/protocol/decoder.rb', line 40 def int64 read(8).unpack("q>").first end |
#int8 ⇒ Integer
Decodes an 8-bit integer from the IO object.
19 20 21 |
# File 'lib/kafka/protocol/decoder.rb', line 19 def int8 read(1).unpack("C").first end |
#read(number_of_bytes) ⇒ String
Reads the specified number of bytes from the IO object, returning them as a String.
87 88 89 |
# File 'lib/kafka/protocol/decoder.rb', line 87 def read(number_of_bytes) @io.read(number_of_bytes) or raise EOFError end |
#string ⇒ String
Decodes a string from the IO object.
60 61 62 63 64 65 66 67 68 |
# File 'lib/kafka/protocol/decoder.rb', line 60 def string size = int16 if size == -1 nil else read(size) end end |