use of java.beans.XMLDecoder in project java-swing-tips by aterai.
the class LoadSaveTask method loadWindowState.
private static void loadWindowState(PersistenceService ps, URL codebase, WindowState windowState) {
try {
FileContents fc = ps.get(codebase);
try (XMLDecoder d = new XMLDecoder(new BufferedInputStream(fc.getInputStream()))) {
@SuppressWarnings("unchecked") Map<String, Serializable> map = (Map<String, Serializable>) d.readObject();
// d.close();
windowState.setSize((Dimension) map.get("size"));
windowState.setLocation((Point) map.get("location"));
// // Test:
// // ObjectInputStream d = new ObjectInputStream(appSettings.getInputStream());
// // WindowState cache = (WindowState) d.readObject();
// // Test:
// WindowState cache = (WindowState) map.get("setting");
// System.out.println("aaa: " + cache.getSize());
// System.out.println("aaa: " + cache.getLocation());
}
} catch (IOException ex) {
// create the cache
try {
long size = ps.create(codebase, 64_000);
System.out.println("Cache created - size: " + size);
} catch (IOException ignore) {
// throw new UncheckedIOException("Application codebase is not a valid URL?!", ignore);
assert false : "Application codebase is not a valid URL?!";
}
}
}
use of java.beans.XMLDecoder in project wcomponents by BorderTech.
the class MenuPanel method loadRecentList.
/**
* Reads the list of recently selected examples from a file on the file system.
*/
private void loadRecentList() {
recent.clear();
File file = new File(RECENT_FILE_NAME);
if (file.exists()) {
try (InputStream fileStream = new FileInputStream(file);
InputStream inputStream = new BufferedInputStream(fileStream);
XMLDecoder decoder = new XMLDecoder(inputStream)) {
List result = (List) decoder.readObject();
for (Object obj : result) {
if (obj instanceof ExampleData) {
recent.add((ExampleData) obj);
}
}
} catch (IOException ex) {
LogFactory.getLog(getClass()).error("Unable to load recent list", ex);
}
}
}
use of java.beans.XMLDecoder in project sulky by huxi.
the class XmlDecoder method decode.
@Override
public E decode(byte[] bytes) {
try (XMLDecoder decoder = createXMLDecoder(bytes)) {
Object result = decoder.readObject();
@SuppressWarnings({ "unchecked" }) E e = (E) result;
return e;
} catch (Throwable e) {
// silently ignore any problems
return null;
}
}
use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.
the class ConfigurationTest method serializationOrderTest.
/**
* Test for a serialization bug in configuration. The problem is that with
* this scenario the output is written in a way that when deserializing the
* input later on, we get {@link NullPointerException} trying to use
* {@link Group#compareTo(Group)}. This exception is caused by wrong order
* of serialization of
* {@link Group#getDescendants()}, {@link Group#getParent()} and
* {@link Project#getGroups()} where the backpointers in a {@link Project}
* to several {@link Group}s shall be stored in a set while this
* {@link Group} does not have a name yet (= {@code null}).
*
* @throws IOException I/O exception
* @see ClassUtil#remarkTransientFields(java.lang.Class)
* ClassUtil#remarkTransientFields() for suggested solution
*/
@Test
public void serializationOrderTest() throws IOException {
Project project = new Project("project");
Group apache = new Group("Apache", "test.*");
Group bsd = new Group("BSD", "test.*");
Group opensource = new Group("OpenSource", "test.*");
opensource.addGroup(apache);
opensource.addGroup(bsd);
bsd.addProject(project);
opensource.addProject(project);
project.getGroups().add(opensource);
project.getGroups().add(bsd);
Configuration cfg = new Configuration();
Configuration oldCfg = cfg;
cfg.addGroup(apache);
cfg.addGroup(bsd);
cfg.addGroup(opensource);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (XMLEncoder enc = new XMLEncoder(out)) {
enc.writeObject(cfg);
}
// Create an exception listener to detect errors while encoding and
// decoding
final LinkedList<Exception> exceptions = new LinkedList<>();
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
XMLDecoder dec = new XMLDecoder(in, null, exceptions::addLast)) {
cfg = (Configuration) dec.readObject();
assertNotNull(cfg);
// verify that the read didn't fail
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
assertEquals(oldCfg.getGroups(), cfg.getGroups());
}
}
use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.
the class GroupTest method testEncodeDecode.
/**
* Test that a {@code Group} instance can be encoded and decoded without
* errors.
*/
@Test
public void testEncodeDecode() {
// Create an exception listener to detect errors while encoding and
// decoding
final LinkedList<Exception> exceptions = new LinkedList<>();
ExceptionListener listener = exceptions::addLast;
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder enc = new XMLEncoder(out);
enc.setExceptionListener(listener);
Group g1 = new Group();
enc.writeObject(g1);
enc.close();
// verify that the write didn'abcd fail
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
XMLDecoder dec = new XMLDecoder(in, null, listener);
Group g2 = (Group) dec.readObject();
assertNotNull(g2);
dec.close();
// verify that the read didn'abcd fail
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
}
Aggregations