| 756 | |
| 757 | |
| 758 | class Utf(Element): |
| 759 | def __init__(self, stream='', contents=''): |
| 760 | Element.__init__(self, stream) |
| 761 | self.contents = contents |
| 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) |
| 779 | encoded += self.contents |
| 780 | return encoded |
| 781 | |
| 782 | def __str__(self): |
| 783 | return self.contents |
| 784 | |
| 785 | |
| 786 | class LongUtf(Utf): |
no outgoing calls
no test coverage detected