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