Ejemplo n.º 1
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.")
Ejemplo n.º 2
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]
Ejemplo n.º 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, 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]
Ejemplo n.º 4
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]
Ejemplo n.º 5
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.")
Ejemplo n.º 6
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))
Ejemplo n.º 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[int] 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[int] = []
        for i in range(0, size):
            out.append(int_from_byte(io.read(2)))

        return out
Ejemplo n.º 9
0
    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
Ejemplo n.º 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
Ejemplo n.º 11
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")
    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
Ejemplo n.º 13
0
 def read(self, io: ByteIo) -> None:
     if io.read() != NoneCodec.NONE_VALUE:
         raise TypeError("Could not deserialize as null.")
     return None