Example usage for java.beans XMLDecoder close

List of usage examples for java.beans XMLDecoder close

Introduction

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

Prototype

public void close() 

Source Link

Document

This method closes the input stream associated with this stream.

Usage

From source file:Main.java

/***
 * Deserialize a xml string into an object
 * /*from w  w  w.j a va2s .c om*/
 * @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:org.scantegrity.IRVTally.IRVTally.java

/**
 * Load a contest from file. /*  ww  w. j  a va2s . c o m*/
 * @param l_fname
 * @throws FileNotFoundException 
 */
@SuppressWarnings("unchecked")
public static Vector<Contest> loadContest(String p_fname) throws FileNotFoundException {
    Vector<Contest> l_res;
    XMLDecoder e;
    e = new XMLDecoder(new BufferedInputStream(new FileInputStream(p_fname)));
    l_res = (Vector<Contest>) e.readObject();
    e.close();
    return l_res;
}

From source file:com.icesoft.icefaces.tutorial.component.autocomplete.AutoCompleteDictionary.java

private static void init() {
    // Raw list of xml cities.
    List cityList = null;/*from   w  w w .j a v  a  2s.c om*/

    // load the city dictionary from the compressed xml file.

    // get the path of the compressed file
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    String basePath = session.getServletContext().getRealPath("/WEB-INF/resources");
    basePath += "/city.xml.zip";

    // extract the file
    ZipEntry zipEntry;
    ZipFile zipFile;
    try {
        zipFile = new ZipFile(basePath);
        zipEntry = zipFile.getEntry("city.xml");
    } catch (Exception e) {
        log.error("Error retrieving records", e);
        return;
    }

    // get the xml stream and decode it.
    if (zipFile.size() > 0 && zipEntry != null) {
        try {
            BufferedInputStream dictionaryStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
            XMLDecoder xDecoder = new XMLDecoder(dictionaryStream);
            // get the city list.
            cityList = (List) xDecoder.readObject();
            dictionaryStream.close();
            zipFile.close();
            xDecoder.close();
        } catch (ArrayIndexOutOfBoundsException e) {
            log.error("Error getting city list, not city objects", e);
            return;
        } catch (IOException e) {
            log.error("Error getting city list", e);
            return;
        }
    }

    // Finally load the object from the xml file.
    if (cityList != null) {
        dictionary = new ArrayList(cityList.size());
        City tmpCity;
        for (int i = 0, max = cityList.size(); i < max; i++) {
            tmpCity = (City) cityList.get(i);
            if (tmpCity != null && tmpCity.getCity() != null) {
                dictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();
        // finally sort the list
        Collections.sort(dictionary, LABEL_COMPARATOR);
    }

}

From source file:resources.ResourceManager.java

public static int loadLayoutTable(File file, LayoutTableModel tableModel) {
    int size = 0;
    try {//ww  w .j  a  va 2  s. co m
        final XMLDecoder decoder = new XMLDecoder(new FileInputStream(file));
        final List<LayoutTableItem> listFromFile = (List<LayoutTableItem>) decoder.readObject();
        tableModel.setLayoutTableItemList(listFromFile);
        decoder.close();

        size = listFromFile.size();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return size;
}

From source file:net.sourceforge.fenixedu.util.tests.ResponseExternalization.java

private static Response getResponse(String xmlResponse) {
    XMLDecoder decoder = null;
    try {/*from   ww w. j  a v a 2s.co  m*/
        decoder = new XMLDecoder(new ByteArrayInputStream(xmlResponse.getBytes(CharEncoding.UTF_8)));
    } catch (UnsupportedEncodingException e1) {
        logger.error(e1.getMessage(), e1);
    }
    Response response = null;
    try {
        response = (Response) decoder.readObject();
    } catch (ArrayIndexOutOfBoundsException e) {
        logger.error(e.getMessage(), e);
    }
    decoder.close();
    return response;
}

From source file:com.snp.site.init.SystemInit.java

/** demo? ??????? */
static public DataBaseConfig getObjectDemo(String xmlObjectString) throws FileNotFoundException, IOException {

    // ??//  w  w  w . ja  v a2s .  c o  m
    // 1.?
    XMLDecoder xmlDecoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(xmlObjectString)));
    DataBaseConfig objectDemo = (DataBaseConfig) xmlDecoder.readObject();
    xmlDecoder.close();
    /*
     * 2.???,?????????
     * ?DEMO??XML??DEMO??
     */
    return objectDemo;
}

From source file:com.snp.site.init.SystemInit.java

/**
 * XML?? ?/*from w w  w .  j  a  v  a  2  s.co m*/
 * 
 */
static public LanmuConfig getLanmuObjectFromXml(String xmlObjectString) throws Exception {
    try {

        XMLDecoder xmlDecoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(xmlObjectString)));
        LanmuConfig lanmuObject = (LanmuConfig) xmlDecoder.readObject();
        xmlDecoder.close();
        if (lanmuObject == null)
            throw new Exception();
        return lanmuObject;
    } catch (Exception e) {
        throw e;
    }
}

From source file:com.swingtech.commons.util.ClassUtil.java

public static Object getObjectFromXML(final InputStream inStream) {
    final XMLDecoder d = new XMLDecoder(new BufferedInputStream(inStream));

    final Object result = d.readObject();
    d.close();
    return result;
}

From source file:org.dawnsci.common.richbeans.beans.BeanUI.java

/**
 * Bean from string using standard java serialization, useful for tables of beans with serialized strings. Used
 * externally to the GDA./*  w  ww .  j a  va2s.c  om*/
 * 
 * @param xml
 * @return the bean
 */
public static Object getBean(final String xml, final ClassLoader loader) throws Exception {

    final ClassLoader original = Thread.currentThread().getContextClassLoader();
    final ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    try {
        Thread.currentThread().setContextClassLoader(loader);
        XMLDecoder d = new XMLDecoder(new BufferedInputStream(stream));
        final Object bean = d.readObject();
        d.close();
        return bean;
    } finally {
        Thread.currentThread().setContextClassLoader(original);
        stream.close();
    }
}

From source file:com.clustercontrol.plugin.impl.AsyncTask.java

/**
 * ??XMLSerializable???/*  w ww  .  j a  va2s  .  c om*/
 * @param xml ??XML
 * @return Serializable
 * @throws IOException
 */
public static Serializable decodeXML(String xml) throws IOException {
    ByteArrayInputStream bais = null;
    XMLDecoder dec = null;
    Serializable obj = null;

    try {
        bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        dec = new XMLDecoder(bais);
        obj = (Serializable) dec.readObject();
    } catch (UnsupportedEncodingException e) {
        log.warn(e);
    } finally {
        if (dec != null) {
            dec.close();
        }
        if (bais != null) {
            bais.close();
        }
    }

    return obj;
}