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



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

def initialize(writer)
  @writer = writer
end

Instance Attribute Details

#writerObject (readonly)

Returns the value of attribute writer.



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

def writer
  @writer
end

Instance Method Details

#write(datum) ⇒ Object

Write an arbritary datum.



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

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).



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

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.



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

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 doubleToLongBits and then encoded in little-endian format.



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

def write_double(datum)
  @writer.write([datum].pack('E'.freeze))
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 floatToIntBits and then encoded in little-endian format.



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

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

#write_int(n) ⇒ Object

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



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

def write_int(n)
  write_long(n)
end

#write_long(n) ⇒ Object

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



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

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



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

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



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

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