Search in sources :

Example 1 with XMLEncoder

use of java.beans.XMLEncoder in project SQLWindowing by hbutani.

the class SerializationUtils method addAntlrPersistenceDelegates.

public static void addAntlrPersistenceDelegates(XMLEncoder e) {
    e.setPersistenceDelegate(ASTNode.class, new PersistenceDelegate() {

        protected Expression instantiate(Object oldInstance, Encoder out) {
            return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] { ((ASTNode) oldInstance).getToken() });
        }
    });
    e.setPersistenceDelegate(CommonTree.class, new PersistenceDelegate() {

        protected Expression instantiate(Object oldInstance, Encoder out) {
            return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] { ((CommonTree) oldInstance).getToken() });
        }
    });
    e.setPersistenceDelegate(BaseTree.class, new PersistenceDelegate() {

        protected Expression instantiate(Object oldInstance, Encoder out) {
            return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] {});
        }

        @SuppressWarnings("rawtypes")
        protected void initialize(Class type, Object oldInstance, Object newInstance, Encoder out) {
            super.initialize(type, oldInstance, newInstance, out);
            BaseTree t = (BaseTree) oldInstance;
            for (int i = 0; i < t.getChildCount(); i++) {
                out.writeStatement(new Statement(oldInstance, "addChild", new Object[] { t.getChild(i) }));
            }
        }
    });
    e.setPersistenceDelegate(CommonToken.class, new PersistenceDelegate() {

        protected Expression instantiate(Object oldInstance, Encoder out) {
            return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] { ((CommonToken) oldInstance).getType(), ((CommonToken) oldInstance).getText() });
        }
    });
}
Also used : BaseTree(org.antlr.runtime.tree.BaseTree) Expression(java.beans.Expression) CommonTree(org.antlr.runtime.tree.CommonTree) XMLEncoder(java.beans.XMLEncoder) Encoder(java.beans.Encoder) Statement(java.beans.Statement) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) CommonToken(org.antlr.runtime.CommonToken) PersistenceDelegate(java.beans.PersistenceDelegate)

Example 2 with XMLEncoder

use of java.beans.XMLEncoder in project SQLWindowing by hbutani.

the class SerializationUtils method serialize.

public static void serialize(OutputStream out, Object o) {
    XMLEncoder e = new XMLEncoder(out);
    e.setExceptionListener(new EL());
    SerializationUtils.addPersistenceDelegates(e);
    e.writeObject(o);
    e.close();
}
Also used : XMLEncoder(java.beans.XMLEncoder)

Example 3 with XMLEncoder

use of java.beans.XMLEncoder in project hutool by looly.

the class XmlUtil method writeObjectAsXml.

/**
 * 将可序列化的对象转换为XML写入文件,已经存在的文件将被覆盖<br>
 * Writes serializable object to a XML file. Existing file will be overwritten
 *
 * @param <T> 对象类型
 * @param dest 目标文件
 * @param t 对象
 * @throws IOException IO异常
 */
public static <T> void writeObjectAsXml(File dest, T t) throws IOException {
    XMLEncoder xmlenc = null;
    try {
        xmlenc = new XMLEncoder(FileUtil.getOutputStream(dest));
        xmlenc.writeObject(t);
    } finally {
        // 关闭XMLEncoder会相应关闭OutputStream
        IoUtil.close(xmlenc);
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder)

Example 4 with XMLEncoder

use of java.beans.XMLEncoder in project jdk8u_jdk by JetBrains.

the class Test4625418 method test.

private void test(String string) {
    try {
        File file = new File("4625418." + this.encoding + ".xml");
        FileOutputStream output = new FileOutputStream(file);
        XMLEncoder encoder = new XMLEncoder(output, this.encoding, true, 0);
        encoder.setExceptionListener(this);
        encoder.writeObject(string);
        encoder.close();
        FileInputStream input = new FileInputStream(file);
        XMLDecoder decoder = new XMLDecoder(input);
        decoder.setExceptionListener(this);
        Object object = decoder.readObject();
        decoder.close();
        if (!string.equals(object))
            throw new Error(this.encoding + " - can't read properly");
        file.delete();
    } catch (FileNotFoundException exception) {
        throw new Error(this.encoding + " - file not found", exception);
    } catch (IllegalCharsetNameException exception) {
        throw new Error(this.encoding + " - illegal charset name", exception);
    } catch (UnsupportedCharsetException exception) {
        throw new Error(this.encoding + " - unsupported charset", exception);
    } catch (UnsupportedOperationException exception) {
        throw new Error(this.encoding + " - unsupported encoder", exception);
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) XMLEncoder(java.beans.XMLEncoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with XMLEncoder

use of java.beans.XMLEncoder in project jdk8u_jdk by JetBrains.

the class AbstractTest method test.

static void test(AbstractTest object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setPersistenceDelegate(object.getClass(), new DefaultPersistenceDelegate(new String[] { "value" }));
    encoder.writeObject(object);
    encoder.close();
    System.out.print(output);
    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    XMLDecoder decoder = new XMLDecoder(input);
    AbstractTest result = (AbstractTest) decoder.readObject();
    decoder.close();
    if (object.getValue() != result.getValue())
        throw new Error("Should be " + object);
}
Also used : XMLEncoder(java.beans.XMLEncoder) DefaultPersistenceDelegate(java.beans.DefaultPersistenceDelegate) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

XMLEncoder (java.beans.XMLEncoder)51 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 BufferedOutputStream (java.io.BufferedOutputStream)19 FileOutputStream (java.io.FileOutputStream)14 IOException (java.io.IOException)13 XMLDecoder (java.beans.XMLDecoder)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Test (org.junit.Test)9 File (java.io.File)8 LinkedList (java.util.LinkedList)7 FileInputStream (java.io.FileInputStream)6 AssertionFailedError (junit.framework.AssertionFailedError)6 ExceptionListener (java.beans.ExceptionListener)5 PersistenceDelegate (java.beans.PersistenceDelegate)4 BufferedInputStream (java.io.BufferedInputStream)4 DefaultPersistenceDelegate (java.beans.DefaultPersistenceDelegate)3 Encoder (java.beans.Encoder)3 OutputStream (java.io.OutputStream)3 Metacard (ddf.catalog.data.Metacard)2 Query (ddf.catalog.operation.Query)2