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

com.cosmian.jna.findex.serde.Leb128Writer Maven / Gradle / Ivy

package com.cosmian.jna.findex.serde;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import com.cosmian.utils.CloudproofException;
import com.cosmian.utils.Leb128;

public class Leb128Writer {

    final OutputStream os;

    public Leb128Writer(OutputStream os) {
        this.os = os;
    }

    public  void writeObject(T obj) throws CloudproofException {
        obj.writeObject(os);
    }

    private  void writeEntry(Entry tuple)
        throws CloudproofException {
        this.writeObject(tuple.getKey());
        this.writeObject(tuple.getValue());
    }

    public  void writeTuple(Tuple tuple)
        throws CloudproofException {
        this.writeEntry(tuple);
    }

    public  void writeCollection(Collection elements) throws CloudproofException {
        try {
            Leb128.writeU64(this.os, elements.size());
        } catch (IOException e) {
            throw new CloudproofException("failed writing the collection to the output: " + e.getMessage(), e);
        }
        for (T value : elements) {
            this.writeObject(value);
        }
    }

    public  void writeMap(Map map)
        throws CloudproofException {
        this.writeEntryCollection(map.entrySet());
    }

    public  void writeEntryCollection(Collection> entryCollection)
        throws CloudproofException {
        try {
            Leb128.writeU64(this.os, entryCollection.size());
        } catch (IOException e) {
            throw new CloudproofException("failed writing the entry collection to the output: " + e.getMessage(), e);
        }
        for (Entry value : entryCollection) {
            this.writeEntry(value);
        }
    }

    // ------------------------------------------------------
    // Static implementations
    // ------------------------------------------------------

    public static  byte[] serializeCollection(Collection elements)
        throws CloudproofException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        new Leb128Writer(bos).writeCollection(elements);
        return bos.toByteArray();
    }

    public static  byte[] serializeMap(Map map)
        throws CloudproofException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        new Leb128Writer(bos).writeMap(map);
        return bos.toByteArray();
    }

    public static  byte[] serializeTuple(Tuple tuple)
        throws CloudproofException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        new Leb128Writer(bos).writeTuple(tuple);
        return bos.toByteArray();
    }

    public static  byte[] serializeEntryCollection(Collection> entryCollection)
        throws CloudproofException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        new Leb128Writer(bos).writeEntryCollection(entryCollection);
        return bos.toByteArray();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy