package net.kencochrane.raven.connection;

import net.kencochrane.raven.event.LoggedEvent;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Connection to a Sentry server through an UDP connection.
 */
public class UdpConnection extends AbstractConnection {
    private static final Logger logger = Logger.getLogger(UdpConnection.class.getCanonicalName());
    private DatagramSocket socket;
    private Charset charset = Charset.defaultCharset();

    public UdpConnection(Dsn dsn) {
        super(dsn);
        openSocket();
    }

    @Override
    public void send(LoggedEvent event) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            writeHeader(baos);
            baos.flush();
            baos.close();

            byte[] message = baos.toByteArray();
            DatagramPacket packet = new DatagramPacket(message, message.length);
            socket.send(packet);
        } catch (IOException e) {
            logger.log(Level.SEVERE,
                    "An exception occurred while trying to establish a connection to the sentry server");
        }
    }

    private void writeHeader(OutputStream os) throws IOException {
        os.write(getAuthHeader().getBytes(charset));
        os.write("\n\n".getBytes(charset));
    }

    private void openSocket() {
        try {
            socket = new DatagramSocket();
            socket.connect(new InetSocketAddress(getDsn().getHost(), getDsn().getPort()));
        } catch (SocketException e) {
            throw new IllegalStateException("The UDP connection couldn't be used, impossible to send anything " +
                    "to sentry", e);
        }
    }

    public void setCharset(Charset charset) {
        this.charset = charset;
    }
}

@@ -36,16 +36,152 @@
 ction;%0A%0A
+import net.kencochrane.raven.connection.marshaller.Marshaller;%0Aimport net.kencochrane.raven.connection.marshaller.SimpleJsonMarshaller;%0A
 import n
@@ -220,16 +220,16 @@
 dEvent;%0A
-
 %0Aimport 
@@ -860,16 +860,80 @@
 arset();
+%0A    private Marshaller marshaller = new SimpleJsonMarshaller();
 %0A%0A    pu
@@ -1198,45 +1198,39 @@
     
-baos.flush();%0A            baos.close(
+marshaller.marshall(event, baos
 );%0A%0A
@@ -2126,16 +2126,16 @@
 y%22, e);%0A
-
         
@@ -2139,24 +2139,124 @@
    %7D%0A    %7D%0A%0A
+    public void setMarshaller(Marshaller marshaller) %7B%0A        this.marshaller = marshaller;%0A    %7D%0A%0A
     public v

Use a marshaller in the UPDConnection