Module: SchemaRegistry::Wire

Defined in:
lib/schema_registry_client/wire.rb

Class Method Summary collapse

Class Method Details

.read_int(stream) ⇒ Object

Read an int with zig-zag encoding. Copied from Avro.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/schema_registry_client/wire.rb', line 17

def read_int(stream)
  b = stream.readbyte
  n = b & 0x7F
  shift = 7
  while (b & 0x80) != 0
    b = stream.readbyte
    n |= (b & 0x7F) << shift
    shift += 7
  end
  (n >> 1) ^ -(n & 1)
end

.write_int(stream, num) ⇒ Object

Write an int with zig-zag encoding. Copied from Avro.



7
8
9
10
11
12
13
14
# File 'lib/schema_registry_client/wire.rb', line 7

def write_int(stream, num)
  num = (num << 1) ^ (num >> 63)
  while (num & ~0x7F) != 0
    stream.write(((num & 0x7f) | 0x80).chr)
    num >>= 7
  end
  stream.write(num.chr)
end