ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLEncoder enc = new XMLEncoder(out); enc.setExceptionListener(listener); Project p1 = new Project("foo"); enc.writeObject(p1); enc.close(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); XMLDecoder dec = new XMLDecoder(in, null, listener); Project p2 = (Project) dec.readObject(); assertNotNull(p2); dec.close();
try { output = File.createTempFile("oghist", null, dir); try (FileOutputStream out = new FileOutputStream(output); XMLEncoder e = new XMLEncoder(new GZIPOutputStream( new BufferedOutputStream(out)))) { e.setPersistenceDelegate(File.class, new FilePersistenceDelegate()); e.writeObject(history);
public void encodeObject(OutputStream out) { try (XMLEncoder e = new XMLEncoder(new BufferedOutputStream(out))) { e.writeObject(this); } }
/** * Serializes the specified <code>source</code> into a byte[] array by using the * {@link java.beans.XMLEncoder XMLEncoder} to encode the object out to a * {@link java.io.ByteArrayOutputStream ByteArrayOutputStream}, where the resulting byte[] array is returned. * @param source the Object to convert into a byte[] array. * @return the byte[] array representation of the XML encoded output. */ public byte[] serialize(Object source) { if (source == null) { String msg = "argument cannot be null."; throw new IllegalArgumentException(msg); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(bos)); encoder.writeObject(source); encoder.close(); return bos.toByteArray(); }
/** * 将可序列化的对象转换为XML写入文件,已经存在的文件将被覆盖<br> * Writes serializable object to a XML file. Existing file will be overwritten * * @param dest 目标文件 * @param bean 对象 * @throws IOException IO异常 */ public static void writeObjectAsXml(File dest, Object bean) throws IOException { XMLEncoder xmlenc = null; try { xmlenc = new XMLEncoder(FileUtil.getOutputStream(dest)); xmlenc.writeObject(bean); } finally { // 关闭XMLEncoder会相应关闭OutputStream IoUtil.close(xmlenc); } }
/** * 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(); }
File xmlFile = new File(directory, template.getID() + ".xml"); try { XMLEncoder e = new XMLEncoder(new BufferedOutputStream( new FileOutputStream(xmlFile))); e.writeObject(template); e.close(); } catch (IOException ioe) { ioe.printStackTrace();
/** * Verify that encoding of Group class does not contain transient members. * @throws Exception exception */ @Test public void testTransientKeywordGroups() throws Exception { Group foo = new Group("foo", "foo.*"); Group bar = new Group("bar", "bar.*"); Configuration cfg = new Configuration(); cfg.addGroup(foo); foo.addGroup(bar); cfg.addGroup(bar); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (XMLEncoder enc = new XMLEncoder(out)) { enc.writeObject(cfg); } // In this test we are no so much interested in exceptions during the // XML decoding as that is covered by the {@code serializationOrderTest} // test. try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); Handler handler = new Handler(); saxParser.parse(new BufferedInputStream(in), handler); } }
public static String serialize(Object o) { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); XMLEncoder xmlEncoder = new XMLEncoder(bOut); xmlEncoder.writeObject(o); xmlEncoder.close(); return bOut.toString(); }
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(testXML))); e.setExceptionListener(listener); e.writeObject(in); e.close(); XMLDecoder d = new XMLDecoder(new FileInputStream(testXML)); IgnoredNames in2 = (IgnoredNames) d.readObject(); d.close();
cfg.addGroup(opensource); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (XMLEncoder enc = new XMLEncoder(out)) { enc.writeObject(cfg); try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); XMLDecoder dec = new XMLDecoder(in, null, (Exception e) -> { exceptions.addLast(e); })) { cfg = (Configuration) dec.readObject(); assertNotNull(cfg);
public String serialize(Object o) { String s; ByteArrayOutputStream stream = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( stream ) ); encoder.writeObject(o); encoder.close(); s = stream.toString(); return s; } private Object deserialize(String s) { XMLDecoder decoder = new XMLDecoder( new ByteArrayInputStream( s.getBytes() ) ); return decoder.readObject(); }
/** * Serializes this gradebook archive into an xml document */ public String archive() { if(log.isDebugEnabled()) log.debug("GradebookArchive.archive() called"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(baos)); encoder.writeObject(this); encoder.flush(); String xml = baos.toString(); if(log.isDebugEnabled()) log.debug("GradebookArchive.archive() finished"); return xml; }
XMLEncoder e = new XMLEncoder(new FileOutputStream(file)); e.setOwner(new PersistenceOwner(baseURL)); e.setPersistenceDelegate(AbstractPainter.Interpolation.class, new TypeSafeEnumPersistenceDelegate()); e.setPersistenceDelegate(RectanglePainter.Style.class, new TypeSafeEnumPersistenceDelegate()); e.setPersistenceDelegate(AbstractLayoutPainter.HorizontalAlignment.class, new TypeSafeEnumPersistenceDelegate()); e.setPersistenceDelegate(AbstractLayoutPainter.VerticalAlignment.class, new TypeSafeEnumPersistenceDelegate()); e.setPersistenceDelegate(AbstractPainter.class, new AbstractPainterDelegate()); e.setPersistenceDelegate(ImagePainter.class, new ImagePainterDelegate()); e.setPersistenceDelegate(RenderingHints.class, new RenderingHintsDelegate()); e.setPersistenceDelegate(GradientPaint.class, new GradientPaintDelegate()); e.setPersistenceDelegate(Arc2D.Float.class, new Arc2DDelegate()); e.setPersistenceDelegate(Arc2D.Double.class, new Arc2DDelegate()); e.setPersistenceDelegate(CubicCurve2D.Float.class, new CubicCurve2DDelegate()); e.setPersistenceDelegate(CubicCurve2D.Double.class, new CubicCurve2DDelegate()); e.setPersistenceDelegate(Ellipse2D.Float.class, new Ellipse2DDelegate()); e.setPersistenceDelegate(Ellipse2D.Double.class, new Ellipse2DDelegate()); e.setPersistenceDelegate(Line2D.Float.class, new Line2DDelegate()); e.setPersistenceDelegate(Line2D.Double.class, new Line2DDelegate()); e.setPersistenceDelegate(Point2D.Float.class, new Point2DDelegate()); e.setPersistenceDelegate(Point2D.Double.class, new Point2DDelegate()); e.setPersistenceDelegate(QuadCurve2D.Float.class, new QuadCurve2DDelegate()); e.setPersistenceDelegate(QuadCurve2D.Double.class, new QuadCurve2DDelegate()); e.setPersistenceDelegate(Rectangle2D.Float.class, new Rectangle2DDelegate()); e.setPersistenceDelegate(Rectangle2D.Double.class, new Rectangle2DDelegate()); e.setPersistenceDelegate(RoundRectangle2D.Float.class, new RoundRectangle2DDelegate()); e.setPersistenceDelegate(RoundRectangle2D.Double.class, new RoundRectangle2DDelegate());
configurations.put(conf.getConfiguredClass(),conf.storeToMap(new HashMap<String,Object>())); FileOutputStream os = new FileOutputStream(temp); XMLEncoder xmlEnc = new XMLEncoder(os); xmlEnc.setExceptionListener(new ExceptionListener(){ @Override public void exceptionThrown(Exception e) { e.printStackTrace(); }}); xmlEnc.setPersistenceDelegate(DefaultListModel.class, new DefaultPersistenceDelegate() { protected void initialize(Class clazz, xmlEnc.writeObject(configurations); xmlEnc.close(); dstCh = new FileOutputStream(finalDest).getChannel(); dstCh.transferFrom(srcCh, 0, srcCh.size()); }catch(Exception e){e.printStackTrace();}
/** * {@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(); }
/** * Serialize the mapredWork object to an output stream. DO NOT use this to write to standard * output since it closes the output stream. DO USE mapredWork.toXML() instead. */ public static void serializeMapRedWork(MapredWork w, OutputStream out) { XMLEncoder e = new XMLEncoder(out); // workaround for java 1.5 e.setPersistenceDelegate(ExpressionTypes.class, new EnumDelegate()); e.setPersistenceDelegate(GroupByDesc.Mode.class, new EnumDelegate()); e.writeObject(w); e.close(); }
XMLEncoder encoder = new XMLEncoder(); XMLDecoder decoder = new XMLDecoder();
/** * 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); } }
/** * 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; }