java.beans.XMLEncoder类的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(119)

本文整理了Java中java.beans.XMLEncoder类的一些代码示例,展示了XMLEncoder类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XMLEncoder类的具体详情如下:
包路径:java.beans.XMLEncoder
类名称:XMLEncoder

XMLEncoder介绍

暂无

代码示例

代码示例来源:origin: oracle/opengrok

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();

代码示例来源:origin: oracle/opengrok

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);

代码示例来源:origin: oracle/opengrok

public void encodeObject(OutputStream out) {
  try (XMLEncoder e = new XMLEncoder(new BufferedOutputStream(out))) {
    e.writeObject(this);
  }
}

代码示例来源:origin: apache/shiro

/**
 * 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();
}

代码示例来源:origin: looly/hutool

/**
 * 将可序列化的对象转换为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);
  }
}

代码示例来源:origin: net.sourceforge.jadex/jadex-platform-base

/**
   *  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();
  }

代码示例来源:origin: bobbylight/RSyntaxTextArea

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();

代码示例来源:origin: oracle/opengrok

/**
 * 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);
  }
}

代码示例来源:origin: openpnp/openpnp

public static String serialize(Object o) {
  ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  XMLEncoder xmlEncoder = new XMLEncoder(bOut);
  xmlEncoder.writeObject(o);
  xmlEncoder.close();
  return bOut.toString();
}

代码示例来源:origin: oracle/opengrok

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();

代码示例来源:origin: oracle/opengrok

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);

代码示例来源:origin: stackoverflow.com

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();
}

代码示例来源:origin: sakaiproject/sakai

/**
 * 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;
}

代码示例来源:origin: org.swinglabs/swingx-beaninfo

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());

代码示例来源:origin: jtrfp/terminal-recall

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();}

代码示例来源:origin: jboss-switchyard/core

/**
 * {@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();
}

代码示例来源:origin: org.apache.hadoop.hive/hive-exec

/**
 * 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();
}

代码示例来源:origin: stackoverflow.com

XMLEncoder encoder = new XMLEncoder();
XMLDecoder decoder = new XMLDecoder();

代码示例来源:origin: be.yildiz-games/common-file-xml

/**
 * 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);
  }
}

代码示例来源:origin: violetumleditor/violetumleditor

/**
 * 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;
}

相关文章