Class: Avro::IO::BinaryEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/avro/io.rb

Overview

Write leaf values

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer) ⇒ BinaryEncoder

Returns a new instance of BinaryEncoder.



171
172
173
# File 'lib/avro/io.rb', line 171

def initialize(writer)
  @writer = writer
end

Instance Attribute Details

#writerObject (readonly)

Returns the value of attribute writer.



169
170
171
# File 'lib/avro/io.rb', line 169

def writer
  @writer
end

Instance Method Details

#write(datum) ⇒ Object

Write an arbritary datum.



234
235
236
# File 'lib/avro/io.rb', line 234

def write(datum)
  writer.write(datum)
end

#write_boolean(datum) ⇒ Object

a boolean is written as a single byte whose value is either 0 (false) or 1 (true).



182
183
184
185
# File 'lib/avro/io.rb', line 182

def write_boolean(datum)
  on_disk = datum ? 1.chr : 0.chr
  writer.write(on_disk)
end

#write_bytes(datum) ⇒ Object

Bytes are encoded as a long followed by that many bytes of data.



221
222
223
224
# File 'lib/avro/io.rb', line 221

def write_bytes(datum)
  write_long(datum.bytesize)
  @writer.write(datum)
end

#write_double(datum) ⇒ Object

A double is written as 8 bytes. The double is converted into a 64-bit integer using a method equivalent to Java’s doubleToRawLongBits and then encoded in little-endian format.



216
217
218
# File 'lib/avro/io.rb', line 216

def write_double(datum)
  @writer.write([datum].pack('E'))
end

#write_float(datum) ⇒ Object

A float is written as 4 bytes. The float is converted into a 32-bit integer using a method equivalent to Java’s floatToRawIntBits and then encoded in little-endian format.



208
209
210
# File 'lib/avro/io.rb', line 208

def write_float(datum)
  @writer.write([datum].pack('e'))
end

#write_int(n) ⇒ Object

int and long values are written using variable-length, zig-zag coding.



189
190
191
# File 'lib/avro/io.rb', line 189

def write_int(n)
  write_long(n)
end

#write_long(n) ⇒ Object

int and long values are written using variable-length, zig-zag coding.



195
196
197
198
199
200
201
202
# File 'lib/avro/io.rb', line 195

def write_long(n)
  n = (n << 1) ^ (n >> 63)
  while (n & ~0x7F) != 0
    @writer.write(((n & 0x7f) | 0x80).chr)
    n >>= 7
  end
  @writer.write(n.chr)
end

#write_null(_datum) ⇒ Object

null is written as zero bytes



176
177
178
# File 'lib/avro/io.rb', line 176

def write_null(_datum)
  nil
end

#write_string(datum) ⇒ Object

A string is encoded as a long followed by that many bytes of UTF-8 encoded character data



228
229
230
231
# File 'lib/avro/io.rb', line 228

def write_string(datum)
  datum = datum.encode('utf-8') if datum.respond_to? :encode
  write_bytes(datum)
end