Package one.nio.serial

Source Code of one.nio.serial.ExternalizableSerializer

package one.nio.serial;

import one.nio.util.JavaInternals;

import sun.misc.Unsafe;

import java.io.Externalizable;
import java.io.IOException;
import java.io.NotSerializableException;

public class ExternalizableSerializer extends Serializer<Externalizable> {
    private static final Unsafe unsafe = JavaInternals.getUnsafe();

    ExternalizableSerializer(Class cls) {
        super(cls);
    }

    @Override
    public void calcSize(Externalizable obj, CalcSizeStream css) throws IOException {
        obj.writeExternal(css);
    }

    @Override
    public void write(Externalizable obj, DataStream out) throws IOException {
        obj.writeExternal(out);
    }

    @Override
    public Externalizable read(DataStream in) throws IOException, ClassNotFoundException {
        Externalizable result;
        try {
            result = (Externalizable) unsafe.allocateInstance(cls);
            in.register(result);
        } catch (InstantiationException e) {
            throw new IOException(e);
        }

        result.readExternal(in);
        return result;
    }

    @Override
    public void skip(DataStream in) throws IOException, ClassNotFoundException {
        read(in);
    }

    @Override
    public void toJson(Externalizable obj, StringBuilder builder) throws NotSerializableException {
        throw new NotSerializableException(cls.getName());
    }
}
TOP

Related Classes of one.nio.serial.ExternalizableSerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.