Example usage for java.beans XMLEncoder close

List of usage examples for java.beans XMLEncoder close

Introduction

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

Prototype

public void close() 

Source Link

Document

This method calls flush , writes the closing postamble and then closes the output stream associated with this stream.

Usage

From source file:Roster.Rosters.java

private JSONObject toXML() {
    String xml = null;//from   w  w  w. j  av a  2s .c o m
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(out);
        encoder.writeObject(students); // serialize to XML
        encoder.close();
        xml = out.toString(); // stringify
    } catch (Exception e) {
    }
    //System.out.println(xml.trim());

    JSONObject jobt = XML.toJSONObject(xml);
    //System.out.println(jobt);

    return jobt;
}

From source file:com.jaspersoft.jasperserver.export.modules.repository.OlapUnitViewOptionsDataProvider.java

public InputStream getData(ExporterModuleContext exportContext, Resource resource) {

    OlapUnit unit = (OlapUnit) resource;
    InputStream dataStream;/*from  www. j ava2s.c om*/
    Object viewOptions = unit.getOlapViewOptions();
    if (viewOptions == null) {
        dataStream = null;
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(baos));
        e.writeObject(unit.getOlapViewOptions());
        e.flush();
        e.close();
        dataStream = new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray()));
    }
    return dataStream;
}

From source file:PersistentFrameTest.java

 public void save()
{
   if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
   {/*  w w w  .  j a va2s  . co  m*/
      try
      {
         File file = chooser.getSelectedFile();
         XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
         encoder.writeObject(frame);
         encoder.close();
      }
      catch (IOException e)
      {
         JOptionPane.showMessageDialog(null, e);
      }
   }
}

From source file:org.geomajas.plugin.deskmanager.domain.types.XmlSerialisationType.java

private String toXmlString(Object value) {
    if (value == null) {
        return null;
    }//from ww  w. j  a va 2  s  .co m
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(baos);
        encoder.writeObject(value);
        encoder.close();
        String result = baos.toString(ENCODING);
        baos.close();
        return result;
    } catch (IOException e) {
        e.printStackTrace();
        IllegalArgumentException ex = new IllegalArgumentException("cannot disassemble the object", e);
        throw ex;
    }
}

From source file:net.commerce.zocalo.experiment.config.SessionConfigurationTest.java

private byte[] writeObjToXmlBytes(Object config) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //        FileOutputStream fos = null;
    //        try {
    //            fos = new FileOutputStream("temp.xml");
    //        } catch (FileNotFoundException e) {
    //            e.printStackTrace();
    //        }/*  ww  w.j av  a2 s .com*/
    //        XMLEncoder fenc = new XMLEncoder(fos);
    //        fenc.writeObject(config);
    //        fenc.close();
    XMLEncoder enc = new XMLEncoder(baos);
    enc.writeObject(config);
    enc.close();
    return baos.toByteArray();
}

From source file:org.cesecore.util.HashMapTest.java

@SuppressWarnings("rawtypes")
@Test//from  w  w  w.j a va 2 s .co m
public void testHashMapNormal() throws Exception {
    HashMap<String, Comparable> a = new HashMap<String, Comparable>();
    a.put("foo0", Boolean.FALSE);
    a.put("foo1", "fooString");
    a.put("foo2", Integer.valueOf(2));
    a.put("foo3", Boolean.TRUE);

    // Write to XML
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.writeObject(a);
    encoder.close();
    String data = baos.toString("UTF8");
    //log.error(data);

    XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data.getBytes("UTF8")));
    HashMap<?, ?> b = (HashMap<?, ?>) decoder.readObject();
    decoder.close();
    assertEquals(((Boolean) b.get("foo0")).booleanValue(), false);
    assertEquals(((Boolean) b.get("foo3")).booleanValue(), true);
    assertEquals(((String) b.get("foo1")), "fooString");
    assertEquals(((Integer) b.get("foo2")).intValue(), 2);

}

From source file:org.scantegrity.scanner.Scanner.java

private static void writeCounters() {
    ListIterator<String> it = c_outDirs.listIterator();
    while (it.hasNext()) {
        try {/*from   www.  ja v  a2s.co m*/
            XMLEncoder l_countFile = new XMLEncoder(
                    new BufferedOutputStream(new FileOutputStream(it.next() + File.separator + "count.xml")));

            l_countFile.writeObject("Scan Count");
            l_countFile.writeObject(c_scanCount);
            l_countFile.writeObject("Ballot Count");
            l_countFile.writeObject(c_ballotCount);
            l_countFile.writeObject("Error Count");
            l_countFile.writeObject(c_errorCount);
            l_countFile.close();
        } catch (FileNotFoundException e) {
            c_log.log(Level.SEVERE, "Unable to create count.xml");
        }
    }
}

From source file:org.cesecore.util.HashMapTest.java

@SuppressWarnings("rawtypes")
@Test//from ww w. ja  v a2s . co m
public void testHashMapStrangeCharsSafe() throws Exception {
    HashMap<String, Comparable> h = new HashMap<String, Comparable>();
    h.put("foo0", Boolean.FALSE);
    h.put("foo1", "\0001\0002fooString");
    h.put("foo2", Integer.valueOf(2));
    h.put("foo3", Boolean.TRUE);
    h.put("foo4", "");
    HashMap<Object, Object> a = new Base64PutHashMap();
    a.putAll(h);

    // Write to XML
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.writeObject(a);
    encoder.close();
    String data = baos.toString("UTF8");
    //log.error(data);

    try {
        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data.getBytes("UTF8")));
        HashMap<?, ?> b = (HashMap<?, ?>) decoder.readObject();
        decoder.close();
        @SuppressWarnings("unchecked")
        HashMap<Object, Object> c = new Base64GetHashMap(b);
        assertEquals(((Boolean) c.get("foo0")).booleanValue(), false);
        assertEquals(((Boolean) c.get("foo3")).booleanValue(), true);
        assertEquals(((String) c.get("foo1")), "\0001\0002fooString");
        assertEquals(((String) c.get("foo4")), "");
        assertEquals(((Integer) c.get("foo2")).intValue(), 2);

    } catch (ClassCastException e) {
        assertTrue(false);
    }
}

From source file:org.cesecore.util.HashMapTest.java

@SuppressWarnings("rawtypes")
@Test/*www. j a  va 2s  .c  o  m*/
public void testHashMapNormalCharsSafe() throws Exception {
    HashMap<String, Comparable> h = new HashMap<String, Comparable>();
    h.put("foo0", Boolean.FALSE);
    h.put("foo1", "fooString");
    h.put("foo2", Integer.valueOf(2));
    h.put("foo3", Boolean.TRUE);
    h.put("foo4", "");
    HashMap<Object, Object> a = new Base64PutHashMap();
    a.putAll(h);

    // Write to XML
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.writeObject(a);
    encoder.close();
    String data = baos.toString("UTF8");
    //log.error(data);

    try {
        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data.getBytes("UTF8")));
        HashMap<?, ?> b = (HashMap<?, ?>) decoder.readObject();
        decoder.close();
        @SuppressWarnings("unchecked")
        HashMap<Object, Object> c = new Base64GetHashMap(b);
        assertEquals(((Boolean) c.get("foo0")).booleanValue(), false);
        assertEquals(((Boolean) c.get("foo3")).booleanValue(), true);
        assertEquals(((String) c.get("foo4")), "");
        assertEquals(((String) c.get("foo1")), "fooString");
        assertEquals(((Integer) c.get("foo2")).intValue(), 2);

    } catch (ClassCastException e) {
        assertTrue(false);
    }
}

From source file:org.cesecore.util.HashMapTest.java

@SuppressWarnings("rawtypes")
@Test//from  w  ww.ja  v a 2 s .c  o  m
public void testHashMapStrangeChars() throws Exception {
    HashMap<String, Comparable> a = new HashMap<String, Comparable>();
    a.put("foo0", Boolean.FALSE);
    a.put("foo1", "\0001\0002fooString");
    a.put("foo2", Integer.valueOf(2));
    a.put("foo3", Boolean.TRUE);

    // Write to XML
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.writeObject(a);
    encoder.close();
    String data = baos.toString("UTF8");
    //log.error(data);

    try {
        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data.getBytes("UTF8")));
        HashMap<?, ?> b = (HashMap<?, ?>) decoder.readObject();
        decoder.close();
        assertEquals(((Boolean) b.get("foo0")).booleanValue(), false);
        // We can get two different errors, I don't know if it is different java versions or what...
        // The important thing is that we do expect an error to occur here
    } catch (ClassCastException e) {
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        return;
    }
    String javaver = System.getProperty("java.version");
    System.out.println(javaver);
    if (StringUtils.contains(javaver, "1.6") || StringUtils.contains(javaver, "1.7")
            || StringUtils.contains(javaver, "1.8")) {
        // In java 1.6 the above does work because it encodes the special characters
        //   <string><char code="#0"/>1<char code="#0"/>2fooString</string> 
        assertTrue(true);
    } else {
        // In java 1.5 the above does not work, because it will insert bad xml-characters 
        // so the test will fail if we got here.
        assertTrue(false);
    }
}