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

io.neow3j.io.NeoSerializableInterface Maven / Gradle / Ivy

package io.neow3j.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

public interface NeoSerializableInterface {

    void deserialize(BinaryReader reader) throws IOException;

    void serialize(BinaryWriter writer) throws IOException;

    default byte[] toArray() {
        try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) {
            try (BinaryWriter writer = new BinaryWriter(ms)) {
                serialize(writer);
                writer.flush();
                return ms.toByteArray();
            }
        } catch (IOException ex) {
            throw new UnsupportedOperationException(ex);
        }
    }

    static  T from(byte[] value, Class t) throws InstantiationException, IllegalAccessException {
        try (ByteArrayInputStream ms = new ByteArrayInputStream(value)) {
            try (BinaryReader reader = new BinaryReader(ms)) {
                return reader.readSerializable(t);
            }
        } catch (IOException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    static  List fromAsList(byte[] value, Class t) throws InstantiationException, IllegalAccessException {
        try (ByteArrayInputStream ms = new ByteArrayInputStream(value)) {
            try (BinaryReader reader = new BinaryReader(ms)) {
                return reader.readSerializableListVarBytes(t);
            }
        } catch (IOException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy