package com.github.pukkaone.gelf.protocol;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class GelfTCPSender extends GelfSender {

    private boolean shutdown;
    private InetAddress host;
    private int port;
    private Socket socket;

    private void connect() throws IOException {
        socket = new Socket(host, port);
    }

    public GelfTCPSender(String host, int port) throws IOException {
        this.host = InetAddress.getByName(host);
        this.port = port;
        connect();
    }

    private ByteBuffer toTCPBuffer(String json) {
        // Do not use GZIP, as the headers will contain \0 bytes.
        // graylog2-server uses \0 as a delimiter for TCP frames
        // see: https://github.com/Graylog2/graylog2-server/issues/127
        byte[] messageBytes = json.getBytes(StandardCharsets.UTF_8);

        ByteBuffer buffer = ByteBuffer.allocate(messageBytes.length + 1);
        buffer.put(messageBytes).put((byte) 0);
        buffer.flip();
        return buffer;
    }

    public boolean sendMessage(GelfMessage message) {
        if (shutdown || !message.isValid()) {
            return false;
        }

        try {
            // reconnect if necessary
            if (socket == null) {
                socket = new Socket(host, port);
            }

            socket.getOutputStream().write(toTCPBuffer(message.toJson()).array());
            return true;
        } catch (IOException e) {
            // if an error occours, signal failure
            socket = null;
            return false;
        }
    }

    public void close() {
        shutdown = true;
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            // Ignore exception closing socket.
        }
    }
}

@@ -65,16 +65,51 @@
 eption;%0A
+import java.io.OutputStreamWriter;%0A
 import j
@@ -157,36 +157,8 @@
 et;%0A
-import java.nio.ByteBuffer;%0A
 impo
@@ -354,17 +354,16 @@
 socket;%0A
-%0A
     priv
@@ -370,83 +370,301 @@
 ate 
-void connect() throws IOException %7B%0A        socket = new Socket(host, port)
+OutputStreamWriter writer;%0A%0A    private OutputStreamWriter getWriter() throws IOException %7B%0A        if (socket == null) %7B%0A            socket = new Socket(host, port);%0A            writer = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8);%0A        %7D%0A        return writer
 ;%0A  
@@ -820,23 +820,25 @@
         
-connect
+getWriter
 ();%0A    
@@ -849,53 +849,158 @@
    p
-rivate ByteBuffer toTCPBuffer(String json) %7B%0A
+ublic boolean sendMessage(GelfMessage message) %7B%0A        if (shutdown %7C%7C !message.isValid()) %7B%0A            return false;%0A        %7D%0A%0A        try %7B%0A    
     
@@ -1069,16 +1069,20 @@
         
+    
 // grayl
@@ -1129,25 +1129,28 @@
 P frames
-%0A
+.%0A  
         
 // see: 
@@ -1145,12 +1145,14 @@
     
+  
 // 
-s
+S
 ee: 
@@ -1218,344 +1218,96 @@
     
-byte%5B%5D messageBytes = json.getBytes(StandardCharsets.UTF_8);%0A%0A        ByteBuffer buffer = ByteBuffer.allocate(messageBytes.length + 1);%0A        buffer.put(messageBytes).put((byte) 0);%0A        buffer.flip();%0A        return buffer;%0A    %7D%0A%0A    public boolean sendMessage(GelfMessage message) %7B%0A        if (shutdown %7C%7C !message.isValid()) %7B
+    OutputStreamWriter writer = getWriter();%0A            writer.write(message.toJson());
 %0A   
@@ -1319,263 +1319,53 @@
     
-return false;%0A        %7D%0A%0A        try %7B%0A            // reconnect if necessary%0A            if (socket == null) %7B%0A                socket = new Socket(host, port);%0A            %7D%0A%0A            socket.getOutputStream().write(toTCPBuffer(message.toJson()).array()
+writer.write('%5C0');%0A            writer.flush(
 );%0A 
@@ -1770,16 +1770,63 @@
 socket.%0A
+        %7D finally %7B%0A            socket = null;%0A
         

Remove data copying in TCP sender