Search in sources :

Example 26 with XMLDecoder

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?!";
        }
    }
}
Also used : Serializable(java.io.Serializable) FileContents(javax.jnlp.FileContents) BufferedInputStream(java.io.BufferedInputStream) XMLDecoder(java.beans.XMLDecoder) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with XMLDecoder

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);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XMLDecoder(java.beans.XMLDecoder) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 28 with XMLDecoder

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;
    }
}
Also used : XMLDecoder(java.beans.XMLDecoder)

Example 29 with XMLDecoder

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());
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with XMLDecoder

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());
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PatternSyntaxException(java.util.regex.PatternSyntaxException) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Aggregations

XMLDecoder (java.beans.XMLDecoder)54 ByteArrayInputStream (java.io.ByteArrayInputStream)28 IOException (java.io.IOException)19 BufferedInputStream (java.io.BufferedInputStream)18 XMLEncoder (java.beans.XMLEncoder)15 FileInputStream (java.io.FileInputStream)14 LinkedList (java.util.LinkedList)13 ExceptionListener (java.beans.ExceptionListener)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 File (java.io.File)8 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 AssertionFailedError (junit.framework.AssertionFailedError)5 Test (org.junit.Test)5 Test (org.junit.jupiter.api.Test)5 FileNotFoundException (java.io.FileNotFoundException)4 List (java.util.List)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 BufferedOutputStream (java.io.BufferedOutputStream)3 Serializable (java.io.Serializable)2