Class: Kafka::Protocol::Encoder
- Inherits:
-
Object
- Object
- Kafka::Protocol::Encoder
- Defined in:
- lib/kafka/protocol/encoder.rb
Overview
An encoder wraps an IO object, making it easy to write specific data types to it.
Class Method Summary collapse
-
.encode_with(object) ⇒ String
Encodes an object into a new buffer.
Instance Method Summary collapse
-
#initialize(io) ⇒ Encoder
constructor
Initializes a new encoder.
-
#write(bytes) ⇒ nil
Writes bytes directly to the IO object.
-
#write_array(array, &block) ⇒ nil
Writes an array to the IO object.
-
#write_boolean(boolean) ⇒ nil
Writes an 8-bit boolean to the IO object.
-
#write_bytes(bytes) ⇒ nil
Writes a byte string to the IO object.
-
#write_int16(int) ⇒ nil
Writes a 16-bit integer to the IO object.
-
#write_int32(int) ⇒ nil
Writes a 32-bit integer to the IO object.
-
#write_int64(int) ⇒ nil
Writes a 64-bit integer to the IO object.
-
#write_int8(int) ⇒ nil
Writes an 8-bit integer to the IO object.
-
#write_string(string) ⇒ nil
Writes a string to the IO object.
Constructor Details
#initialize(io) ⇒ Encoder
Initializes a new encoder.
15 16 17 18 |
# File 'lib/kafka/protocol/encoder.rb', line 15 def initialize(io) @io = io @io.set_encoding(Encoding::BINARY) end |
Class Method Details
.encode_with(object) ⇒ String
Encodes an object into a new buffer.
118 119 120 121 122 123 124 125 |
# File 'lib/kafka/protocol/encoder.rb', line 118 def self.encode_with(object) buffer = StringIO.new encoder = new(buffer) object.encode(encoder) buffer.string end |
Instance Method Details
#write(bytes) ⇒ nil
Writes bytes directly to the IO object.
24 25 26 27 28 |
# File 'lib/kafka/protocol/encoder.rb', line 24 def write(bytes) @io.write(bytes) nil end |
#write_array(array, &block) ⇒ nil
Writes an array to the IO object.
Each item in the specified array will be yielded to the provided block; it's the responsibility of the block to write those items using the encoder.
78 79 80 81 82 83 84 85 86 |
# File 'lib/kafka/protocol/encoder.rb', line 78 def write_array(array, &block) if array.nil? # An array can be null, which is different from it being empty. write_int32(-1) else write_int32(array.size) array.each(&block) end end |
#write_boolean(boolean) ⇒ nil
Writes an 8-bit boolean to the IO object.
34 35 36 |
# File 'lib/kafka/protocol/encoder.rb', line 34 def write_boolean(boolean) boolean ? write_int8(1) : write_int8(0) end |
#write_bytes(bytes) ⇒ nil
Writes a byte string to the IO object.
105 106 107 108 109 110 111 112 |
# File 'lib/kafka/protocol/encoder.rb', line 105 def write_bytes(bytes) if bytes.nil? write_int32(-1) else write_int32(bytes.bytesize) write(bytes) end end |
#write_int16(int) ⇒ nil
Writes a 16-bit integer to the IO object.
50 51 52 |
# File 'lib/kafka/protocol/encoder.rb', line 50 def write_int16(int) write([int].pack("s>")) end |
#write_int32(int) ⇒ nil
Writes a 32-bit integer to the IO object.
58 59 60 |
# File 'lib/kafka/protocol/encoder.rb', line 58 def write_int32(int) write([int].pack("l>")) end |
#write_int64(int) ⇒ nil
Writes a 64-bit integer to the IO object.
66 67 68 |
# File 'lib/kafka/protocol/encoder.rb', line 66 def write_int64(int) write([int].pack("q>")) end |
#write_int8(int) ⇒ nil
Writes an 8-bit integer to the IO object.
42 43 44 |
# File 'lib/kafka/protocol/encoder.rb', line 42 def write_int8(int) write([int].pack("C")) end |
#write_string(string) ⇒ nil
Writes a string to the IO object.
92 93 94 95 96 97 98 99 |
# File 'lib/kafka/protocol/encoder.rb', line 92 def write_string(string) if string.nil? write_int16(-1) else write_int16(string.bytesize) write(string) end end |