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(); }
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,
/** * 将可序列化的对象转换为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); } }
/** * 将可序列化的对象转换为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); } }
/** * 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); } }
XMLEncoder enc = new XMLEncoder(out); enc.setExceptionListener(listener); Project p1 = new Project("foo");
XMLEncoder enc = new XMLEncoder(out); enc.setExceptionListener(listener); Group g1 = new Group();
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(testXML))); e.setExceptionListener(listener); e.writeObject(in);
try (XMLEncoder enc = new XMLEncoder(out)) { enc.writeObject(cfg);
File xmlFile = new File(directory, template.getID() + ".xml"); try { XMLEncoder e = new XMLEncoder(new BufferedOutputStream( new FileOutputStream(xmlFile))); e.writeObject(template);
import java.beans.XMLEncoder; import java.beans.XMLDecoder; import java.io.*; public class XMLSerializer { public static void write(String[][] f, String filename) throws Exception{ XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( new FileOutputStream(filename))); encoder.writeObject(f); encoder.close(); } public static String[][] read(String filename) throws Exception { XMLDecoder decoder = new XMLDecoder(new BufferedInputStream( new FileInputStream(filename))); String[][] o = (String[][])decoder.readObject(); decoder.close(); return o; } }
private XMLEncoder getXmlEncoder(String filename) throws FileNotFoundException { XMLEncoder result = null; BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(filename)); result = new XMLEncoder(os); result.setPersistenceDelegate(ArtifactAction.class, enumDelegate); return result; } }
/** * Constructs an <code>XMLEncoder</code> and upgrades it to support other non-JavaBean classes. * @param out The stream to which the XML representation of the objects will be sent. * @return A new <code>XMLEncoder</code> which supports encoding of other other non-JavaBean classes. * @see XMLEncoder * @see #upgradeEncoder(Encoder) */ public static XMLEncoder createUpgradedXMLEncoder(OutputStream out) { final XMLEncoder xmlEncoder = new XMLEncoder(out); //create a new XML encoder upgradeEncoder(xmlEncoder); //upgrade the encoder return xmlEncoder; //return the new upgraded encoder }
protected XMLEncoder getEncoder(File changeset) throws FileNotFoundException { XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(new File( changeset.getParent(), changeset.getName() + outputSuffix)))); encoder.setPersistenceDelegate(UUID.class, new java.beans.PersistenceDelegate() { protected Expression instantiate(Object oldInstance, Encoder out) { return new Expression(oldInstance, oldInstance.getClass(), "fromString", new Object[] { oldInstance.toString() }); } }); return encoder; }
public static String serialize(Object o) { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); XMLEncoder xmlEncoder = new XMLEncoder(bOut); xmlEncoder.writeObject(o); xmlEncoder.close(); return bOut.toString(); }
public static void saveToFile(String filename, Map<String, Object> map) throws IOException { FileOutputStream os = new FileOutputStream(filename); XMLEncoder encoder = new XMLEncoder(os); encoder.writeObject(map); encoder.close(); os.close(); }
/** {@inheritDoc} */ public void encode(Object source, OutputStream target) throws IOException { XMLEncoder xmlOut = new XMLEncoder(target); xmlOut.writeObject(source); xmlOut.close(); }
NPair fe = new NPair(); fe.setNumber1(12); fe.setNumber2(13); FileOutputStream fos1 = new FileOutputStream("d:\\ser.xml"); java.beans.XMLEncoder xe1 = new java.beans.XMLEncoder(fos1); xe1.writeObject(fe); xe1.flush(); xe1.close();
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(); }