def serialize(self, writer: io.IOBase) -> None: """ Serializes this tag. Parameters: - `writer`: The writer; """ pyilint.ilint_encode_to_stream(self.id, writer) if not self.implicit: pyilint.ilint_encode_to_stream(self.value_size(), writer) self.serialize_value(writer)
def serialize_tag_from_components(value: str, writer: io.IOBase, id: id = ILTAG_STRING_ID) -> int: """ Serializes `ILStringTag` like tag without the need to create a new instance of ILStringTag. This method was devised as a way to avoid unnecessary allocation of `ILStringTags` whenever possible. Parameters: - `str`: The string to be serialized. - `writer`: The writer that will receive the tag. - `id`: Alternative tag id if it is not ILTAG_STRING_ID; """ if isinstance(value, str): bin_value = ILStringTag.to_utf8(value) else: bin_value = value size = pyilint.ilint_encode_to_stream(id, writer) size += pyilint.ilint_encode_to_stream(len(bin_value), writer) writer.write(bin_value) return size + len(bin_value)
def test_serialize(self): class DummyILTag(ILTag): def value_size(self) -> int: return 4 def serialize_value(self, writer: io.IOBase) -> None: writer.write(b'1234') # Normal tag with no payload t = DummyILTag(65535) writer = io.BytesIO() t.serialize(writer) exp = io.BytesIO() ilint_encode_to_stream(65535, exp) ilint_encode_to_stream(4, exp) exp.write(b'1234') exp.seek(0) writer.seek(0) self.assertEqual(exp.read(), writer.read()) # Implicit t = DummyILTag(1, True) writer = io.BytesIO() t.serialize(writer) exp = io.BytesIO() ilint_encode_to_stream(1, exp) exp.write(b'1234') exp.seek(0) writer.seek(0) self.assertEqual(exp.read(), writer.read())
def serialize_value(self, writer: io.IOBase) -> None: pyilint.ilint_encode_to_stream(len(self), writer) for key in self: ILStringTag.serialize_tag_from_components(key, writer) ILStringTag.serialize_tag_from_components(self[key], writer)
def serialize_value(self, writer: io.IOBase) -> None: pyilint.ilint_encode_to_stream(self.first, writer) write_int(self.count, 2, False, writer)
def serialize_value(self, writer: io.IOBase) -> None: pyilint.ilint_encode_to_stream(len(self), writer) for t in self: t.serialize(writer)
def serialize_value(self, writer: io.IOBase) -> None: pyilint.ilint_encode_to_stream(self.value, writer)
def test_serialize(self): t = ILRawTag(16) writer = io.BytesIO() t.serialize(writer) exp = io.BytesIO() ilint_encode_to_stream(16, exp) ilint_encode_to_stream(0, exp) writer.seek(0) exp.seek(0) self.assertEqual(exp.read(), writer.read()) t = ILRawTag(256, b'') writer = io.BytesIO() t.serialize(writer) exp = io.BytesIO() ilint_encode_to_stream(256, exp) ilint_encode_to_stream(0, exp) writer.seek(0) exp.seek(0) self.assertEqual(exp.read(), writer.read()) t = ILRawTag(12312312, b'0123456789') writer = io.BytesIO() t.serialize(writer) exp = io.BytesIO() ilint_encode_to_stream(12312312, exp) ilint_encode_to_stream(10, exp) exp.write(b'0123456789') writer.seek(0) exp.seek(0) self.assertEqual(exp.read(), writer.read())