All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javonet.core.protocol.CommandDeserializer Maven / Gradle / Ivy

Go to download

Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. It works on Linux/Windows and MacOS for applications created in JVM, CLR/Netcore, Perl, Python, Ruby, NodeJS, C++ or GoLang and gives you unparalleled freedom and flexibility with native performance in building your mixed-technologies products. Let it be accessing best AI or cryptography libraries, devices SDKs, legacy client modules, internal custom packages or anything from public repositories available on NPM, Nuget, PyPI, Maven/Gradle, RubyGems or GitHub. Get free from programming languages barriers today! For more information check out our guides at https://www.javonet.com/guides/v2/

There is a newer version: 2.4.5
Show newest version
package com.javonet.core.protocol;

import com.javonet.utils.*;

import java.util.Arrays;

public class CommandDeserializer {

    private final byte[] buffer;
    private int position;
    private Command command;

    public CommandDeserializer(byte[] buffer) {
        this.buffer = buffer;
        command = new Command(
                RuntimeName.values()[buffer[0]],
                CommandType.values()[buffer[10]]
        );
        position = 11;
    }

    private boolean isAtEnd() {
        return position >= buffer.length;
    }


    public Command deserialize() {
        while (!isAtEnd()) {
            command = command.addArgToPayload(readObject(buffer[position]));
        }
        return command;
    }


    private byte[] copyFrom(byte[] bytes, int elementsToSkip) {
        int size = bytes.length - elementsToSkip;
        byte[] newByteArray = new byte[size];
        System.arraycopy(bytes, elementsToSkip, newByteArray, 0, size);
        return newByteArray;
    }

    private Object readObject(int typeNum) {
        Type type = Type.values()[typeNum];
        switch (type) {
            case COMMAND:
                return readCommand();
            case STRING:
                return readString();
            case INTEGER:
                return readInt();
            case BOOLEAN:
                return readBool();
            case FLOAT:
                return readFloat();
            case BYTE:
                return readByte();
            case CHAR:
                return readChar();
            case LONG:
                return readLong();
            case DOUBLE:
                return readDouble();
            case UNSIGNED_LONG_LONG:
                return readUnsignedLongLong();
            case UNSIGNED_INTEGER:
                return readUnsignedInteger();

            default:
                throw new RuntimeException("Type not supported: " + typeNum);
        }
    }


    protected Command readCommand() {
        int p = position;
        int numberOfElementsInPayload = TypeDeserializer.deserializeInt(Arrays.copyOfRange(buffer, p + 1, p + 5));
        byte runtime = buffer[p + 5];
        byte type = buffer[p + 6];

        position += 7;

        Command command = new Command(
                RuntimeName.values()[runtime],
                CommandType.values()[type]
        );

        return readCommandRecursively(numberOfElementsInPayload, command);
    }

    private Command readCommandRecursively(int numberOfElementsInPayloadLeft, Command command) {
        if (numberOfElementsInPayloadLeft == 0) return command;
        int p = position;
        command = command.addArgToPayload(readObject(buffer[p]));
        return readCommandRecursively(numberOfElementsInPayloadLeft - 1, command);
    }


    private String readString() {
        int p = position;
        StringEncodingMode stringEncodingMode = StringEncodingMode.values()[buffer[p + 1]];
        int size = TypeDeserializer.deserializeInt(Arrays.copyOfRange(buffer, p + 2, p + 6));
        position += 6;
        p = position;
        position += size;
        return TypeDeserializer.deserializeString(stringEncodingMode, Arrays.copyOfRange(buffer, p, p + size));
    }

    private int readInt() {
        int size = 4;
        position += 2;
        int p = position;
        position += size;
        return TypeDeserializer.deserializeInt(Arrays.copyOfRange(buffer, p, p + size));
    }


    private boolean readBool() {
        int size = 1;
        position += 2;
        int p = position;
        position += size;
        int byteVal = buffer[p];
        return byteVal == 1;
    }

    private float readFloat() {
        int size = 4;
        position += 2;
        int p = position;
        position += size;
        return TypeDeserializer.deserializeFloat(Arrays.copyOfRange(buffer, p, p + size));
    }

    private byte readByte() {
        int size = 1;
        position += 2;
        int p = position;
        position += size;
        return TypeDeserializer.deserializeByte(buffer[p]);
    }

    private char readChar() {
        int size = 1;
        position += 2;
        int p = position;
        position += size;
        return TypeDeserializer.deserializeChar(buffer[p]);
    }

    private long readLong() {
        int size = 8;
        position += 2;
        int p = position;
        position += size;
        return TypeDeserializer.deserializeLongLong(Arrays.copyOfRange(buffer, p, p + size));
    }

    private double readDouble() {
        int size = 8;
        position += 2;
        int p = position;
        position += size;
        return TypeDeserializer.deserializeDouble(Arrays.copyOfRange(buffer, p, p + size));
    }

    private long readUnsignedLongLong() {
        int size = 8;
        position += 2;
        int p = position;
        position += size;
        return TypeDeserializer.deserializeLongLong(Arrays.copyOfRange(buffer, p, p + size));
    }

    private long readUnsignedInteger() {
        int size = 4;
        position += 2;
        int p = position;
        position += size;
        return TypeDeserializer.deserializeInt(Arrays.copyOfRange(buffer, p, p + size));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy