Java 8 XMLDecoder - 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 XMLDecoder class.

This code example shows how to use the following methods:close, readObject
		Object result = null; 
		FileInputStream fis = null;
		XMLDecoder xmldec = null;
		try { 
			fis = new FileInputStream(source);
			xmldec = new XMLDecoder(new BufferedInputStream(fis));
			result = xmldec.readObject();
		} finally { 
			StreamUtil.close(fis);
			if (xmldec != null) {
				xmldec.close();
			} 
		} 
		return result; 
	} 
 
	/** 
	 * @see #readObjectFromXml(java.io.File) 
	 */ 
	public static Object readObjectFromXml(String source) throws IOException {
6
CodeRank
This code example shows how to use the following methods:close, readObject
            afe.initCause(exceptions.getFirst());
            throw afe;
        } 
 
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        XMLDecoder dec = new XMLDecoder(in, null, listener);
        Project p2 = (Project) dec.readObject();
        assertNotNull(p2);
        dec.close();
 
        // verify that the read 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:close, readObject
	public <T> T valueOf(byte[] str, Class<T> type) throws ParseException {
		if (str == null) {
			return null; 
		} 
		ByteArrayInputStream bi = new ByteArrayInputStream(str);
		XMLDecoder xd = new XMLDecoder(bi);
		try { 
			return (T) xd.readObject();
		} finally { 
			xd.close();
		} 
	} 
 
} 
6
CodeRank
This code example shows how to use the following methods:close, readObject, setExceptionListener
            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);
6
CodeRank
This code example shows how to use the following methods:close, readObject
		this.xmlToDecode = xmlToDecode;
	} 
	 
	public AbstractReportObject toAbstractReportObject() { 
		ExceptionListener exListener = new ReportObjectWrapperExceptionListener();
		XMLDecoder dec = new XMLDecoder(new BufferedInputStream(new ByteArrayInputStream(xmlToDecode.getBytes())), null,
		        exListener);
		AbstractReportObject o = (AbstractReportObject) dec.readObject();
		dec.close();
		 
		return o;
	} 
	 
	/** 
	 * @return Returns the xmlToDecode. 
	 */ 
	public String getXmlToDecode() {
		return xmlToDecode;
	} 
	 
6
CodeRank
This code example shows how to use the following methods:readObject
   */ 
  @SuppressWarnings("unchecked") 
  public EventData(String xml) {
    ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
    try { 
      XMLDecoder decoder = new XMLDecoder(bais);
      this.eventData = (Map<String, Object>) decoder.readObject();
    } catch (Exception e) {
      throw new EventException("Error decoding " + xml, e);
    } 
  } 
 
  /** 
   * Serialize all the EventData items into an XML representation. 
   *  
   * @return an XML String containing all the EventDAta items. 
   */ 
  public String toXML() {
    return toXML(eventData); 
  } 
6
CodeRank
This code example shows how to use the following methods:close, readObject
 
    private static void test(Locale locale, byte[] data) {
        Locale.setDefault(locale);
        System.out.println("locale = " + locale);
 
        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data));
        System.out.println("object = " + decoder.readObject());
        decoder.close();
    } 
 
    public static class DataBean { 
        private boolean illegal;
 
        public boolean isIllegal() { 
            return this.illegal;
        } 
 
        public void setIllegal(boolean illegal) {
            this.illegal = illegal;
        } 
6
CodeRank
This code example shows how to use the following methods:readObject
		if (raw.length() == 0) {
			return new PublicKeyFriendBean[0]; 
		} 
		PublicKeyFriendBean[] res = null;
		ByteArrayInputStream in = new ByteArrayInputStream(raw.getBytes("UTF-8"));
		XMLDecoder d = new XMLDecoder(in);
 
		Object o = d.readObject();
		if (o instanceof PublicKeyFriendBean[]) { 
			res = ((PublicKeyFriendBean[]) o);
		} 
		in.close();
		if (!oldClassLoader.equals(beanClassLoader)) {
			log("setting old classloader: " + oldClassLoader);
			Thread.currentThread().setContextClassLoader(oldClassLoader);
		} 
		return res;
	} 
 
	private static void log(String msg) {
6
CodeRank
This code example shows how to use the following methods:setOwner
 
    @Override 
    protected void validate(XMLDecoder decoder) {
        FIELD = "static prefix";
        field = "prefix";
        decoder.setOwner(this);
        validate(decoder, "static prefix");
        validate(decoder, "static postfix");
        validate(decoder, "prefix");
        validate(decoder, "postfix");
    } 
 
    private static void validate(XMLDecoder decoder, String name) {
        if (!decoder.readObject().equals(name)) {
            throw new Error(name + " expected");
        } 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:close, readObject
            public void exceptionThrown(Exception exception) {
                throw new Error("unexpected exception", exception);
            } 
        }; 
 
        XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
        Object object = decoder.readObject();
        decoder.close();
 
        if (!type.equals(object.getClass())) {
            throw new Error("unexpected " + object.getClass());
        } 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:close, readObject
        for (int i = 0; i < ATTEMPTS; i++)
            parse(); 
    } 
 
    private void parse() { 
        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(this.buffer));
        decoder.readObject();
        decoder.close();
    } 
} 
6
CodeRank
This code example shows how to use the following methods:setOwner
        this.message = message;
    } 
 
    @Override 
    protected void validate(XMLDecoder decoder) {
        decoder.setOwner(this);
        validate(decoder, "message");
        validate(decoder, "indexed");
    } 
 
    private static void validate(XMLDecoder decoder, String name) {
        if (!decoder.readObject().equals(name)) {
            throw new Error(name + " expected");
        } 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:close, readObject
        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:close, readObject
        return output.toByteArray();
    } 
 
    private Object readObject(byte[] array) {
        ByteArrayInputStream input = new ByteArrayInputStream(array);
        XMLDecoder decoder = new XMLDecoder(input);
        initialize(decoder);
        Object object = decoder.readObject();
        decoder.close();
        return object; 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:close, readObject
            afe.initCause(exceptions.getFirst());
            throw afe;
        } 
 
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        XMLDecoder dec = new XMLDecoder(in, null, listener);
        Project p2 = (Project) dec.readObject();
        assertNotNull(p2);
        dec.close();
 
        // verify that the read 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:close, readObject
XMLDecoder xmldec = null;
			try 
			{ 
				FileInputStream fis = new FileInputStream(file);
				xmldec = new XMLDecoder(fis);
				settings = (Settings) xmldec.readObject();
			} 
			catch (Exception e)
			{ 
				settings = null; 
			} 
			finally 
			{ 
				if (xmldec != null)
					xmldec.close();
			} 
		} 
		return settings; 
	} 
 
6
CodeRank
This code example shows how to use the following methods:close, readObject
    @SuppressWarnings("unchecked") 
    public XMLTables(final File file) throws IOException {
        this.propFile = file;
        this.timestamp = System.currentTimeMillis();
        if (propFile.exists()) {
            final XMLDecoder xmldec = new XMLDecoder(new FileInputStream(propFile));
            tables = (HashMap<String, Map<String, String>>) xmldec.readObject();
            xmldec.close();
        } else { 
            tables = new HashMap<String, Map<String, String>>();
        } 
    } 
 
    public void commit(final File target) throws IOException {
        // this method is used if the Mircrotable was created without assigning 
        // a file to it as an empty table the table then becomes file-based, 
        // and write operation will be committed to the file 
        this.propFile = target;
        commit(true); 
    } 
6
CodeRank
This code example shows how to use the following methods:close, readObject
            return decodeObject(in);
        } 
    } 
 
    private static Configuration decodeObject(InputStream in) throws IOException {
        XMLDecoder d = new XMLDecoder(new BufferedInputStream(in));
        final Object ret = d.readObject();
        d.close();
 
        if (!(ret instanceof Configuration)) { 
            throw new IOException("Not a valid config file");
        } 
        return (Configuration) ret; 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:close, readObject
        encoder.close();
    } 
 
    public static Object read(String filename) throws FileNotFoundException {
        XMLDecoder decoder =
                new XMLDecoder(new BufferedInputStream(
                        new FileInputStream(filename)));
        Object o = decoder.readObject();
        decoder.close();
        return o; 
    } 
} 
5
CodeRank
This code example shows how to use the following methods:readObject
		InputStream in=null;
		Mesh toReturn;
		try 
		{ 
			in=file.getInputStream();
			XMLDecoder decoder=new XMLDecoder(in);
			toReturn=(Mesh)decoder.readObject();
		} 
		catch (Exception ex)
		{ 
			ErrorManager.getDefault().log(ex.getMessage());
			String name=Utilities.getFreeName(file.getParent(), "amibe",".dir");
			toReturn = new Mesh(name);
		} 
		finally 
		{ 
			if(in!=null)
				try 
				{ 
					in.close();
5
CodeRank
This code example shows how to use the following methods:close, readObject
		} 
		else { 
			XMLDecoder dec = null;
 
			try { 
				dec = new XMLDecoder(new BufferedInputStream(new FileInputStream(prefsFileLocation)));
				values = (HashMap) dec.readObject();
				dec.close();
			} 
			catch (FileNotFoundException e) {
				log.error("Could not load preferences.");
			} 
 
			setDefaultPreferences(); // Write defaults for missing keys. 
		} 
	} 
 
	/** 
	 * Stores the preferences to disk 
	 */ 
5
CodeRank
This code example shows how to use the following methods:close, readObject
public class LoaderXML { 
 
    public DataSet load(String filePath) {
        DataSet dataset;
        try { 
            XMLDecoder dec = new XMLDecoder(
                    new BufferedInputStream(
                    new FileInputStream(filePath)));
            dataset = (DataSet) dec.readObject();
            dec.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null; 
        } 
 
        return dataset;
    } 
} 
5
CodeRank
This code example shows how to use the following methods:readObject, setOwner
        this.message = message;
    } 
 
    @Override 
    protected void validate(XMLDecoder decoder) {
        decoder.setOwner(this);
        if (this != decoder.readObject()) {
            throw new Error("owner should be the same");
        } 
        if (this.message == null) {
            throw new Error("owner's method is not called");
        } 
    } 
} 
5
CodeRank
This code example shows how to use the following methods:close, readObject
public class LoaderXML implements Loader{
 
    public DataSet loadAsDataSet(String filePath) {
        DataSet dataset;
        try { 
            XMLDecoder dec = new XMLDecoder(
                    new BufferedInputStream(
                    new FileInputStream(filePath)));
            dataset = (DataSet) dec.readObject();
            dec.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null; 
        } 
 
        return dataset;
    } 
 
	@Override 
	public Doc load(String filePath) throws FileVersionError {
5
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:getPersistenceDelegate
        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");
    } 
} 
5
CodeRank
This code example shows how to use the following methods:close, writeObject
 
public class ExporterXML { 
 
    public boolean export(DataSet dataset, String filePath) {
        try { 
            XMLEncoder enc = new XMLEncoder(
                    new BufferedOutputStream(
                    new FileOutputStream(filePath)));
            enc.writeObject(dataset);
            enc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false; 
        } 
 
        return true; 
    } 
} 
5
CodeRank
This code example shows how to use the following methods:close, writeObject
 
    public boolean export(Doc doc, String filePath) {
    	DataSet dataset = new DataSet(doc);
    	 
        try { 
            XMLEncoder enc = new XMLEncoder(
                    new BufferedOutputStream(
                    new FileOutputStream(filePath)));
            enc.writeObject(dataset);
            enc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false; 
        } 
 
        return true; 
    } 
} 
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
This code example shows how to use the following methods:close, flush, writeObject
 */ 
public class Serializer { 
 
    public static String serialize(Serializable whatever) {
        ByteArrayOutputStream io = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(io);
        encoder.writeObject(whatever);
        encoder.flush();
        encoder.close();
        try { 
            return io.toString("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            return ""; 
        } 
    } 
 
    public static Serializable deserialize(String whatever) {
        try { 
            ByteArrayInputStream io = new ByteArrayInputStream(whatever.getBytes("UTF-8"));
            XMLDecoder decoder = new XMLDecoder(io);
5
CodeRank
See Code Examples for Java 8 XMLDecoder Methods: