use of org.opensolaris.opengrok.util.XmlEofOutputStream 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