|
@ -244,7 +244,8 @@ class BCDataStream(object): |
|
|
self.input = None |
|
|
self.input = None |
|
|
self.read_cursor = 0 |
|
|
self.read_cursor = 0 |
|
|
|
|
|
|
|
|
def write(self, _bytes): # Initialize with string of _bytes |
|
|
def write(self, _bytes: Union[bytes, bytearray]): # Initialize with string of _bytes |
|
|
|
|
|
assert isinstance(_bytes, (bytes, bytearray)) |
|
|
if self.input is None: |
|
|
if self.input is None: |
|
|
self.input = bytearray(_bytes) |
|
|
self.input = bytearray(_bytes) |
|
|
else: |
|
|
else: |
|
@ -271,12 +272,14 @@ class BCDataStream(object): |
|
|
self.write_compact_size(len(string)) |
|
|
self.write_compact_size(len(string)) |
|
|
self.write(string) |
|
|
self.write(string) |
|
|
|
|
|
|
|
|
def read_bytes(self, length) -> bytes: |
|
|
def read_bytes(self, length: int) -> bytes: |
|
|
|
|
|
if self.input is None: |
|
|
|
|
|
raise SerializationError("call write(bytes) before trying to deserialize") |
|
|
assert length >= 0 |
|
|
assert length >= 0 |
|
|
input_len = len(self.input) |
|
|
input_len = len(self.input) |
|
|
read_begin = self.read_cursor |
|
|
read_begin = self.read_cursor |
|
|
read_end = read_begin + length |
|
|
read_end = read_begin + length |
|
|
if 0 <= read_begin <= input_len and read_end <= input_len: |
|
|
if 0 <= read_begin <= read_end <= input_len: |
|
|
result = self.input[read_begin:read_end] # type: bytearray |
|
|
result = self.input[read_begin:read_end] # type: bytearray |
|
|
self.read_cursor += length |
|
|
self.read_cursor += length |
|
|
return bytes(result) |
|
|
return bytes(result) |
|
@ -288,7 +291,7 @@ class BCDataStream(object): |
|
|
return False |
|
|
return False |
|
|
return self.read_cursor < len(self.input) |
|
|
return self.read_cursor < len(self.input) |
|
|
|
|
|
|
|
|
def read_boolean(self): return self.read_bytes(1)[0] != chr(0) |
|
|
def read_boolean(self) -> bool: return self.read_bytes(1) != b'\x00' |
|
|
def read_int16(self): return self._read_num('<h') |
|
|
def read_int16(self): return self._read_num('<h') |
|
|
def read_uint16(self): return self._read_num('<H') |
|
|
def read_uint16(self): return self._read_num('<H') |
|
|
def read_int32(self): return self._read_num('<i') |
|
|
def read_int32(self): return self._read_num('<i') |
|
@ -296,7 +299,7 @@ class BCDataStream(object): |
|
|
def read_int64(self): return self._read_num('<q') |
|
|
def read_int64(self): return self._read_num('<q') |
|
|
def read_uint64(self): return self._read_num('<Q') |
|
|
def read_uint64(self): return self._read_num('<Q') |
|
|
|
|
|
|
|
|
def write_boolean(self, val): return self.write(chr(1) if val else chr(0)) |
|
|
def write_boolean(self, val): return self.write(b'\x01' if val else b'\x00') |
|
|
def write_int16(self, val): return self._write_num('<h', val) |
|
|
def write_int16(self, val): return self._write_num('<h', val) |
|
|
def write_uint16(self, val): return self._write_num('<H', val) |
|
|
def write_uint16(self, val): return self._write_num('<H', val) |
|
|
def write_int32(self, val): return self._write_num('<i', val) |
|
|
def write_int32(self, val): return self._write_num('<i', val) |
|
|