Example usage for java.beans XMLDecoder readObject

List of usage examples for java.beans XMLDecoder readObject

Introduction

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

Prototype

public Object readObject() 

Source Link

Document

Reads the next object from the underlying input stream.

Usage

From source file:Main.java

/**
 * Deserializes an object into from xml file
 * //  w  w  w. j a v a 2  s . com
 * @param fileName path to ffile
 */
public static Object decodeFromFile(String fileName) throws FileNotFoundException, IOException {
    Object object = null;
    XMLDecoder decoder = new XMLDecoder(new FileInputStream(fileName));
    try {
        object = decoder.readObject();
    } finally {
        decoder.close();
    }
    return object;
}

From source file:Main.java

public static Object deserialize(InputStream in1) {
    XMLDecoder d = null;
    try {/* ww w .  ja  v  a2  s .  c o  m*/
        d = new XMLDecoder(in1, null, null);
        return d.readObject();
    } finally {
        if (null != d) {
            d.close();
        }
    }
}

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  ww w  . j a  va  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 ww  . j  a v a2  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 readBeanFromXml(String path) throws Exception {
    FileInputStream fis = null;//from  w  w  w .j  a va 2s .  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:fi.vm.sade.log.client.LoggerJms.java

/**
 * Decode XML to LogEvent. Encoded in "encode" method.
 *
 * @param xml/* w  w  w . j  a  v a  2s .  c  o m*/
 * @return
 */
public static LogEvent decode(String xml) {
    if (xml == null) {
        return null;
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
    XMLDecoder xmlDecoder = new XMLDecoder(bais);
    LogEvent logEvent = (LogEvent) xmlDecoder.readObject();
    xmlDecoder.close();

    return logEvent;
}

From source file:Main.java

/***
 * Deserialize a xml string into an object
 * /*w ww .ja v a 2 s. c o 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

/**
 * Reads an object from an XML file./*from  w  w  w  . jav  a 2  s  .  c o m*/
 * 
 * @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:com.npower.dl.FirmwareMimeTypeHelper.java

public static List<DownloadMimeType> load(InputStream in) throws IOException {
    XMLDecoder decoder = new XMLDecoder(in);
    List<DownloadMimeType> types = (List<DownloadMimeType>) decoder.readObject();
    return types;
}

From source file:Main.java

/**
 * Creates from the given xml string an java object.
 * /*w w w.j  a  va 2 s.  c  o  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 {
        InputStream is = new ByteArrayInputStream(xmlString.getBytes());
        dec = new XMLDecoder(is);

        obj = (T) dec.readObject();

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