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");
}
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();
}
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();
}
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.");
}
}
}
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;
}
Aggregations