Class: Avro::IO::BinaryDecoder

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

Overview

FIXME(jmhodges) move validate to this module?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader) ⇒ BinaryDecoder

Returns a new instance of BinaryDecoder.



41
42
43
# File 'lib/avro/io.rb', line 41

def initialize(reader)
  @reader = reader
end

Instance Attribute Details

#readerObject (readonly)

reader is an object on which we can call read, seek and tell.



40
41
42
# File 'lib/avro/io.rb', line 40

def reader
  @reader
end

Instance Method Details

#byte!Object



45
46
47
# File 'lib/avro/io.rb', line 45

def byte!
  @reader.read(1).unpack('C').first
end

#read(len) ⇒ Object



104
105
106
107
# File 'lib/avro/io.rb', line 104

def read(len)
  # Read n bytes
  @reader.read(len)
end

#read_booleanObject



54
55
56
# File 'lib/avro/io.rb', line 54

def read_boolean
  byte! == 1
end

#read_bytesObject



90
91
92
93
94
# File 'lib/avro/io.rb', line 90

def read_bytes
  # Bytes are encoded as a long followed by that many bytes of
  # data.
  read(read_long)
end

#read_doubleObject



82
83
84
85
86
87
88
# File 'lib/avro/io.rb', line 82

def read_double
  #  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.
  @reader.read(8).unpack('E')[0]
end

#read_floatObject



74
75
76
77
78
79
80
# File 'lib/avro/io.rb', line 74

def read_float
  # 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.
  @reader.read(4).unpack('e')[0]
end

#read_intObject



58
# File 'lib/avro/io.rb', line 58

def read_int; read_long; end

#read_longObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/avro/io.rb', line 60

def read_long
  # int and long values are written using variable-length,
  # zig-zag coding.
  b = byte!
  n = b & 0x7F
  shift = 7
  while (b & 0x80) != 0
    b = byte!
    n |= (b & 0x7F) << shift
    shift += 7
  end
  (n >> 1) ^ -(n & 1)
end

#read_nullObject



49
50
51
52
# File 'lib/avro/io.rb', line 49

def read_null
  # null is written as zero byte's
  nil
end

#read_stringObject



96
97
98
99
100
101
102
# File 'lib/avro/io.rb', line 96

def read_string
  # A string is encoded as a long followed by that many bytes of
  # UTF-8 encoded character data.
  read_bytes.tap do |string|
    string.force_encoding("UTF-8") if string.respond_to? :force_encoding
  end
end

#skip(n) ⇒ Object



144
145
146
# File 'lib/avro/io.rb', line 144

def skip(n)
  reader.seek(reader.tell() + n)
end

#skip_booleanObject



113
114
115
# File 'lib/avro/io.rb', line 113

def skip_boolean
  skip(1)
end

#skip_bytesObject



136
137
138
# File 'lib/avro/io.rb', line 136

def skip_bytes
  skip(read_long)
end

#skip_doubleObject



132
133
134
# File 'lib/avro/io.rb', line 132

def skip_double
  skip(8)
end

#skip_floatObject



128
129
130
# File 'lib/avro/io.rb', line 128

def skip_float
  skip(4)
end

#skip_intObject



117
118
119
# File 'lib/avro/io.rb', line 117

def skip_int
  skip_long
end

#skip_longObject



121
122
123
124
125
126
# File 'lib/avro/io.rb', line 121

def skip_long
  b = byte!
  while (b & 0x80) != 0
    b = byte!
  end
end

#skip_nullObject



109
110
111
# File 'lib/avro/io.rb', line 109

def skip_null
  nil
end

#skip_stringObject



140
141
142
# File 'lib/avro/io.rb', line 140

def skip_string
  skip_bytes
end