def write(self, io: ByteIo, array: List[T]) -> None:
        if array is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        io.write_size(len(array), self.reserved_byte)
        for item in array:
            self.codec.write(io, item)
예제 #2
0
    def read(self, io: ByteIo) -> bytes or None:
        marker: bytes = io.read()
        if marker == NoneCodec.NONE_VALUE:
            return None

        if marker == self.byte_bytes:
            return io.read()

        raise TypeError("Could not deserialize as a byte.")
예제 #3
0
    def read(self, io: ByteIo) -> int or None:
        marker: bytes = io.read()
        if marker == NoneCodec.NONE_VALUE:
            return None

        read: bytes
        if marker == self.size_1:
            read: bytes = io.read(1, 8)
        elif marker == self.size_2:
            read: bytes = io.read(2, 8)
        elif marker == self.size_3:
            read: bytes = io.read(3, 8)
        elif marker == self.size_4:
            read: bytes = io.read(4, 8)
        elif marker == self.size_5:
            read: bytes = io.read(5, 8)
        elif marker == self.size_6:
            read: bytes = io.read(6, 8)
        elif marker == self.size_7:
            read: bytes = io.read(7, 8)
        elif marker == self.size_8:
            read: bytes = io.read(8, 8)
        else:
            raise TypeError("Could not deserialize as a long.")
        return struct.unpack(">q", read)[0]
    def read(self, io: ByteIo) -> List[T] or None:
        read: int = from_byte(io.peek())

        if read == from_byte(NoneCodec.NONE_VALUE):
            return None

        size: bytes or None = io.read_size(self.reserved_byte)

        out: List[T] = []
        for i in range(0, size):
            out.append(self.codec.read(io))
        return out
예제 #5
0
    def write(self, io: ByteIo, collection: Set[T]) -> None:
        if collection is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        io.write_size(len(collection), self.reserved_byte)

        for value in collection:
            codec: Codec = self.value_codec
            if codec is None:
                codec = self.codec_cache.codec_for(value)
            codec.write(io, value)
예제 #6
0
    def read(self, io: ByteIo) -> int or None:
        marker: bytes = io.read()
        if marker == NoneCodec.NONE_VALUE:
            return None

        read: bytes
        if marker == self.size_1:
            read: bytes = io.read(1, 2)
        elif marker == self.size_2:
            read: bytes = io.read(2, 2)
        else:
            raise TypeError("Could not deserialize as a short.")
        return struct.unpack(">h", read)[0]
예제 #7
0
    def read(self, io: ByteIo) -> str or None:
        marker: bytes = io.read()
        if marker == NoneCodec.NONE_VALUE:
            return None

        if marker == self.size_1:
            return chr(io.read_int())
        if marker == self.size_2:
            return chr(io.read_int(2))
        if marker == self.size_3:
            return chr(io.read_int(3))

        raise TypeError("Could not deserialize as a character.")
    def read(self, io: ByteIo) -> List[bytes] or None:
        read: int = from_byte(io.peek())

        if read == from_byte(NoneCodec.NONE_VALUE):
            return None

        size: bytes or None = io.read_size(self.reserved_byte)

        if size == 0:
            return []
        out: List[bytes] = []
        for b in io.read(size):
            out.append(b.to_bytes(1, byteorder="big", signed=False))
        return out
예제 #9
0
    def read(self, io: ByteIo) -> Set[T] or None:
        read: int = from_byte(io.peek())

        if read == from_byte(NoneCodec.NONE_VALUE):
            return None

        size: int = io.read_size(self.reserved_byte)

        out: Set[any] = set()
        for i in range(0, size):
            codec: Codec = self.value_codec
            if codec is None:
                codec = self.codec_cache.get(io.peek())
            out.add(codec.read(io))
        return out
예제 #10
0
    def read(self, io: ByteIo) -> List[str] or None:
        read: int = from_byte(io.peek())

        if read == from_byte(NoneCodec.NONE_VALUE):
            return None

        size: bytes or None = io.read_size(self.reserved_byte)

        out: List[str] = []

        encoded: bytes = io.read(size)
        string: str = encoded.decode("UTF-8")
        for char in string:
            out.append(char)
        return out
예제 #11
0
    def read(self, io: ByteIo) -> float or None:
        marker: bytes = io.read()
        if marker == NoneCodec.NONE_VALUE:
            return None

        read: bytes
        if marker == self.size_1:
            read: bytes = io.read(1, 4)
        elif marker == self.size_2:
            read: bytes = io.read(2, 4)
        elif marker == self.size_3:
            read: bytes = io.read(3, 4)
        elif marker == self.size_4:
            read: bytes = io.read(4, 4)
        else:
            raise TypeError("Could not deserialize as a float.")
        return struct.unpack(">f", read)[0]
    def write(self, io: ByteIo, array: List[int]) -> None:
        if array is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        io.write_size(len(array), self.reserved_byte)

        for element in array:
            io.write1(element >> 8)
            io.write1(element)
예제 #13
0
    def write(self, io: ByteIo, dictionary: Dict[T, U]) -> None:
        if dictionary is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        io.write_size(len(dictionary), self.reserved_byte)

        for key, value in dictionary.items():
            k_codec: Codec[T] = self.key_codec
            if k_codec is None:
                k_codec = self.codec_cache.codec_for(key)

            v_codec: Codec[U] = self.value_codec
            if v_codec is None:
                v_codec = self.codec_cache.codec_for(value)

            k_codec.write(io, key)
            v_codec.write(io, value)
예제 #14
0
    def read(self, io: ByteIo) -> str or None:
        size: int or None
        read: int = from_byte(io.peek())

        if read == from_byte(NoneCodec.NONE_VALUE):
            return None

        if self.optimized_from <= read < self.size_1:
            size = read - self.optimized_from
        else:
            size = io.read_size(to_byte(self.size_1))
        if size is None:
            return None

        if size == 0:
            return ""

        raw_bytes: bytes = io.read(size)
        return str(raw_bytes, "UTF-8")
예제 #15
0
class Deserializer:
    __codec_cache: CodecCache
    __reader: ByteIo

    def __init__(self, codec_cache: CodecCache, reader: BinaryIO):
        super().__init__()

        self.__codec_cache = codec_cache
        self.__reader = ByteIo(reader)

    def read(self, codec: None or Codec[T] = None) -> T:
        if codec is not None:
            return codec.read(self.__reader)

        found: Codec = self.__codec_cache.get(self.__reader.peek())
        return found.read(self.__reader)

    def close(self) -> None:
        self.__reader.close()
예제 #16
0
    def read(self, io: ByteIo) -> bool or None:
        marker: bytes = io.read()
        if marker == NoneCodec.NONE_VALUE:
            return None

        if marker == self.true_bytes:
            return True
        if marker == self.false_bytes:
            return False

        raise TypeError("Could not deserialize as a boolean.")
예제 #17
0
    def write(self, io: ByteIo, collection: str) -> None:
        if collection is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        data: bytes = collection.encode("UTF-8")
        length: int = len(data)
        if length < self.size_1 - self.optimized_from:
            io.write(to_byte((self.optimized_from + length) & 0xFF))
        else:
            io.write_size(length, to_byte(self.size_1))
        if length != 0:
            io.write(data)
예제 #18
0
    def write(self, io: ByteIo, value: bytes) -> None:
        if value is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        io.write(self.byte_bytes)
        io.write(value)
예제 #19
0
    def read(self, io: ByteIo) -> Dict[T, U] or None:
        read: int = from_byte(io.peek())

        if read == from_byte(NoneCodec.NONE_VALUE):
            return None

        size: int = io.read_size(self.reserved_byte)

        out: Dict[T, U] = {}
        for i in range(0, size):
            k_codec: Codec[T] = self.key_codec
            if k_codec is None:
                k_codec = self.codec_cache.get(io.peek())
            key: T = k_codec.read(io)

            v_codec: Codec[U] = self.value_codec
            if v_codec is None:
                v_codec = self.codec_cache.get(io.peek())
            value: T = v_codec.read(io)

            out[key] = value
        return out
예제 #20
0
    def read(self, io: ByteIo) -> Deque[T] or None:
        marker: bytes = io.read()

        if marker == NoneCodec.NONE_VALUE:
            return None

        if marker != self.start:
            raise TypeError("Start of queue not found")

        out: Deque[T] = collections.deque()

        while True:
            marker = io.peek()

            if marker == self.end:
                io.read()
                return out

            codec: Codec = self.value_codec
            if codec is None:
                codec = self.codec_cache.get(marker)
            out.append(codec.read(io))
예제 #21
0
    def write(self, io: ByteIo, array: List[bytes]) -> None:
        if array is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        io.write_size(len(array), self.reserved_byte)
        for b in array:
            io.write(b)
예제 #22
0
    def write(self, io: ByteIo, array: List[str]) -> None:
        if array is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        encoded: bytes = "".join(array).encode("UTF-8")
        io.write_size(len(encoded), self.reserved_byte)
        io.write(encoded)
    def read(self, io: ByteIo) -> List[bool] or None:
        read: int = from_byte(io.peek())

        if read == from_byte(NoneCodec.NONE_VALUE):
            return None

        size: bytes or None = io.read_size(self.reserved_byte)

        out: List[bool] = []
        i: int = 0
        value: int = 0
        read: int = 8
        while i < size:
            if read == 8:
                value = int_from_byte(io.read(1))
                read = 0
            decoded: bool = (value & 0b10000000) != 0
            out.append(decoded)
            i += 1
            value = value << 1
            read += 1

        return out
예제 #24
0
    def write(self, io: ByteIo, value: int) -> None:
        if value is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        b: bytes = struct.pack(">h", value)
        buffer_len: int = len(b)
        found: bool = False

        for i in range(0, len(b)):
            if not found and b[i] == 0:
                buffer_len = buffer_len - 1
            else:
                found = True

        if buffer_len == 2:
            io.write(self.size_2)
            io.write2(value)
        elif buffer_len <= 1:
            io.write(self.size_1)
            io.write1(value)
        else:
            raise TypeError(f"Could not serialize short {value}")
예제 #25
0
    def write(self, io: ByteIo, value: bool) -> None:
        if value is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        if value:
            io.write(self.true_bytes)
        else:
            io.write(self.false_bytes)
예제 #26
0
class Serializer:
    __codec_cache: CodecCache
    __writer: ByteIo

    def __init__(self, codec_cache: CodecCache, writer: BinaryIO):
        super().__init__()

        self.__codec_cache = codec_cache
        self.__writer = ByteIo(writer)

    def append(self, value: T, codec: None or Codec[T]) -> None:
        if codec is not None:
            codec.write(self.__writer, value)
            return

        codec: Codec[T] = self.__codec_cache.codec_for(value)

        if codec is None:
            raise ValueError(f"Missing codec for {type(value)}")

        codec.write(self.__writer, value)

    def close(self) -> None:
        self.__writer.close()
예제 #27
0
    def write(self, io: ByteIo, collection: Deque[T]) -> None:
        if collection is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        io.write(self.start)
        for value in collection:
            codec: Codec = self.value_codec
            if codec is None:
                codec = self.codec_cache.codec_for(value)
            codec.write(io, value)
        io.write(self.end)
    def write(self, io: ByteIo, array: List[bool]) -> None:
        if array is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        io.write_size(len(array), self.reserved_byte)
        current_count: int = 0
        value: int = 0

        for element in array:
            bool_int: int = 0
            if element:
                bool_int = 1
            value = value << 1 | bool_int
            if current_count == 7:
                current_count = 0
                io.write1(value)
            else:
                current_count = current_count + 1
        if current_count > 0:
            io.write1(value << (8 - current_count))
예제 #29
0
    def __init__(self, codec_cache: CodecCache, reader: BinaryIO):
        super().__init__()

        self.__codec_cache = codec_cache
        self.__reader = ByteIo(reader)
예제 #30
0
 def writer(self) -> ByteIo:
     file: str = "/d/tmp/seria.test"
     return ByteIo(open(file, "wb"))