List of usage examples for java.beans XMLEncoder writeObject
public void writeObject(Object o)
From source file:org.cesecore.util.HashMapTest.java
@SuppressWarnings("rawtypes") @Test//from ww w .j av a2s . c o 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//from w ww. j a va 2 s. com 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//w w w . j a v a2 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); } }
From source file:edu.wisc.my.portlets.bookmarks.dao.file.FileSystemBookmarkStore.java
/** * @see edu.wisc.my.portlets.bookmarks.dao.BookmarkStore#storeBookmarkSet(edu.wisc.my.portlets.bookmarks.domain.BookmarkSet) *//*from w w w . j av a2 s. co m*/ public void storeBookmarkSet(BookmarkSet bookmarkSet) { if (bookmarkSet == null) { throw new IllegalArgumentException("AddressBook may not be null"); } final File storeFile = this.getStoreFile(bookmarkSet.getOwner(), bookmarkSet.getName()); try { final FileOutputStream fos = new FileOutputStream(storeFile); final BufferedOutputStream bos = new BufferedOutputStream(fos); final XMLEncoder e = new XMLEncoder(bos); try { e.writeObject(bookmarkSet); } finally { e.close(); } } catch (FileNotFoundException fnfe) { final String errorMsg = "Error storing BookmarkSet='" + bookmarkSet + "' to file='" + storeFile + "'"; logger.error(errorMsg, fnfe); throw new DataAccessResourceFailureException(errorMsg, fnfe); } }
From source file:org.ejbca.extra.db.SubMessages.java
/** * Method used to persist the set of submessages * @return a String representation of the data *///from ww w . j a v a 2 s . c om String save() { String retval = null; ArrayList savearray = new ArrayList(); Iterator<ISubMessage> iter = submessages.iterator(); while (iter.hasNext()) { ISubMessage next = iter.next(); savearray.add(next.saveData()); } try { java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(savearray); byte[] messagedata = baos.toByteArray(); if (isSigned) { messagedata = ExtRAMsgHelper.signData(userKey, userCert, messagedata); } if (isEncrypted) { messagedata = ExtRAMsgHelper.encryptData(encCert, messagedata); } java.io.ByteArrayOutputStream baos2 = new java.io.ByteArrayOutputStream(); java.beans.XMLEncoder encoder = new java.beans.XMLEncoder(baos2); encoder.writeObject(Boolean.valueOf(isSigned)); encoder.writeObject(Boolean.valueOf(isEncrypted)); encoder.writeObject(new String(Base64.encode(messagedata))); encoder.close(); retval = baos2.toString("UTF8"); } catch (Exception e) { log.error("Error writing persistent SubMessages.", e); } return retval; }
From source file:net.chaosserver.timelord.data.XmlDataReaderWriter.java
/** * Writes out the timelordData object to the default filename in the * user's home directory. Also generates a backup version of the file. * * @param timelordData the data to write to file * @param outputFile the file to output to * @throws TimelordDataException indicates an error writing the * data out to file.// w ww .j a v a 2 s .c o m */ public void writeTimelordData(TimelordData timelordData, File outputFile) throws TimelordDataException { Calendar yesterday = Calendar.getInstance(); yesterday.add(Calendar.DAY_OF_YEAR, -1); File homeDirectory = new File(System.getProperty("user.home")); try { FileOutputStream fileOutputStream = new FileOutputStream(outputFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); XMLEncoder xmlEncoder = new XMLEncoder(bufferedOutputStream); xmlEncoder.writeObject(timelordData); xmlEncoder.close(); File backupFile = new File(homeDirectory, (DEFAULT_FILENAME + "." + startWeekFormat.format(yesterday.getTime()) + DEFAULT_EXTENSION + ".gzip")); fileOutputStream = new FileOutputStream(backupFile); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); GZIPOutputStream zipOutputStream = new GZIPOutputStream(bufferedOutputStream); xmlEncoder = new XMLEncoder(zipOutputStream); xmlEncoder.writeObject(timelordData); xmlEncoder.close(); } catch (FileNotFoundException e) { throw new TimelordDataException("Failed to output", e); } catch (IOException e) { throw new TimelordDataException("Failed to output", e); } }
From source file:lu.lippmann.cdb.graph.GraphUtil.java
/** * //from ww w . j a va 2 s. co m * @param g * @return */ public static String saveGraphWithOperation(final GraphWithOperations g) { final ArrayList<GraphOperation> operations = new ArrayList<GraphOperation>(g.getOperations()); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final XMLEncoder xmlEncoder = new XMLEncoder(baos); xmlEncoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(final Exception e) { throw new IllegalStateException(e); } }); xmlEncoder.writeObject(new GraphTO(g.getId(), operations, g.getUntitledVertexCount(), g.getUntitledEdgeCount(), g.getVariables())); xmlEncoder.close(); return baos.toString(); }
From source file:org.shaman.database.Benchmark.java
/** * prints the file size//from w ww . j a v a 2 s . co m * @param roots the roots from the different passes */ private void printFileSizes(Record... roots) throws IOException { //result array int size; String format = "%1$,11d"; System.out.println("passes database serial compressed xml"); //save it for (int i = 0; i < roots.length; i++) { Record root = roots[i]; System.out.print("pass " + (i + 1) + " "); //database ArrayOutput ao = new ArrayOutput(); Database db = new Database(root); db.save(ao, FailOnErrorHandler.INSTANCE); size = ao.getTotalSize(); System.out.printf(format, size); System.out.print("B "); //uncompressed serialization CountingOutputStream out = new CountingOutputStream(new NullOutputStream()); ObjectOutputStream dos = new ObjectOutputStream(out); dos.writeObject(root); dos.flush(); size = out.getCount(); System.out.printf(format, size); System.out.print("B "); //compressed serialization out.resetCount(); dos = new ObjectOutputStream(new GZIPOutputStream(out)); dos.writeObject(root); dos.flush(); size = out.getCount(); System.out.printf(format, size); System.out.print("B "); //xml out.resetCount(); XMLEncoder xml = new XMLEncoder(out); xml.writeObject(root); xml.flush(); size = out.getCount(); System.out.printf(format, size); System.out.println("B"); } }
From source file:psidev.psi.mi.filemakers.xmlMaker.structure.FlatFile.java
public void save(XMLEncoder oos) { oos.writeObject(new Boolean(firstLineForTitles)); oos.writeObject(lineSeparator);//from www .j a v a 2 s .co m oos.writeObject(lineSeparator); oos.writeObject(separators); }
From source file:ddf.catalog.source.solr.provider.SolrProviderExtensibleMetacards.java
@Test public void queryInteger() throws UnsupportedQueryException { Query query = new QueryImpl(getFilterBuilder().attribute(intField).greaterThan().number(intFieldValue - 1)); QueryRequest request = new QueryRequestImpl(query); SourceResponse response = provider.query(request); assertEquals(1, response.getResults().size()); Metacard resultMetacard = response.getResults().get(0).getMetacard(); assertThat(resultMetacard.getAttribute(Metacard.ID), notNullValue()); assertThat(resultMetacard.getAttribute(doubleField).getValue(), equalTo(doubleFieldValue)); assertThat(resultMetacard.getAttribute(intField).getValue(), equalTo(intFieldValue)); assertThat(resultMetacard.getAttribute(floatField).getValue(), equalTo(floatFieldValue)); assertThat(resultMetacard.getAttribute(longField).getValue(), equalTo(longFieldValue)); assertThat(resultMetacard.getAttribute(byteField).getValue(), equalTo(byteFieldValue)); assertThat(resultMetacard.getAttribute(booleanField).getValue(), equalTo(true)); assertThat(resultMetacard.getAttribute(dateField).getValue(), equalTo(dateFieldValue)); assertThat(resultMetacard.getAttribute(geoField).getValue(), equalTo(geoFieldValue)); assertThat(resultMetacard.getAttribute(shortField).getValue(), equalTo(shortFieldValue)); assertThat(resultMetacard.getAttribute(objectField).getValue(), instanceOf(BevelBorder.class)); /*// w w w. j a va2 s . c o m * Going to use the XMLEncoder. If it writes out the objects the same way in xml, then they * are the same. */ ByteArrayOutputStream beveledBytesStreamFromSolr = new ByteArrayOutputStream(); XMLEncoder solrXMLEncoder = new XMLEncoder(new BufferedOutputStream(beveledBytesStreamFromSolr)); solrXMLEncoder.writeObject((resultMetacard.getAttribute(objectField).getValue())); solrXMLEncoder.close(); ByteArrayOutputStream beveledBytesStreamOriginal = new ByteArrayOutputStream(); XMLEncoder currendEncoder = new XMLEncoder(new BufferedOutputStream(beveledBytesStreamOriginal)); currendEncoder.writeObject(objectFieldValue); currendEncoder.close(); assertThat(beveledBytesStreamFromSolr.toByteArray(), equalTo(beveledBytesStreamOriginal.toByteArray())); }