Class: Kafka::Protocol::Encoder

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Encoder

Initializes a new encoder.

Parameters:

  • io (IO)

    an object that acts as an IO.



13
14
15
16
# File 'lib/kafka/protocol/encoder.rb', line 13

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.

Parameters:

  • object (#encode)

    the object that will encode itself.

Returns:

  • (String)

    the encoded data.



111
112
113
114
115
116
117
118
# File 'lib/kafka/protocol/encoder.rb', line 111

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.

Parameters:

  • bytes (String)

Returns:

  • (nil)


22
23
24
25
26
# File 'lib/kafka/protocol/encoder.rb', line 22

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.

Parameters:

  • array (Array)

Returns:

  • (nil)


76
77
78
79
# File 'lib/kafka/protocol/encoder.rb', line 76

def write_array(array, &block)
  write_int32(array.size)
  array.each(&block)
end

#write_boolean(boolean) ⇒ nil

Writes an 8-bit boolean to the IO object.

Parameters:

  • boolean (Boolean)

Returns:

  • (nil)


32
33
34
# File 'lib/kafka/protocol/encoder.rb', line 32

def write_boolean(boolean)
  write(boolean ? 0x1 : 0x0)
end

#write_bytes(bytes) ⇒ nil

Writes a byte string to the IO object.

Parameters:

  • bytes (String)

Returns:

  • (nil)


98
99
100
101
102
103
104
105
# File 'lib/kafka/protocol/encoder.rb', line 98

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.

Parameters:

  • int (Integer)

Returns:

  • (nil)


48
49
50
# File 'lib/kafka/protocol/encoder.rb', line 48

def write_int16(int)
  write([int].pack("s>"))
end

#write_int32(int) ⇒ nil

Writes a 32-bit integer to the IO object.

Parameters:

  • int (Integer)

Returns:

  • (nil)


56
57
58
# File 'lib/kafka/protocol/encoder.rb', line 56

def write_int32(int)
  write([int].pack("l>"))
end

#write_int64(int) ⇒ nil

Writes a 64-bit integer to the IO object.

Parameters:

  • int (Integer)

Returns:

  • (nil)


64
65
66
# File 'lib/kafka/protocol/encoder.rb', line 64

def write_int64(int)
  write([int].pack("q>"))
end

#write_int8(int) ⇒ nil

Writes an 8-bit integer to the IO object.

Parameters:

  • int (Integer)

Returns:

  • (nil)


40
41
42
# File 'lib/kafka/protocol/encoder.rb', line 40

def write_int8(int)
  write([int].pack("C"))
end

#write_string(string) ⇒ nil

Writes a string to the IO object.

Parameters:

  • string (String)

Returns:

  • (nil)


85
86
87
88
89
90
91
92
# File 'lib/kafka/protocol/encoder.rb', line 85

def write_string(string)
  if string.nil?
    write_int16(-1)
  else
    write_int16(string.bytesize)
    write(string)
  end
end