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

de.tsl2.nano.replication.serializer.SerializeBytes Maven / Gradle / Ivy

There is a newer version: 2.5.1
Show newest version
package de.tsl2.nano.replication.serializer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeBytes implements Serializer {
    public static final String KEY = "BYTES";
    @Override
    public String getKey() {
        return KEY;
    }
    @Override
    public String getExtension() {
        return "bytes";
    }
    @Override
    public ByteArrayOutputStream serialize(Object obj) {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        try(ObjectOutputStream stream = new ObjectOutputStream(bs)) {
			stream.writeObject(obj);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
        return bs;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public  T deserialize(InputStream stream, Class type) throws ClassNotFoundException, IOException {
        try(ObjectInputStream in = new ObjectInputStream(stream)) {return (T) in.readObject();}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy