Search in sources :

Example 1 with XmlEofOutputStream

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;
}
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)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Socket (java.net.Socket)1 XmlEofOutputStream (org.opensolaris.opengrok.util.XmlEofOutputStream)1