def _deserialize_fixed_size_items_and_offsets(stream, field_sedes): for sedes in field_sedes: if sedes.is_fixed_sized: field_size = sedes.get_fixed_size() field_data = read_exact(field_size, stream) yield (sedes.deserialize(field_data), sedes) else: yield (s_decode_offset(stream), sedes)
def _deserialize_stream_to_tuple( self, stream: IO[bytes]) -> Iterable[TDeserializedElement]: if self.element_sedes.is_fixed_sized: element_size = self.element_sedes.get_fixed_size() for _ in range(self.length): element_data = read_exact(element_size, stream) yield self.element_sedes.deserialize(element_data) else: offsets = tuple( s_decode_offset(stream) for _ in range(self.length)) for left_offset, right_offset in sliding_window(2, offsets): element_length = right_offset - left_offset element_data = read_exact(element_length, stream) yield self.element_sedes.deserialize(element_data) # simply reading to the end of the current stream gives us all of the final element data final_element_data = stream.read() yield self.element_sedes.deserialize(final_element_data)
def deserialize_variable_size_parts(self, offset_pairs: Tuple[Tuple[int, TSedes], ...], stream: IO[bytes]) -> Iterable[Any]: offsets, fields = zip(*offset_pairs) *head_fields, last_field = fields for sedes, (left_offset, right_offset) in zip(head_fields, sliding_window(2, offsets)): field_length = right_offset - left_offset field_data = read_exact(field_length, stream) yield sedes.deserialize(field_data) # simply reading to the end of the current stream gives us all of the final element data final_field_data = stream.read() yield last_field.deserialize(final_field_data)
def _deserialize_stream_to_tuple( self, stream: IO[bytes]) -> Iterable[TDeserialized]: if self.element_sedes.is_fixed_sized: element_size = self.element_sedes.get_fixed_size() data = stream.read() if len(data) % element_size != 0: raise DeserializationError( f"Invalid max_length. List is comprised of a fixed size sedes " f"but total serialized data is not an even multiple of the " f"element size. data max_length: {len(data)} element size: " f"{element_size}") for start_idx in range(0, len(data), element_size): segment = data[start_idx:start_idx + element_size] yield self.element_sedes.deserialize(segment) else: stream_zero_loc = stream.tell() try: first_offset = s_decode_offset(stream) except DeserializationError: if stream.tell() == stream_zero_loc: # Empty list return else: raise num_remaining_offset_bytes = first_offset - stream.tell() if num_remaining_offset_bytes % OFFSET_SIZE != 0: raise DeserializationError( f"Offset bytes was not a multiple of {OFFSET_SIZE}. Got " f"{num_remaining_offset_bytes}") num_remaining_offsets = num_remaining_offset_bytes // OFFSET_SIZE tail_offsets = tuple( s_decode_offset(stream) for _ in range(num_remaining_offsets)) offsets = tuple(cons(first_offset, tail_offsets)) for left_offset, right_offset in sliding_window(2, offsets): element_length = right_offset - left_offset element_data = read_exact(element_length, stream) yield self.element_sedes.deserialize(element_data) # simply reading to the end of the current stream gives us all of the final element data final_element_data = stream.read() yield self.element_sedes.deserialize(final_element_data)