Search in sources :

Example 11 with XMLEncoder

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

the class Test4880633 method run.

public void run() {
    // 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");
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with XMLEncoder

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

the class SimpleServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final String msg = req.getParameter("input");
    // the first call needs to be concurrent
    //bean.setMessage(msg);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final CountDownLatch latch = new CountDownLatch(1);
    final Future<String>[] futures = new Future[2];
    for (int i = 0; i < futures.length; i++) {
        futures[i] = executor.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                try {
                    return bean.echo(latch, msg);
                } finally {
                    // the second concurrent call will throw ConcurrentAccessException
                    // so now we make the first call proceed
                    latch.countDown();
                }
            }
        });
    }
    final List<String> results = new LinkedList<String>();
    final List<Throwable> exceptions = new LinkedList<Throwable>();
    for (int i = 0; i < futures.length; i++) {
        try {
            String result = futures[i].get(10, SECONDS);
            results.add(result);
        } catch (ExecutionException e) {
            e.printStackTrace();
            exceptions.add(e.getCause());
        } catch (InterruptedException e) {
            e.printStackTrace();
            exceptions.add(e);
        } catch (TimeoutException e) {
            e.printStackTrace();
            exceptions.add(e);
        }
    }
    // make a 'nice' report
    PrintWriter writer = resp.getWriter();
    XMLEncoder encoder = new XMLEncoder(new WriterOutputStream(writer));
    encoder.writeObject(results);
    encoder.writeObject(exceptions);
    encoder.writeObject(sharedContext);
    encoder.close();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) WriterOutputStream(org.jboss.stdio.WriterOutputStream) Callable(java.util.concurrent.Callable) LinkedList(java.util.LinkedList) XMLEncoder(java.beans.XMLEncoder) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) PrintWriter(java.io.PrintWriter)

Example 13 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 14 with XMLEncoder

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

the class FileHistoryCache method writeHistoryToFile.

/**
     * Store history in file on disk.
     * @param dir directory where the file will be saved
     * @param history history to store
     * @param cacheFile the file to store the history to
     * @throws HistoryException
     */
private void writeHistoryToFile(File dir, History history, File cacheFile) throws HistoryException {
    // We have a problem that multiple threads may access the cache layer
    // at the same time. Since I would like to avoid read-locking, I just
    // serialize the write access to the cache file. The generation of the
    // cache file would most likely be executed during index generation, and
    // that happens sequencial anyway....
    // Generate the file with a temporary name and move it into place when
    // I'm done so I don't have to protect the readers for partially updated
    // files...
    final File output;
    try {
        output = File.createTempFile("oghist", null, dir);
        try (FileOutputStream out = new FileOutputStream(output);
            XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new GZIPOutputStream(out)))) {
            e.setPersistenceDelegate(File.class, new FilePersistenceDelegate());
            e.writeObject(history);
        }
    } catch (IOException ioe) {
        throw new HistoryException("Failed to write history", ioe);
    }
    synchronized (lock) {
        if (!cacheFile.delete() && cacheFile.exists()) {
            if (!output.delete()) {
                LOGGER.log(Level.WARNING, "Failed to remove temporary history cache file");
            }
            throw new HistoryException("Cachefile exists, and I could not delete it.");
        }
        if (!output.renameTo(cacheFile)) {
            if (!output.delete()) {
                LOGGER.log(Level.WARNING, "Failed to remove temporary history cache file");
            }
            throw new HistoryException("Failed to rename cache tmpfile.");
        }
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 15 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)

Aggregations

XMLEncoder (java.beans.XMLEncoder)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 XMLDecoder (java.beans.XMLDecoder)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 ExceptionListener (java.beans.ExceptionListener)4 PersistenceDelegate (java.beans.PersistenceDelegate)4 BufferedOutputStream (java.io.BufferedOutputStream)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 AssertionFailedError (junit.framework.AssertionFailedError)4 DefaultPersistenceDelegate (java.beans.DefaultPersistenceDelegate)3 Encoder (java.beans.Encoder)3 IOException (java.io.IOException)3 Expression (java.beans.Expression)2 BufferedInputStream (java.io.BufferedInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 MapNode (aimax.osm.data.entities.MapNode)1