Пример #1
0
 def deserialize_from_bytes(cls, data_stream: Union[bytes, bytearray]):
     """ Deserialize object from a byte array. """
     br = BinaryReader(stream=data_stream)
     addr_payload = cls()
     addr_payload.deserialize(br)
     br.cleanup()
     return addr_payload
Пример #2
0
    def test_read_byte_from_empty_stream(self):
        x = BinaryReader(stream=bytearray())

        with self.assertRaises(ValueError) as context:
            x.read_byte()

        expected_error = "Could not read byte from empty stream"
        self.assertEqual(expected_error, str(context.exception))
Пример #3
0
    def test_read_more_data_than_available(self):
        data = b'\xaa\xbb'
        x = BinaryReader(stream=bytearray(data))

        with self.assertRaises(ValueError) as context:
            x.read_bytes(3)
        expected_error = "Could not read 3 bytes from stream. Only found 2 bytes of data"
        self.assertEqual(expected_error, str(context.exception))
Пример #4
0
 def deserialize_from_bytes(cls, data_stream: Union[bytes, bytearray]) -> 'Header':
     """ Deserialize object from a byte array. """
     br = BinaryReader(stream=data_stream)
     header = cls(None, None, None, None, None, None, None)
     try:
         header.deserialize(br)
     except DeserializationError:
         return None
     br.cleanup()
     return header
Пример #5
0
 def deserialize_from_bytes(cls, data_stream: Union[bytes, bytearray]):
     """ Deserialize object from a byte array. """
     br = BinaryReader(stream=data_stream)
     inv_payload = cls()
     try:
         inv_payload.deserialize(br)
     except ValueError:
         return None
     finally:
         br.cleanup()
     return inv_payload
Пример #6
0
    def test_reading_bytes(self):
        data = b'\xaa\xbb\xCC'
        x = BinaryReader(stream=bytearray(data))

        read_one = x.read_byte()
        self.assertEqual(1, len(read_one))
        self.assertEqual(b'\xaa', read_one)

        read_two = x.read_bytes(2)
        self.assertEqual(2, len(read_two))
        self.assertEqual(b'\xbb\xcc', read_two)
Пример #7
0
 def deserialize_from_bytes(cls, data_stream: Union[bytes, bytearray]) -> 'Block':
     """ Deserialize object from a byte array. """
     br = BinaryReader(stream=data_stream)
     block = cls(None, None, None, None, None, None)
     try:
         block.deserialize(br)
         # at this point we do not fully support all classes that can build up a block (e.g. Transactions)
         # the normal size calculation would request each class for its size and sum them up
         # we can shortcut this calculation in the absence of those classes by just determining the amount of bytes
         # in the payload
         block._size = len(data_stream)
     except ValueError:
         return None
     finally:
         br.cleanup()
     return block
Пример #8
0
 def test_initialization_with_bytearray(self):
     data = b'\xaa\xbb'
     x = BinaryReader(stream=bytearray(data))
     self.assertTrue(data, x._stream.getvalue())