Search in sources :

Example 51 with XMLDecoder

use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.

the class ConfigurationTest method testEncodeDecode.

@Test
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);
    Configuration configuration1 = new Configuration();
    configuration1.setInterval(500);
    configuration1.setSearchTimeout(1000);
    configuration1.setConnectTimeout(42);
    configuration1.setCountLimit(10);
    configuration1.setServers(new ArrayList<>(List.of(new LdapServer("http://server.com"))));
    WebHooks webHooks = new WebHooks();
    WebHook hook = new WebHook();
    hook.setContent("foo");
    hook.setURI("http://localhost:8080/source/api/v1/messages");
    webHooks.setFail(hook);
    configuration1.setWebHooks(webHooks);
    enc.writeObject(configuration1);
    enc.close();
    // verify that the write didn't fail
    if (!exceptions.isEmpty()) {
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Configuration configuration2 = (Configuration) dec.readObject();
    assertNotNull(configuration2);
    assertEquals(configuration1.getXMLRepresentationAsString(), configuration2.getXMLRepresentationAsString());
    dec.close();
    // verify that the read didn't fail
    if (!exceptions.isEmpty()) {
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
}
Also used : WebHook(opengrok.auth.plugin.util.WebHook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) LdapServer(opengrok.auth.plugin.ldap.LdapServer) XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) WebHooks(opengrok.auth.plugin.util.WebHooks) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 52 with XMLDecoder

use of java.beans.XMLDecoder in project JavaYouShouldKnow by CFMystery.

the class JavaXMLSerializer method deserialize.

@Override
@SuppressWarnings("unchecked")
public <T> T deserialize(byte[] data, Class<T> clazz) {
    XMLDecoder xmlDecoder = new XMLDecoder(new ByteArrayInputStream(data));
    Object obj = xmlDecoder.readObject();
    xmlDecoder.close();
    return (T) obj;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder)

Example 53 with XMLDecoder

use of java.beans.XMLDecoder in project Java8 by huhuhuHR.

the class ObjectToXMLUtil method objectXmlEncode.

@SneakyThrows
public static List objectXmlEncode(String path) {
    var file = new File(path);
    @Cleanup var fis = new FileInputStream(file);
    @Cleanup var decode = new XMLDecoder(fis);
    return (List<Object>) decode.readObject();
}
Also used : lombok.experimental.var(lombok.experimental.var) XMLDecoder(java.beans.XMLDecoder) List(java.util.List) File(java.io.File) Cleanup(lombok.Cleanup) FileInputStream(java.io.FileInputStream) SneakyThrows(lombok.SneakyThrows)

Example 54 with XMLDecoder

use of java.beans.XMLDecoder in project shiro by apache.

the class XmlSerializer method deserialize.

/**
 * Deserializes the specified <code>serialized</code> source back into an Object by using a
 * {@link java.io.ByteArrayInputStream ByteArrayInputStream} to wrap the argument and then decode this
 * stream via an {@link java.beans.XMLDecoder XMLDecoder}, where the
 * {@link java.beans.XMLDecoder#readObject() readObject} call results in the original Object to return.
 * @param serialized the byte[] array representation of the XML encoded output.
 * @return the original source Object in reconstituted form.
 */
public Object deserialize(byte[] serialized) {
    if (serialized == null) {
        throw new IllegalArgumentException("Argument cannot be null.");
    }
    ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
    XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(bis));
    Object o = decoder.readObject();
    decoder.close();
    return o;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) XMLDecoder(java.beans.XMLDecoder)

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