public static void storeHashMap(Map<String, List<String>> clusterMap) { File file = null; FileOutputStream fos = null; XMLEncoder encode = null; try { file = new File(BuildConstants.propFile); fos = new FileOutputStream(file); encode = new XMLEncoder(fos); encode.writeObject(clusterMap); log.info("Done Saving the Map to propFile.properties file"); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (encode != null) { encode.flush(); encode.close(); } if (fos != null) { fos.flush(); fos.close(); } } catch (Exception e) { e.printStackTrace(); } } }
@Test public void testAccountSerialization() throws Exception { Account a = ModelFactory.createAccount("Foo", ModelFactory.createAccountType("Cash", false)); a.setInterestRate(1234); a.setNotes("Foo bar notes"); a.setOverdraftCreditLimit(123); a.setStartingBalance(10000); ByteArrayOutputStream os = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(os); encoder.writeObject(a); encoder.flush(); encoder.close(); InputStream is = new ByteArrayInputStream(os.toByteArray()); XMLDecoder decoder = new XMLDecoder(is); Object o = decoder.readObject(); decoder.close(); if (!(o instanceof Account)) { throw new Exception("Deserialized object is not an account!"); } Account newAccount = (Account) o; assertEquals(a.getName(), newAccount.getName()); assertEquals(a.getAccountType(), newAccount.getAccountType()); assertEquals(a.getInterestRate(), newAccount.getInterestRate()); assertEquals(a.getNotes(), newAccount.getNotes()); assertEquals(a.getOverdraftCreditLimit(), newAccount.getOverdraftCreditLimit()); assertEquals(a.getStartingBalance(), newAccount.getStartingBalance()); }
/** * Persists the list of {@link AwaitedJob} to file * * @throws FileNotFoundException */ protected synchronized void saveAwaitedJobsToFile() throws FileNotFoundException { File statusFileBK = new File(statusFilename + ".BAK"); if (statusFileBK.isFile()) { statusFileBK.delete(); } if (statusFile != null && statusFile.isFile()) statusFile.renameTo(statusFileBK); try { statusFile = new File(statusFilename); XMLEncoder encoder = new XMLEncoder(new FileOutputStream(statusFile)); for (AwaitedJob aj : awaitedJobs.values()) { encoder.writeObject(aj); } encoder.flush(); encoder.close(); } catch (Throwable t) { logger.error( "Could not persist the list of awaited jobs. Some jobs output data might not be transfered after application restart ", t); // recover the status file from the backup file statusFile.delete(); statusFileBK.renameTo(statusFile); } }
public static String storeBean(Serializable bean, OutputStream out) { XMLEncoder encoder = null; try { encoder = new XMLEncoder(out, ContentContext.CHARACTER_ENCODING, true, 0); encoder.writeObject(bean); encoder.flush(); } finally { if (encoder != null) { encoder.close(); } } return null; }
/** * Get a by array by serializing the specified object as XML * * @param object an object */ public static byte[] marshallObject(Object object) { if (object == null) { throw new IllegalArgumentException("Null is not serializable"); } if (!Serializable.class.isAssignableFrom(object.getClass())) { throw new IllegalArgumentException("The object must be Serializable"); } ByteArrayOutputStream baOut = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(baOut); encoder.writeObject(object); encoder.flush(); encoder.close(); return baOut.toByteArray(); }
public static String storeBeanFromXML(Serializable bean) { ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(out, ContentContext.CHARACTER_ENCODING, true, 0); // XMLEncoder encoder = new XMLEncoder(out); encoder.writeObject(bean); encoder.flush(); encoder.close(); try { return new String(out.toByteArray(), ContentContext.CHARACTER_ENCODING); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
public static String storeBean(Serializable bean, File file) throws FileNotFoundException { OutputStream out = new FileOutputStream(file); XMLEncoder encoder = null; try { encoder = new XMLEncoder(out, ContentContext.CHARACTER_ENCODING, true, 0); encoder.writeObject(bean); encoder.flush(); } finally { closeResource(out); if (encoder != null) { encoder.close(); } } return null; }