from struct import unpack class BinaryDecoder: """Decoder for the avro binary format. NOTE: All attributes and methods on this class should be considered private. Parameters ---------- fo: file-like Input stream """ def __init__(self, fo): self.fo = fo def read_null(self): """null is written as zero bytes.""" return None def read_boolean(self): """A boolean is written as a single byte whose value is either 0 (false) or 1 (true). """ # technically 0x01 == true and 0x00 == false, but many languages will # cast anything other than 0 to True and only 0 to False return unpack("B", self.fo.read(1))[0] != 0 def read_long(self): """int and long values are written using variable-length, zig-zag coding.""" c = self.fo.read(1) # We do EOF checking only here, since most reader start here if not c: raise EOFError b = ord(c) n = b & 0x7F shift = 7 while (b & 0x80) != 0: b = ord(self.fo.read(1)) n |= (b & 0x7F) << shift shift += 7 return (n >> 1) ^ -(n & 1) read_int = read_long def read_float(self): """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. """ return unpack("