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

com.jn.agileway.codec.serialization.xml.javabeans.JavaBeansXmlCodec Maven / Gradle / Ivy

Go to download

Provide an unified codec API for hession, kryo, protostuff, fst, fes, xson, cbor, jackson, json, etc....

There is a newer version: 3.1.12
Show newest version
package com.jn.agileway.codec.serialization.xml.javabeans;

import com.jn.agileway.codec.AbstractCodec;
import com.jn.langx.codec.CodecException;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Serializer implementation that uses the JavaBeans
 * {@link java.beans.XMLEncoder XMLEncoder} and {@link java.beans.XMLDecoder XMLDecoder} to serialize
 * and deserialize, respectively.
 * 

* NOTE: The JavaBeans XMLEncoder/XMLDecoder only successfully encode/decode objects when they are * JavaBeans compatible! * * @since 2.4.1 * This class should not be used directly because of unsecure XMLEncoder/XMLDecoder usage. */ public class JavaBeansXmlCodec extends AbstractCodec { @Override protected byte[] doEncode(T t, boolean withSchema) throws CodecException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(bos)); encoder.writeObject(t); encoder.close(); return bos.toByteArray(); } @Override protected T doDecode(byte[] bytes, boolean withSchema, Class targetType) throws CodecException { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(bis)); T o = (T) decoder.readObject(); decoder.close(); return o; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy