Simple, performant message library for Java.
Go to file
Andrew Lalis d88ce58e6e Add example, set release version 1.0.0 2023-09-22 09:56:47 -04:00
src Fix bug with primitive wrappers. 2023-09-22 09:19:08 -04:00
.gitignore Added first version stuff. 2022-04-16 13:03:21 +02:00
LICENSE Initial commit 2022-04-16 11:05:09 +02:00
README.md Add example, set release version 1.0.0 2023-09-22 09:56:47 -04:00
pom.xml Add example, set release version 1.0.0 2023-09-22 09:56:47 -04:00

README.md

record-net

Simple, performant message library for Java, using records. It allows you to serialize and deserialize records and their contents for use in network or filesystem IO.

Example

import com.andrewlalis.record_net.RecordMappedSerializer;
import java.io.*;

class Main {
    public static void main(String[] args) throws IOException {
        record MyData(int a, float b, String s) {}
        record FileData(String name, byte[] data) {}
        
        // Register the record types you want to use.
        RecordMappedSerializer serializer = new RecordMappedSerializer();
        serializer.registerType(MyData.class);
        serializer.registerType(FileData.class);

        // Write records.
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serializer.writeMessage(new MyData(42, 3.1415f, "Hello world!"), out);
        serializer.writeMessage(new FileData("test.txt", new byte[]{1, 2, 3, 4}), out);
        byte[] serialized = out.toByteArray();

        // Read records.
        Object obj = serializer.readMessage(new ByteArrayInputStream(serialized));
        switch (obj) {
            case MyData d -> System.out.println("Got MyData: " + d);
            case FileData d -> System.out.println("Got file data: " + d);
            default -> throw new RuntimeException();
        }
    }
}