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.
Class Method Summary collapse
Instance Method Summary collapse
-
#array(&block) ⇒ Array
Decodes an array from the IO object.
-
#boolean ⇒ Boolean
Decodes an 8-bit boolean from the IO object.
-
#bytes ⇒ String
Decodes a list of bytes from the IO object.
- #eof? ⇒ Boolean
-
#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.
17 18 19 |
# File 'lib/kafka/protocol/decoder.rb', line 17 def initialize(io) @io = io end |
Class Method Details
.from_string(str) ⇒ Object
10 11 12 |
# File 'lib/kafka/protocol/decoder.rb', line 10 def self.from_string(str) new(StringIO.new(str)) 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.
68 69 70 71 |
# File 'lib/kafka/protocol/decoder.rb', line 68 def array(&block) size = int32 size.times.map(&block) end |
#boolean ⇒ Boolean
Decodes an 8-bit boolean from the IO object.
28 29 30 |
# File 'lib/kafka/protocol/decoder.rb', line 28 def boolean read(1) == 0x1 end |
#bytes ⇒ String
Decodes a list of bytes from the IO object.
89 90 91 92 93 94 95 96 97 |
# File 'lib/kafka/protocol/decoder.rb', line 89 def bytes size = int32 if size == -1 nil else read(size) end end |
#eof? ⇒ Boolean
21 22 23 |
# File 'lib/kafka/protocol/decoder.rb', line 21 def eof? @io.eof? end |
#int16 ⇒ Integer
Decodes a 16-bit integer from the IO object.
42 43 44 |
# File 'lib/kafka/protocol/decoder.rb', line 42 def int16 read(2).unpack("s>").first end |
#int32 ⇒ Integer
Decodes a 32-bit integer from the IO object.
49 50 51 |
# File 'lib/kafka/protocol/decoder.rb', line 49 def int32 read(4).unpack("l>").first end |
#int64 ⇒ Integer
Decodes a 64-bit integer from the IO object.
56 57 58 |
# File 'lib/kafka/protocol/decoder.rb', line 56 def int64 read(8).unpack("q>").first end |
#int8 ⇒ Integer
Decodes an 8-bit integer from the IO object.
35 36 37 |
# File 'lib/kafka/protocol/decoder.rb', line 35 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.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kafka/protocol/decoder.rb', line 103 def read(number_of_bytes) return "" if number_of_bytes == 0 data = @io.read(number_of_bytes) or raise EOFError # If the `read` call returned less data than expected we should not # proceed. raise EOFError if data.size != number_of_bytes data end |
#string ⇒ String
Decodes a string from the IO object.
76 77 78 79 80 81 82 83 84 |
# File 'lib/kafka/protocol/decoder.rb', line 76 def string size = int16 if size == -1 nil else read(size) end end |