| 762 | self.length = len(contents) |
| 763 | |
| 764 | def decode(self, io): |
| 765 | raw_length = io.read(2) |
| 766 | if not raw_length or len(raw_length) != 2: |
| 767 | raise Exception('Failed to unserialize Utf') |
| 768 | self.length = struct.unpack('>H', raw_length)[0] |
| 769 | if self.length == 0: |
| 770 | self.contents = "" |
| 771 | else: |
| 772 | self.contents = io.read(self.length) |
| 773 | if not self.contents or len(self.contents) != self.length: |
| 774 | raise Exception('Failed to unserialize Utf') |
| 775 | return self |
| 776 | |
| 777 | def encode(self): |
| 778 | encoded = struct.pack('>H', self.length) |