package io.logmatic.asynclogger.net;

import android.util.Log;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;


public class SSLSocketEndpoint {

    private SSLSocket sslSocket;
    private OutputStream stream;
    private final String hostname;
    private final Integer port;

    public SSLSocketEndpoint(String hostname, Integer port) {
        this.port = port;
        this.hostname = hostname;
        this.sslSocket = null;

        openConnection();
    }

    public SSLSocketEndpoint(SSLSocket socket) {
        this.port = null;
        this.hostname = null;
        this.sslSocket = socket;

        openConnection();
    }


    public boolean send(byte[] data) {

        if (!isConnected()) return false;

        try {
            stream.write(data);
            stream.flush();
            return true;

        } catch (IOException e) {
            Log.e(getClass().getName(), "Failed to send data to the endpoint", e);

        }
        return false;
    }

    public boolean isConnected() {
        return sslSocket.isConnected();
    }


    public void closeConnection() {

        try {
            stream.flush();
            sslSocket.close();
        } catch (IOException e) {
            Log.e(getClass().getName(), "Connection close failed", e);
        } catch (NullPointerException ne) {
            Log.v(getClass().getName(), "Connection close failed, no previous connection have been open");
        }

    }

    public boolean openConnection() {


        try {

            if (sslSocket == null) {
                Socket socket = new Socket(hostname, port);
                SSLSocketFactory sslFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
                sslSocket = (SSLSocket) sslFactory.createSocket(socket, hostname, port, true);
            }

            stream = sslSocket.getOutputStream();
            return true;

        } catch (IOException e) {
            Log.e(getClass().getName(), "Failed to open socket", e);
        }
        return false;
    }

}

@@ -72,27 +72,32 @@
 java.io.
-IOException
+DataOutputStream
 ;%0Aimport
@@ -105,28 +105,27 @@
 java.io.
-OutputStream
+IOException
 ;%0Aimport
@@ -247,16 +247,36 @@
 ndpoint 
+implements Endpoint 
 %7B%0A%0A    p
@@ -315,16 +315,20 @@
 private 
+Data
 OutputSt
@@ -760,32 +760,46 @@
 tion();%0A    %7D%0A%0A%0A
+    @Override%0A
     public boole
@@ -810,14 +810,14 @@
 end(
-byte%5B%5D
+String
  dat
@@ -907,13 +907,25 @@
 rite
-(data
+Bytes(data + '%5Cn'
 );%0A 
@@ -1126,32 +1126,46 @@
 n false;%0A    %7D%0A%0A
+    @Override%0A
     public boole
@@ -1227,24 +1227,38 @@
 ();%0A    %7D%0A%0A%0A
+    @Override%0A
     public v
@@ -1621,32 +1621,46 @@
       %7D%0A%0A    %7D%0A%0A
+    @Override%0A
     public boole
@@ -2022,16 +2022,37 @@
 tream = 
+new DataOutputStream(
 sslSocke
@@ -2070,16 +2070,17 @@
 Stream()
+)
 ;%0A      
@@ -2124,32 +2124,66 @@
 IOException e) %7B
+%0A%0A            e.printStackTrace();
 %0A            Log

generalize Endpoints