class Kafka::Protocol::Encoder

An encoder wraps an IO object, making it easy to write specific data types to it.

Public Class Methods

encode_with(object) click to toggle source

Encodes an object into a new buffer.

@param object [#encode] the object that will encode itself. @return [String] the encoded data.

# File lib/kafka/protocol/encoder.rb, line 174
def self.encode_with(object)
  buffer = StringIO.new
  encoder = new(buffer)

  object.encode(encoder)

  buffer.string
end
new(io) click to toggle source

Initializes a new encoder.

@param io [IO] an object that acts as an IO.

# File lib/kafka/protocol/encoder.rb, line 13
def initialize(io)
  @io = io
  @io.set_encoding(Encoding::BINARY)
end

Public Instance Methods

write(bytes) click to toggle source

Writes bytes directly to the IO object.

@param bytes [String] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 22
def write(bytes)
  @io.write(bytes)

  nil
end
write_array(array, &block) click to toggle source

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.

@param array [Array] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 76
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) click to toggle source

Writes an 8-bit boolean to the IO object.

@param boolean [Boolean] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 32
def write_boolean(boolean)
  boolean ? write_int8(1) : write_int8(0)
end
write_bytes(bytes) click to toggle source

Writes a byte string to the IO object.

@param bytes [String] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 148
def write_bytes(bytes)
  if bytes.nil?
    write_int32(-1)
  else
    write_int32(bytes.bytesize)
    write(bytes)
  end
end
write_int16(int) click to toggle source

Writes a 16-bit integer to the IO object.

@param int [Integer] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 48
def write_int16(int)
  write([int].pack("s>"))
end
write_int32(int) click to toggle source

Writes a 32-bit integer to the IO object.

@param int [Integer] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 56
def write_int32(int)
  write([int].pack("l>"))
end
write_int64(int) click to toggle source

Writes a 64-bit integer to the IO object.

@param int [Integer] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 64
def write_int64(int)
  write([int].pack("q>"))
end
write_int8(int) click to toggle source

Writes an 8-bit integer to the IO object.

@param int [Integer] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 40
def write_int8(int)
  write([int].pack("C"))
end
write_string(string) click to toggle source

Writes a string to the IO object.

@param string [String] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 104
def write_string(string)
  if string.nil?
    write_int16(-1)
  else
    write_int16(string.bytesize)
    write(string)
  end
end
write_varint(int) click to toggle source

Writes an integer under varints serializing to the IO object. developers.google.com/protocol-buffers/docs/encoding#varints

@param int [Integer] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 131
def write_varint(int)
  int = int << 1
  int = ~int | 1 if int < 0

  chunks = []
  while int >> 7 != 0
    chunks << (int & 0x7f | 0x80)
    int >>= 7
  end
  chunks << int
  write(chunks.pack("C*"))
end
write_varint_array(array, &block) click to toggle source

Writes an array to the IO object. Just like write_array, unless the size is under varint format

@param array [Array] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 91
def write_varint_array(array, &block)
  if array.nil?
    write_varint(-1)
  else
    write_varint(array.size)
    array.each(&block)
  end
end
write_varint_bytes(bytes) click to toggle source

Writes a byte string to the IO object, the size is under varint format

@param bytes [String] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 161
def write_varint_bytes(bytes)
  if bytes.nil?
    write_varint(-1)
  else
    write_varint(bytes.bytesize)
    write(bytes)
  end
end
write_varint_string(string) click to toggle source

Writes a string to the IO object, the size is under varint format

@param string [String] @return [nil]

# File lib/kafka/protocol/encoder.rb, line 117
def write_varint_string(string)
  if string.nil?
    write_varint(-1)
  else
    write_varint(string.bytesize)
    write(string)
  end
end