Java 8 Encoder - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Encoder class.

This code example shows how to use the following methods:setExceptionListener, close, writeObject
                exceptions.addLast(e);
            } 
        }; 
 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder enc = new XMLEncoder(out);
        enc.setExceptionListener(listener);
        Project p1 = new Project();
        enc.writeObject(p1);
        enc.close();
 
        // verify that the write didn't fail 
        if (!exceptions.isEmpty()) {
            AssertionFailedError afe = new AssertionFailedError(
                    "Got " + exceptions.size() + " exception(s)");
            // Can only chain one of the exceptions. Take the first one. 
            afe.initCause(exceptions.getFirst());
            throw afe;
        } 
 
7
CodeRank
This code example shows how to use the following methods:setExceptionListener, close, writeObject
   * @return an XML String containing all the EventDAta items. 
   */ 
  public static String toXML(Map<String, Object> map) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try { 
      XMLEncoder encoder = new XMLEncoder(baos);
      encoder.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(Exception exception) {
          exception.printStackTrace();
        } 
      }); 
      encoder.writeObject(map);
      encoder.close();
      return baos.toString();
    } catch (Exception e) {
      e.printStackTrace();
      return null; 
    } 
  } 
 
7
CodeRank
This code example shows how to use the following methods:getPersistenceDelegate, setPersistenceDelegate
        test(Date.class, new DefaultPersistenceDelegate(names));
        test(null, new DefaultPersistenceDelegate());
    } 
 
    private static void test(Class<?> type, PersistenceDelegate pd) {
        Encoder encoder1 = new Encoder();
        Encoder encoder2 = new XMLEncoder(System.out);
 
        PersistenceDelegate pd1 = encoder1.getPersistenceDelegate(type);
        PersistenceDelegate pd2 = encoder2.getPersistenceDelegate(type);
 
        encoder1.setPersistenceDelegate(type, pd);
 
        if (pd1 == encoder1.getPersistenceDelegate(type))
            throw new Error("first persistence delegate is not changed");
 
        if (pd2 != encoder2.getPersistenceDelegate(type))
            throw new Error("second persistence delegate is changed");
    } 
} 
6
CodeRank
This code example shows how to use the following methods:setExceptionListener, close, writeObject
 
    private byte[] encode(String name) throws Exception {
        Object object = loadClass(name).newInstance();
        validate(object); 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(out);
        encoder.setExceptionListener(this);
        encoder.writeObject(object);
        encoder.close();
        return out.toByteArray();
    } 
 
    private Object decode(byte[] array) {
        ByteArrayInputStream in = new ByteArrayInputStream(array);
        XMLDecoder decoder = new XMLDecoder(in, null, this, this);
        Object object = decoder.readObject();
        validate(object); 
        decoder.close();
        return object; 
    } 
6
CodeRank
This code example shows how to use the following methods:setExceptionListener, close, writeObject
    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();
6
CodeRank
This code example shows how to use the following methods:setExceptionListener, close, writeObject
                exceptions.addLast(e);
            } 
        }; 
 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder enc = new XMLEncoder(out);
        enc.setExceptionListener(listener);
        Project p1 = new Project();
        enc.writeObject(p1);
        enc.close();
 
        // verify that the write didn't fail 
        if (!exceptions.isEmpty()) {
            AssertionFailedError afe = new AssertionFailedError(
                    "Got " + exceptions.size() + " exception(s)");
            // Can only chain one of the exceptions. Take the first one. 
            afe.initCause(exceptions.getFirst());
            throw afe;
        } 
 
6
CodeRank
This code example shows how to use the following methods:getPersistenceDelegate, setPersistenceDelegate
    private static final XMLEncoder ENCODER = new XMLEncoder(System.out);
 
    public static void main(String[] args) throws InterruptedException {
        Class<?> type = TestPersistenceDelegate.class;
        test(type, DefaultPersistenceDelegate.class);
        ENCODER.setPersistenceDelegate(type, new BeanPersistenceDelegate());
        test(type, BeanPersistenceDelegate.class);
        ENCODER.setPersistenceDelegate(type, null);
        test(type, DefaultPersistenceDelegate.class);
        // the following tests fails on previous build 
        test(Bean.class, BeanPersistenceDelegate.class); 
        test(BeanPersistenceDelegate.class, BeanPersistenceDelegate.class); 
    } 
 
    private static void test(Class<?> type, Class<? extends PersistenceDelegate> expected) {
        PersistenceDelegate actual = ENCODER.getPersistenceDelegate(type);
        if ((actual == null) && (expected != null)) {
            throw new Error("expected delegate is not found");
        } 
        if ((actual != null) && !actual.getClass().equals(expected)) {
            throw new Error("found unexpected delegate");
6
CodeRank
This code example shows how to use the following methods:setPersistenceDelegate, close, writeObject
		String prefsFile = System.getProperty("user.home") + "/." + AppContext.getName() + "/preferences.xml";
 
		XMLEncoder enc;
 
		try { 
			enc = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(new File(prefsFile))));
			enc.setPersistenceDelegate(FileObject.class, new DefaultPersistenceDelegate(new String[] { "absolutePath" }));
			enc.writeObject(values);
			enc.close();
 
		} 
		catch (FileNotFoundException e) {
			log.error("could not persist preferences.");
		} 
	} 
} 
5
CodeRank
This code example shows how to use the following methods:setExceptionListener
import java.beans.Expression;
 
public final class Test4950122 { 
    public static void main(String[] args) {
        TestBean bean = new TestBean(true, 11);
        Encoder encoder = new Encoder();
        encoder.setExceptionListener(bean);
        new TestDPD().instantiate(bean, encoder);
    } 
 
    public static class TestDPD extends DefaultPersistenceDelegate {
        public TestDPD() { 
            super(new String[] {"boolean", "integer"});
        } 
 
        public Expression instantiate(Object oldInstance, Encoder out) {
            return super.instantiate(oldInstance, out);
        } 
    } 
 
5
CodeRank
This code example shows how to use the following methods:setExceptionListener, setPersistenceDelegate
    protected Object getAnotherObject() { 
        return new A(); 
    } 
 
    protected void initialize(XMLEncoder encoder) {
        encoder.setExceptionListener(this);
        encoder.setPersistenceDelegate(C.class, new DefaultPersistenceDelegate() {
            protected Expression instantiate(Object oldInstance, Encoder out) {
                C c = (C) oldInstance;
                return new Expression(c, c.getX(), "createC", new Object[] {});
            } 
        }); 
    } 
 
    public static class Base { 
        private Object x; 
 
        public Object getX() { 
            return this.x;
        } 
5
CodeRank
See Code Examples for Java 8 Encoder Methods: