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:fi.vm.sade.log.client.LoggerJms.java

/**
 * Decode XML to LogEvent. Encoded in "encode" method.
 *
 * @param xml//from ww w.ja  v  a  2 s  .  com
 * @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

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;/*  w  w  w . ja 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

/**
 * Reads an object from an XML file./*from   w  w w .j av  a  2  s  .co  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:org.clickframes.testframes.TestResultsParser.java

public static ProjectTestResults parseTestResults(File zip) throws IOException {
    // tmp directory
    File tmpDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    tmpDirectory.mkdirs();//from  w  w  w.  j  a  va2s. c  o m

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
    ZipEntry entry;
    int BUFFER = 4096;
    while ((entry = zis.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        File destinationFile = new File(tmpDirectory, entry.getName());
        destinationFile.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(destinationFile);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }

    File firefoxDir = new File(tmpDirectory, "firefox");

    if (!firefoxDir.exists()) {
        throw new RuntimeException("Invalid test results zip file uploaded. firefox directory does not exist.");
    }

    File testResultsXmlFile = new File(tmpDirectory, "testResults.xml");

    XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(testResultsXmlFile)));
    ProjectTestResults projectTestResults = (ProjectTestResults) d.readObject();
    d.close();

    return projectTestResults;
}

From source file:Main.java

/**
 * Creates from the given xml string an java object.
 * //  www . j  ava 2  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

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   ww  w .  java2  s . c  o 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

/**
 * Creates from the given xml string an java object.
 * //w  ww .ja  v  a  2  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 {
        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

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();//from  w  w  w  .  j a  v a2 s  . c  o  m
        }
    });

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

From source file:org.icefaces.sample.location.CityDictionary.java

/**
 * Reads the zipped xml city cityDictionary and loads it into memory.
 *
 * @throws java.io.IOException If anything goes wrong acquiring or readying the zip file of city data.
 *//*from  w w  w.jav  a 2s .  com*/
private static void init() throws IOException {

    if (!initialized) {

        initialized = true;

        // Loading of the resource must be done the "JSF way" so that
        // it is agnostic about it's environment (portlet vs servlet).
        // First we get the resource as an InputStream
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        InputStream is = ec.getResourceAsStream(DATA_RESOURCE_PATH);

        //is a zip file.
        ZipInputStream zipStream = new ZipInputStream(is);

        //Prime the stream by reading the first entry.  The way
        //we have it currently configured, there should only be
        //one.
        ZipEntry firstEntry = zipStream.getNextEntry();

        //Pass the ZipInputStream to the XMLDecoder so that it
        //can read in the list of cities and associated data.
        XMLDecoder xDecoder = new XMLDecoder(zipStream);
        List cityList = (List) xDecoder.readObject();

        //Close the decoder and the stream.
        xDecoder.close();
        zipStream.close();

        if (cityList == null) {
            throw new IOException();
        }

        cityDictionary = new ArrayList(cityList.size());
        City tmpCity = null;
        for (int i = 0, max = cityList.size(); i < max; i++) {
            tmpCity = (City) cityList.get(i);
            if (tmpCity != null && tmpCity.getCity() != null) {
                cityDictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();
        Collections.sort(cityDictionary, LABEL_COMPARATOR);
    }

}

From source file:org.icefaces.application.showcase.view.bean.examples.component.selectInputText.CityDictionary.java

/**
 * Reads the zipped xml city cityDictionary and loads it into memory.
 *///from   ww  w .j a v  a 2  s  .c o m
private static void init() throws IOException {

    if (!initialized) {

        initialized = true;

        // Loading of the resource must be done the "JSF way" so that
        // it is agnostic about it's environment (portlet vs servlet).
        // First we get the resource as an InputStream
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        InputStream is = ec.getResourceAsStream(DATA_RESOURCE_PATH);

        //Wrap the InputStream as a ZipInputStream since it
        //is a zip file.
        ZipInputStream zipStream = new ZipInputStream(is);

        //Prime the stream by reading the first entry.  The way
        //we have it currently configured, there should only be
        //one.
        ZipEntry firstEntry = zipStream.getNextEntry();

        //Pass the ZipInputStream to the XMLDecoder so that it
        //can read in the list of cities and associated data.
        XMLDecoder xDecoder = new XMLDecoder(zipStream);
        List cityList = (List) xDecoder.readObject();

        //Close the decoder and the stream.
        xDecoder.close();
        zipStream.close();

        if (cityList == null) {
            throw new IOException();
        }

        cityDictionary = 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) {
                cityDictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();

        Collections.sort(cityDictionary, LABEL_COMPARATOR);
    }

}