java.beans.XMLEncoder Java Examples

The following examples show how to use java.beans.XMLEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Test4880633.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    // run thread a few time
    // object stays the same but NullPointerException appears randomly
    // on dual proccessor a lock is generated
    for (int i = 0; i < THREAD_LENGTH; i++) {
        // create XMLEncoder to ByteArrayOutputStream
        // this is to exclude file locking problems
        XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
        encoder.setExceptionListener(this);
        // write the object
        // will see randomly null pointer exceptions
        // a bug as object is same through different encode phases
        encoder.writeObject(this.object);
        //close encoder
        encoder.close();
    }
    System.out.println(Thread.currentThread().getName() + " is finished");
}
 
Example #2
Source File: Test4646747.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    XMLEncoder encoder = new XMLEncoder(System.out);
    encoder.setPersistenceDelegate(Test4646747.class, new MyPersistenceDelegate());
    // WARNING: This can eat up a lot of memory
    Object[] obs = new Object[10000];
    while (obs != null) {
        try {
            obs = new Object[obs.length + obs.length / 3];
        }
        catch (OutOfMemoryError error) {
            obs = null;
        }
    }
    PersistenceDelegate pd = encoder.getPersistenceDelegate(Test4646747.class);
    if (!(pd instanceof MyPersistenceDelegate))
        throw new Error("persistence delegate has been lost");
}
 
Example #3
Source File: Test4880633.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    // run thread a few time
    // object stays the same but NullPointerException appears randomly
    // on dual proccessor a lock is generated
    for (int i = 0; i < THREAD_LENGTH; i++) {
        // create XMLEncoder to ByteArrayOutputStream
        // this is to exclude file locking problems
        XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
        encoder.setExceptionListener(this);
        // write the object
        // will see randomly null pointer exceptions
        // a bug as object is same through different encode phases
        encoder.writeObject(this.object);
        //close encoder
        encoder.close();
    }
    System.out.println(Thread.currentThread().getName() + " is finished");
}
 
Example #4
Source File: Test4880633.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    // run thread a few time
    // object stays the same but NullPointerException appears randomly
    // on dual proccessor a lock is generated
    for (int i = 0; i < THREAD_LENGTH; i++) {
        // create XMLEncoder to ByteArrayOutputStream
        // this is to exclude file locking problems
        XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
        encoder.setExceptionListener(this);
        // write the object
        // will see randomly null pointer exceptions
        // a bug as object is same through different encode phases
        encoder.writeObject(this.object);
        //close encoder
        encoder.close();
    }
    System.out.println(Thread.currentThread().getName() + " is finished");
}
 
Example #5
Source File: Test4646747.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    XMLEncoder encoder = new XMLEncoder(System.out);
    encoder.setPersistenceDelegate(Test4646747.class, new MyPersistenceDelegate());
    // WARNING: This can eat up a lot of memory
    Object[] obs = new Object[10000];
    while (obs != null) {
        try {
            obs = new Object[obs.length + obs.length / 3];
        }
        catch (OutOfMemoryError error) {
            obs = null;
        }
    }
    PersistenceDelegate pd = encoder.getPersistenceDelegate(Test4646747.class);
    if (!(pd instanceof MyPersistenceDelegate))
        throw new Error("persistence delegate has been lost");
}
 
Example #6
Source File: AbstractTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static void test(AbstractTest object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setPersistenceDelegate(
            object.getClass(),
            new DefaultPersistenceDelegate(new String[] {"value"}));

    encoder.writeObject(object);
    encoder.close();

    System.out.print(output);

    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    XMLDecoder decoder = new XMLDecoder(input);
    AbstractTest result = (AbstractTest) decoder.readObject();
    decoder.close();

    if (object.getValue() != result.getValue())
        throw new Error("Should be " + object);
}
 
Example #7
Source File: BrowserMDIFrame.java    From ApkToolPlus with Apache License 2.0 6 votes vote down vote up
private void saveWorkspaceToFile(File file) {

		try {
			FileOutputStream fos = new FileOutputStream(file);
			XMLEncoder encoder = new XMLEncoder(fos);
			encoder.writeObject(config);
			encoder.close();
			recentMenu.addRecentWorkspace(file);
		} catch (FileNotFoundException e) {
			GUIHelper.showMessage(this, "An error occured while saving to "
					+ file.getPath(), JOptionPane.ERROR_MESSAGE);
		}
		GUIHelper.showMessage(this, "Workspace saved to " + file.getPath(),
				JOptionPane.INFORMATION_MESSAGE);
		actionSaveWorkspaceAs.setEnabled(true);
	}
 
Example #8
Source File: AbstractTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static void test(AbstractTest object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setPersistenceDelegate(
            object.getClass(),
            new DefaultPersistenceDelegate(new String[] {"value"}));

    encoder.writeObject(object);
    encoder.close();

    System.out.print(output);

    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    XMLDecoder decoder = new XMLDecoder(input);
    AbstractTest result = (AbstractTest) decoder.readObject();
    decoder.close();

    if (object.getValue() != result.getValue())
        throw new Error("Should be " + object);
}
 
Example #9
Source File: XMLSerializer.java    From development with Apache License 2.0 6 votes vote down vote up
private static void setPersistenceDelegates(XMLEncoder encoder,
        Class<?>[] types) {
    if (types != null && types.length > 0) {
        PersistenceDelegate persistenceDelegate = new EnumPersistenceDelegate();
        for (int i = 0; i < types.length; i++) {
            encoder.setPersistenceDelegate(types[i], persistenceDelegate);
        }
    }

    // Handle "BiGDecimal" manually (has no default constructor)
    encoder.setPersistenceDelegate(BigDecimal.class,
            new BigDecimalPersistenceDelegate());

    encoder.setPersistenceDelegate(byte[].class,
            new ByteArrayPersistenceDelegate());
    encoder.setPersistenceDelegate(UUID.class, new UUIDDelegate());
}
 
Example #10
Source File: Test4646747.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    XMLEncoder encoder = new XMLEncoder(System.out);
    encoder.setPersistenceDelegate(Test4646747.class, new MyPersistenceDelegate());
    // WARNING: This can eat up a lot of memory
    Object[] obs = new Object[10000];
    while (obs != null) {
        try {
            obs = new Object[obs.length + obs.length / 3];
        }
        catch (OutOfMemoryError error) {
            obs = null;
        }
    }
    PersistenceDelegate pd = encoder.getPersistenceDelegate(Test4646747.class);
    if (!(pd instanceof MyPersistenceDelegate))
        throw new Error("persistence delegate has been lost");
}
 
Example #11
Source File: Test4646747.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    XMLEncoder encoder = new XMLEncoder(System.out);
    encoder.setPersistenceDelegate(Test4646747.class, new MyPersistenceDelegate());
    // WARNING: This can eat up a lot of memory
    Object[] obs = new Object[10000];
    while (obs != null) {
        try {
            obs = new Object[obs.length + obs.length / 3];
        }
        catch (OutOfMemoryError error) {
            obs = null;
        }
    }
    PersistenceDelegate pd = encoder.getPersistenceDelegate(Test4646747.class);
    if (!(pd instanceof MyPersistenceDelegate))
        throw new Error("persistence delegate has been lost");
}
 
Example #12
Source File: Test4880633.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    // run thread a few time
    // object stays the same but NullPointerException appears randomly
    // on dual proccessor a lock is generated
    for (int i = 0; i < THREAD_LENGTH; i++) {
        // create XMLEncoder to ByteArrayOutputStream
        // this is to exclude file locking problems
        XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
        encoder.setExceptionListener(this);
        // write the object
        // will see randomly null pointer exceptions
        // a bug as object is same through different encode phases
        encoder.writeObject(this.object);
        //close encoder
        encoder.close();
    }
    System.out.println(Thread.currentThread().getName() + " is finished");
}
 
Example #13
Source File: Test4880633.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    // run thread a few time
    // object stays the same but NullPointerException appears randomly
    // on dual proccessor a lock is generated
    for (int i = 0; i < THREAD_LENGTH; i++) {
        // create XMLEncoder to ByteArrayOutputStream
        // this is to exclude file locking problems
        XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
        encoder.setExceptionListener(this);
        // write the object
        // will see randomly null pointer exceptions
        // a bug as object is same through different encode phases
        encoder.writeObject(this.object);
        //close encoder
        encoder.close();
    }
    System.out.println(Thread.currentThread().getName() + " is finished");
}
 
Example #14
Source File: AbstractTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
static void test(AbstractTest object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setPersistenceDelegate(
            object.getClass(),
            new DefaultPersistenceDelegate(new String[] {"value"}));

    encoder.writeObject(object);
    encoder.close();

    System.out.print(output);

    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    XMLDecoder decoder = new XMLDecoder(input);
    AbstractTest result = (AbstractTest) decoder.readObject();
    decoder.close();

    if (object.getValue() != result.getValue())
        throw new Error("Should be " + object);
}
 
Example #15
Source File: AbstractTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static void test(AbstractTest object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setPersistenceDelegate(
            object.getClass(),
            new DefaultPersistenceDelegate(new String[] {"value"}));

    encoder.writeObject(object);
    encoder.close();

    System.out.print(output);

    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    XMLDecoder decoder = new XMLDecoder(input);
    AbstractTest result = (AbstractTest) decoder.readObject();
    decoder.close();

    if (object.getValue() != result.getValue())
        throw new Error("Should be " + object);
}
 
Example #16
Source File: AbstractTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static void test(AbstractTest object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setPersistenceDelegate(
            object.getClass(),
            new DefaultPersistenceDelegate(new String[] {"value"}));

    encoder.writeObject(object);
    encoder.close();

    System.out.print(output);

    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    XMLDecoder decoder = new XMLDecoder(input);
    AbstractTest result = (AbstractTest) decoder.readObject();
    decoder.close();

    if (object.getValue() != result.getValue())
        throw new Error("Should be " + object);
}
 
Example #17
Source File: PriceModelResource.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Get the price model data for the given context and locales
 * 
 * @return the price model data as an XML encoded list of strings
 */
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getPriceModel() {

    FileBilling fb = new FileBilling();
    splitParametersToList(contextKeys);
    splitParametersToList(contextValues);

    List<String> priceModelContent = fb.getPriceModel(contextKeys,
            contextValues, locales);


    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (XMLEncoder xmlEncoder = new XMLEncoder(bos)) {
        xmlEncoder.writeObject(priceModelContent);
    }

    String serializedList = "";
    try {
        serializedList = bos.toString(StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
    }
    return Response.ok(serializedList).build();
}
 
Example #18
Source File: Test5023550.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void initialize(XMLEncoder encoder) {
    encoder.setOwner(this.owner);
    encoder.setPersistenceDelegate(A.class, new ADelegate());
    encoder.setPersistenceDelegate(B.class, new BDelegate());
    encoder.setPersistenceDelegate(C.class, new CDelegate());
}
 
Example #19
Source File: XmlProtocolHandlerImpl.java    From ikasoa with MIT License 5 votes vote down vote up
private <E> String formatXML(E entity) {
	try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
			BufferedOutputStream bos = new BufferedOutputStream(baos)) {
		XMLEncoder encoder = new XMLEncoder(bos);
		encoder.writeObject(entity);
		encoder.close();
		return baos.toString();
	} catch (IOException e) {
		Log.error(e.getMessage());
		return String.valueOf(V);
	}
}
 
Example #20
Source File: UpdateXmlSerializerImpl.java    From bigtable-sql with Apache License 2.0 5 votes vote down vote up
/**
 * @see net.sourceforge.squirrel_sql.client.update.xmlbeans.UpdateXmlSerializer#write(net.sourceforge.squirrel_sql.client.update.xmlbeans.ChannelXmlBean, java.lang.String)
 */
public void write(ChannelXmlBean channelBean, String filename)
      throws FileNotFoundException {
   XMLEncoder os = getXmlEncoder(filename);
   os.writeObject(channelBean);
   os.close();
}
 
Example #21
Source File: Test6329581.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private byte[] encode(String name) throws Exception {
    Object object = loadClass(name).newInstance();
    validate(object);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(out);
    encoder.setExceptionListener(this);
    encoder.writeObject(object);
    encoder.close();
    return out.toByteArray();
}
 
Example #22
Source File: AbstractTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private byte[] writeObject(Object object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setExceptionListener(this);
    initialize(encoder);
    encoder.writeObject(object);
    encoder.close();
    return output.toByteArray();
}
 
Example #23
Source File: Test4968523.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void test(Class<?> type, PersistenceDelegate pd) {
    Encoder encoder1 = new Encoder();
    Encoder encoder2 = new XMLEncoder(System.out);

    PersistenceDelegate pd1 = encoder1.getPersistenceDelegate(type);
    PersistenceDelegate pd2 = encoder2.getPersistenceDelegate(type);

    encoder1.setPersistenceDelegate(type, pd);

    if (pd1 == encoder1.getPersistenceDelegate(type))
        throw new Error("first persistence delegate is not changed");

    if (pd2 != encoder2.getPersistenceDelegate(type))
        throw new Error("second persistence delegate is changed");
}
 
Example #24
Source File: Test4936682.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected void initialize(XMLEncoder encoder) {
    encoder.setPersistenceDelegate(
            OuterClass.InnerClass.class,
            new DefaultPersistenceDelegate() {
                protected Expression instantiate(Object oldInstance, Encoder out) {
                    OuterClass.InnerClass inner = (OuterClass.InnerClass) oldInstance;
                    OuterClass outer = inner.getOuter();
                    return new Expression(inner, outer, "getInner", new Object[0]);
                }
            }
    );
}
 
Example #25
Source File: Test4822050.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.writeObject(new JLabel("hello")); // NON-NLS: test message
    encoder.close();

    byte[] buffer = baos.toByteArray();
    for (int i = 0; i < THREADS; i++)
        start(buffer);
}
 
Example #26
Source File: Test4679556.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void initialize(XMLEncoder encoder) {
    encoder.setPersistenceDelegate(C.class, new DefaultPersistenceDelegate() {
        protected Expression instantiate(Object oldInstance, Encoder out) {
            C c = (C) oldInstance;
            return new Expression(c, c.getX(), "createC", new Object[] {});
        }
    });
}
 
Example #27
Source File: XMLStore.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Save the current state of the Preferences tree to the given OutputStream.
 */
public void save(OutputStream out) throws java.io.IOException {
  outputExceptionMessage = null;

  // the OutputMunger strips off the XMLEncoder header
  OutputMunger bos = new OutputMunger(out);
  PrintWriter pw = new PrintWriter(new OutputStreamWriter(bos, StandardCharsets.UTF_8));

  XMLEncoder beanEncoder = new XMLEncoder(bos);
  beanEncoder.setExceptionListener(new ExceptionListener() {
    public void exceptionThrown(Exception exception) {
      System.out.println("XMLStore.save() got Exception: abort saving the preferences!");
      exception.printStackTrace();
      outputExceptionMessage = exception.getMessage();
    }
  });

  pw.printf("<?xml version='1.0' encoding='UTF-8'?>%n");
  pw.printf("<preferences EXTERNAL_XML_VERSION='1.0'>%n");
  if (!rootPrefs.isUserNode())
    pw.printf("  <root type='system'>%n");
  else
    pw.printf("  <root type='user'>%n");

  Indent indent = new Indent(2);
  indent.incr();
  writeXmlNode(bos, pw, rootPrefs, beanEncoder, indent);
  if (outputExceptionMessage != null)
    throw new IOException(outputExceptionMessage);

  pw.printf("  </root>%n");
  pw.printf("</preferences>%n");
  pw.flush();
}
 
Example #28
Source File: Test4968523.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void test(Class<?> type, PersistenceDelegate pd) {
    Encoder encoder1 = new Encoder();
    Encoder encoder2 = new XMLEncoder(System.out);

    PersistenceDelegate pd1 = encoder1.getPersistenceDelegate(type);
    PersistenceDelegate pd2 = encoder2.getPersistenceDelegate(type);

    encoder1.setPersistenceDelegate(type, pd);

    if (pd1 == encoder1.getPersistenceDelegate(type))
        throw new Error("first persistence delegate is not changed");

    if (pd2 != encoder2.getPersistenceDelegate(type))
        throw new Error("second persistence delegate is changed");
}
 
Example #29
Source File: Test5023552.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected void initialize(XMLEncoder encoder) {
    encoder.setPersistenceDelegate(Container.class, new PersistenceDelegate() {
        protected Expression instantiate(Object oldInstance, Encoder out) {
            Container container = (Container) oldInstance;
            Component component = container.getComponent();
            return new Expression(container, component, "create", new Object[] {component});
        }
    });
}
 
Example #30
Source File: AbstractTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private byte[] writeObject(Object object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setExceptionListener(this);
    initialize(encoder);
    encoder.writeObject(object);
    encoder.close();
    return output.toByteArray();
}