enc.setExceptionListener(listener); Project p1 = new Project("foo"); enc.writeObject(p1);
enc.setExceptionListener(listener); Group g1 = new Group(); enc.writeObject(g1);
e.setExceptionListener(listener); e.writeObject(in); e.close();
/** Writes the object and CLOSES the stream. Uses the persistence delegate registered in this class. * @param os The stream to write to. Will be closed. * @param o The object to be serialized. * @param listener The listener to receive the exceptions if there are any. If <code>null</code> not used. */ static void writeXMLObject(OutputStream os, Object o, ExceptionListener listener) { ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(LayoutUtil.class.getClassLoader()); XMLEncoder encoder = new XMLEncoder(os); if (listener != null) encoder.setExceptionListener(listener); encoder.writeObject(o); encoder.close(); // Must be closed to write. Thread.currentThread().setContextClassLoader(oldClassLoader); }
/** * Encode data with the codec. * @param val The value. * @return The encoded object. */ public synchronized byte[] encode(Object val, ClassLoader classloader, Map<Class<?>, Object[]> info) { ByteArrayOutputStream bs = new ByteArrayOutputStream(); XMLEncoder e = new XMLEncoder(bs); e.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception e) { System.out.println("XML encoding ERROR: "); e.printStackTrace(); } }); Thread.currentThread().setContextClassLoader(classloader); // System.err.println("encoding with class loader: "+Thread.currentThread()+", "+Thread.currentThread().getContextClassLoader()); e.writeObject(val); e.close(); return bs.toByteArray(); }
/** * Serialize all the EventData items into an XML representation. * * @param map the Map to transform * @return an XML String containing all the EventData items. */ public static String toXML(Map<String, Object> map) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { XMLEncoder encoder = new XMLEncoder(baos); encoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception exception) { exception.printStackTrace(); } }); encoder.writeObject(map); encoder.close(); return baos.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Encode an object. * @param obj The object. * @throws IOException */ // public byte[] encode(Object val, ClassLoader classloader) public Object encode(Object val, ClassLoader classloader) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLEncoder enc = new XMLEncoder(baos); enc.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception e) { System.out.println("XML encoding ERROR: "); e.printStackTrace(); } }); enc.writeObject(val); enc.close(); try{baos.close();} catch(Exception e) {} return baos.toByteArray(); }
/** * Creates a new instance of XML Encoder pre-configured for Violet beans serailization * * @param out * @return configured encoder */ private XMLEncoder getXMLEncoder(OutputStream out) { XMLEncoder encoder = new XMLEncoder(out); encoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception ex) { ex.printStackTrace(); } }); configure(encoder); return encoder; }
/** * Write an object into a XML file. * * @param o Object to persist. */ @Override public void writeToFile(final T o) { try (OutputStream fos = Files.newOutputStream(this.file)) { try (XMLEncoder encoder = new XMLEncoder(fos)) { encoder.setExceptionListener(FileCreationException::new); encoder.writeObject(o); } } catch (IOException e) { throw new FileCreationException(e); } }
beanEncoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception exception) { System.out.println("XMLStore.save() got Exception: abort saving the preferences!");
beanEncoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception exception) { System.out.println("XMLStore.save() got Exception: abort saving the preferences!");
/** * To xml. * * @param obj * the obj * @return the string */ // //////////////////////////////////////////////////////////////////// public static String toXml(final Object obj) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); // XStream x = createXStream(); // String xml = x.toXML(obj); // return xml; final XMLEncoder e = new XMLEncoder(out); e.setExceptionListener(new XmlEncoderExceptionListener()); // e.setPersistenceDelegate(Object.class, new MyPersistenceDelegate()); e.writeObject(obj); e.close(); return out.toString(); // return null; }
e = new XMLEncoder(bst); e.setPersistenceDelegate(Rectangle.class, new RectanglePD()); e.setExceptionListener(el); e.writeObject(states); } finally {
/** * @see XMLEncoder */ @SuppressWarnings("resource") public static String bean2xml(final Object javaBean) throws SerializationException { Args.notNull("javaBean", javaBean); final ArrayList<Exception> exList = new ArrayList<Exception>(2); final FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); final XMLEncoder encoder = new XMLEncoder(bos); encoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(final Exception ex) { exList.add(ex); } }); encoder.writeObject(javaBean); encoder.close(); if (exList.size() > 0) throw new SerializationException("An error occured during XML serialization", exList.get(0)); return bos.toString(); }
public static void serialize(OutputStream out, Object o) { XMLEncoder e = new XMLEncoder(out); e.setExceptionListener(new EL()); PTFUtils.addPersistenceDelegates(e); e.writeObject(o); e.close(); }
persistenceDelegatesInitialized = true; e.setExceptionListener(el); e.writeObject(bean);
/** * {@inheritDoc} */ @Override public <T> int serialize(T obj, Class<T> type, OutputStream out) throws IOException { out = new CountingOutputStream(new BufferedOutputStream(out, getBufferSize())); EL el = new EL(); XMLEncoder enc = new XMLEncoder(out); try { enc.setExceptionListener(el); enc.writeObject(obj); enc.flush(); } finally { if (isCloseEnabled()) { enc.close(); } } IOException ioe = el.getIOException(); if (ioe != null) { throw ioe; } return ((CountingOutputStream)out).getCount(); }
enc.setExceptionListener(listener); enc.writeObject(original); enc.close();
/** * Serialize the whole query plan. */ public static void serializeQueryPlan(QueryPlan plan, OutputStream out) { XMLEncoder e = new XMLEncoder(out); e.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception e) { LOG.warn(org.apache.hadoop.util.StringUtils.stringifyException(e)); throw new RuntimeException("Cannot serialize the query plan", e); } }); // workaround for java 1.5 e.setPersistenceDelegate(ExpressionTypes.class, new EnumDelegate()); e.setPersistenceDelegate(GroupByDesc.Mode.class, new EnumDelegate()); e.setPersistenceDelegate(Operator.ProgressCounter.class, new EnumDelegate()); e.setPersistenceDelegate(org.datanucleus.sco.backed.Map.class, new MapDelegate()); e.setPersistenceDelegate(org.datanucleus.sco.backed.List.class, new ListDelegate()); e.writeObject(plan); e.close(); }
/** * Serialize the object. This helper function mainly makes sure that enums, * counters, etc are handled properly. */ private static void serializeObjectByJavaXML(Object plan, OutputStream out) { XMLEncoder e = new XMLEncoder(out); e.setExceptionListener(new ExceptionListener() { @Override public void exceptionThrown(Exception e) { LOG.warn(org.apache.hadoop.util.StringUtils.stringifyException(e)); throw new RuntimeException("Cannot serialize object", e); } }); // workaround for java 1.5 e.setPersistenceDelegate(ExpressionTypes.class, new EnumDelegate()); e.setPersistenceDelegate(GroupByDesc.Mode.class, new EnumDelegate()); e.setPersistenceDelegate(java.sql.Date.class, new DatePersistenceDelegate()); e.setPersistenceDelegate(Timestamp.class, new TimestampPersistenceDelegate()); e.setPersistenceDelegate(org.datanucleus.store.types.backed.Map.class, new MapDelegate()); e.setPersistenceDelegate(org.datanucleus.store.types.backed.List.class, new ListDelegate()); e.setPersistenceDelegate(CommonToken.class, new CommonTokenDelegate()); e.setPersistenceDelegate(Path.class, new PathDelegate()); e.writeObject(plan); e.close(); }