Search in sources :

Example 16 with XMLEncoder

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

the class Message method write.

/**
     * Serialize the message as XML and send it into the socket.
     *
     * @param host host
     * @param port port number
     * @throws IOException
     *
     * @see #throwIfError(int c, String message)
     *
     * @return possible output for this application, null if no output
     */
public byte[] write(String host, int port) throws IOException {
    try (Socket sock = new Socket(host, port)) {
        try (XMLEncoder e = new XMLEncoder(new XmlEofOutputStream(sock.getOutputStream()))) {
            e.writeObject(this);
        }
        try (InputStream input = sock.getInputStream();
            ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            int ret, r;
            if ((ret = input.read()) < 0) {
                throwIfError(ret, "unexpected end of socket while waiting for ack");
            }
            byte[] buffer = new byte[4096];
            while ((r = input.read(buffer)) >= 0) {
                out.write(buffer, 0, r);
            }
            throwIfError(ret, out.toString());
            if (out.size() > 0) {
                return out.toByteArray();
            }
        }
    }
    return null;
}
Also used : XMLEncoder(java.beans.XMLEncoder) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XmlEofOutputStream(org.opensolaris.opengrok.util.XmlEofOutputStream) Socket(java.net.Socket)

Example 17 with XMLEncoder

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

the class ProjectTest method testEncodeDecode.

/**
     * Test that a {@code Project} instance can be encoded and decoded
     * without errors. Bug #3077.
     */
@Test
public void testEncodeDecode() {
    // Create an exception listener to detect errors while encoding and
    // decoding
    final LinkedList<Exception> exceptions = new LinkedList<Exception>();
    ExceptionListener listener = new ExceptionListener() {

        public void exceptionThrown(Exception e) {
            exceptions.addLast(e);
        }
    };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder enc = new XMLEncoder(out);
    enc.setExceptionListener(listener);
    Project p1 = new Project();
    enc.writeObject(p1);
    enc.close();
    // verify that the write didn't fail
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Project p2 = (Project) dec.readObject();
    assertNotNull(p2);
    dec.close();
    // verify that the read didn't fail
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssertionFailedError(junit.framework.AssertionFailedError) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 18 with XMLEncoder

use of java.beans.XMLEncoder in project antlrworks by antlr.

the class XJDataXML method writeData.

@Override
public void writeData() throws IOException {
    XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(getFile())));
    e.writeObject(dictionary);
    customWriteData(e);
    e.close();
}
Also used : XMLEncoder(java.beans.XMLEncoder)

Example 19 with XMLEncoder

use of java.beans.XMLEncoder in project jdk8u_jdk by JetBrains.

the class Test4822050 method main.

public static void main(String[] args) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    // NON-NLS: test message
    encoder.writeObject(new JLabel("hello"));
    encoder.close();
    byte[] buffer = baos.toByteArray();
    for (int i = 0; i < THREADS; i++) start(buffer);
}
Also used : XMLEncoder(java.beans.XMLEncoder) JLabel(javax.swing.JLabel) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 20 with XMLEncoder

use of java.beans.XMLEncoder in project jdk8u_jdk by JetBrains.

the class Test4646747 method main.

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");
}
Also used : XMLEncoder(java.beans.XMLEncoder) DefaultPersistenceDelegate(java.beans.DefaultPersistenceDelegate) PersistenceDelegate(java.beans.PersistenceDelegate)

Aggregations

XMLEncoder (java.beans.XMLEncoder)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 XMLDecoder (java.beans.XMLDecoder)8 BufferedOutputStream (java.io.BufferedOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 FileOutputStream (java.io.FileOutputStream)6 IOException (java.io.IOException)6 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 ExceptionListener (java.beans.ExceptionListener)4 PersistenceDelegate (java.beans.PersistenceDelegate)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 AssertionFailedError (junit.framework.AssertionFailedError)4 DefaultPersistenceDelegate (java.beans.DefaultPersistenceDelegate)3 Encoder (java.beans.Encoder)3 Expression (java.beans.Expression)2 BufferedInputStream (java.io.BufferedInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2