Beispiel #1
0
  def encode_to_stream(self, value, out, nested):
    # Compatible with Java's IterableLikeCoder.
    if hasattr(value, '__len__'):
      out.write_bigendian_int32(len(value))
      for elem in value:
        self._elem_coder.encode_to_stream(elem, out, True)
    else:
      # We don't know the size without traversing it so use a fixed size buffer
      # and encode as many elements as possible into it before outputting
      # the size followed by the elements.

      # -1 to indicate that the length is not known.
      out.write_bigendian_int32(-1)
      buffer = create_OutputStream()
      prev_index = index = -1
      for index, elem in enumerate(value):
        self._elem_coder.encode_to_stream(elem, buffer, True)
        if out.size() > self._DEFAULT_BUFFER_SIZE:
          out.write_var_int64(index - prev_index)
          out.write(buffer.get())
          prev_index = index
          buffer = create_OutputStream()
      if index > prev_index:
        out.write_var_int64(index - prev_index)
        out.write(buffer.get())
      out.write_var_int64(0)
Beispiel #2
0
    def encode_to_stream(self, value, out, nested):
        # Compatible with Java's IterableLikeCoder.
        if hasattr(value, '__len__'):
            out.write_bigendian_int32(len(value))
            for elem in value:
                self._elem_coder.encode_to_stream(elem, out, True)
        else:
            # We don't know the size without traversing it so use a fixed size buffer
            # and encode as many elements as possible into it before outputting
            # the size followed by the elements.

            # -1 to indicate that the length is not known.
            out.write_bigendian_int32(-1)
            buffer = create_OutputStream()
            prev_index = index = -1
            for index, elem in enumerate(value):
                self._elem_coder.encode_to_stream(elem, buffer, True)
                if out.size() > self._DEFAULT_BUFFER_SIZE:
                    out.write_var_int64(index - prev_index)
                    out.write(buffer.get())
                    prev_index = index
                    buffer = create_OutputStream()
            if index > prev_index:
                out.write_var_int64(index - prev_index)
                out.write(buffer.get())
            out.write_var_int64(0)
 def encode(self, value):
   out = create_OutputStream()
   self.encode_to_stream(value, out, False)
   return out.get()
Beispiel #4
0
 def encode(self, value):
     out = create_OutputStream()
     self.encode_to_stream(value, out, False)
     return out.get()