Example usage for java.beans XMLDecoder XMLDecoder

List of usage examples for java.beans XMLDecoder XMLDecoder

Introduction

In this page you can find the example usage for java.beans XMLDecoder XMLDecoder.

Prototype

public XMLDecoder(InputSource is) 

Source Link

Document

Creates a new decoder to parse XML archives created by the XMLEncoder class.

Usage

From source file:Main.java

public static Object deserializeObject(File file) {
    Object data = null;//  w w w .j  a va  2s .  com
    try {
        FileInputStream os = new FileInputStream(file);
        XMLDecoder decoder = new XMLDecoder(os);
        data = decoder.readObject();
        decoder.close();
    } catch (FileNotFoundException f) {
        data = null;
        f.printStackTrace();
    }
    return data;
}

From source file:Main.java

public static List<Object> objectXmlDecoder(String source) throws IOException {
    List<Object> objList = new ArrayList<Object>();
    File fin = new File(source);
    FileInputStream fis = new FileInputStream(fin);
    XMLDecoder decoder = new XMLDecoder(fis);
    Object obj = null;//from w  w  w . ja  v  a  2 s. c o m
    try {
        while ((obj = decoder.readObject()) != null) {
            objList.add(obj);
        }
    } catch (Exception e) {
    }
    fis.close();
    decoder.close();
    return objList;
}

From source file:Main.java

public static List ObjectXmlDecoder(String objSource) throws FileNotFoundException, IOException, Exception {
    List objList = new ArrayList();
    File fin = new File(objSource);
    FileInputStream fis = new FileInputStream(fin);
    XMLDecoder decoder = new XMLDecoder(fis);
    Object obj = null;//from   w  w  w.  j  a  v a 2  s. co m
    try {
        while ((obj = decoder.readObject()) != null) {
            objList.add(obj);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
    }
    fis.close();
    decoder.close();
    return objList;
}

From source file:Main.java

public static Object deserializeObjectFromFile(final String filePath)
        throws FileNotFoundException, IOException, ClassNotFoundException {

    try (FileInputStream file = new FileInputStream(filePath);
            BufferedInputStream buffer = new BufferedInputStream(file);
            XMLDecoder input = new XMLDecoder(buffer);) {

        return input.readObject();

    }//www  .j  a  v  a2  s.  co m
}

From source file:Main.java

public static Object readBeanFromXml(String path) throws Exception {
    FileInputStream fis = null;/*from ww w  .ja  v a 2  s .  c om*/
    Object aplicacion = null;
    BufferedInputStream bis = null;
    try {
        fis = new FileInputStream(path);
        bis = new BufferedInputStream(fis);
        XMLDecoder xmlDecoder = new XMLDecoder(bis);
        aplicacion = (Object) xmlDecoder.readObject();

    } catch (FileNotFoundException ex) {
        throw new Exception("File to read not found.", ex);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                throw new Exception("File to close FileInputStream after read.", ex);
            }
        }

        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ex) {
                throw new Exception("File to close BufferedInputStream after read.", ex);
            }
        }
    }

    return aplicacion;

}

From source file:Main.java

/**
 * Creates from the given xml string an java object.
 * //from w  ww .j a v  a2 s.  c  om
 * @param <T>
 *            the generic type
 * @param xmlString
 *            the xml string to transform to an java object.
 * @return the xml string
 */
@SuppressWarnings("unchecked")
public static <T> T toObjectWithXMLDecoder(final String xmlString) {

    XMLDecoder dec = null;
    T obj = null;
    try {
        InputStream is = new ByteArrayInputStream(xmlString.getBytes());
        dec = new XMLDecoder(is);

        obj = (T) dec.readObject();

    } finally {
        if (dec != null) {
            dec.close();
        }
    }
    return obj;
}

From source file:Main.java

/**
 * Reads an object from an XML file.//  w ww  . jav  a 2 s .c  om
 * 
 * @param filename the filename
 * 
 * @return the object
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Object readBeanFromFile(String filename) throws IOException {
    XMLDecoder decoder = null;
    try {
        decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename)));
        return decoder.readObject();
    } finally {
        if (decoder != null) {
            decoder.close();
        }
    }
}

From source file:Main.java

/**
 * Creates from the given xml string an java object.
 * /*from  ww w.  ja va2  s . co m*/
 * @param <T>
 *            the generic type
 * @param xmlString
 *            the xml string to transform to an java object.
 * @return the xml string
 */
@SuppressWarnings("unchecked")
public static <T> T toObjectWithXMLDecoder(final String xmlString) {

    XMLDecoder dec = null;
    T obj = null;
    try {
        final InputStream is = new ByteArrayInputStream(xmlString.getBytes());
        dec = new XMLDecoder(is);

        obj = (T) dec.readObject();

    } finally {
        if (dec != null) {
            dec.close();
        }
    }
    return obj;
}

From source file:Main.java

/***
 * Deserialize a xml string into an object
 * /*from  w  w  w  . j a v  a2 s . co m*/
 * @param <T>
 *            type of the object to deserialize to
 * @param xmlStream
 *            xml stream represents an object
 * @param classToCastTo
 *            class to deserialize to
 * @return object deserialized from the xml
 * @throws SAXParseException
 *             if xmlString is not well formatted
 * @throws ClassCastException
 *             if the object is not the an instance of classToCastTo
 */
@SuppressWarnings("unchecked")
public static <T> T deserializeObject(final InputStream xmlStream, final Class<T> classToCastTo)
        throws SAXParseException, ClassCastException {

    // Create XML decoder.
    final XMLDecoder xmlDecoder = new XMLDecoder(xmlStream);

    try {

        final Object deserializedObj = xmlDecoder.readObject();

        // Will not perform cast check,
        // Let the casting throw ClassCastException if needed.
        return (T) deserializedObj;
    } finally {
        if (xmlDecoder != null) {
            xmlDecoder.close();
        }
    }

}

From source file:Main.java

public static Object decodeObject(byte[] byteArray, final boolean noisy) {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);

    XMLDecoder decoder = new XMLDecoder(inputStream);
    decoder.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(Exception ex) {
            if (noisy)
                ex.printStackTrace();/*  w ww . ja v a 2 s.  c om*/
        }
    });

    Object copy = decoder.readObject();
    decoder.close();
    return copy;
}