package rx.broadcast;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Base64;

public final class Sender implements Serializable, Comparable<Sender> {
    private static final long serialVersionUID = 114L;

    private static final int BYTES_IPV6_ADDRESS = 16;

    private static final int BYTES_INT_PORT = 4;

    private static final int BYTES_SENDER_BUFFER = BYTES_IPV6_ADDRESS + BYTES_INT_PORT;

    private final byte[] byteBuffer;

    private transient ByteBuffer bytes;

    @SuppressWarnings("unused")
    Sender() {
        // This no-arg ctor is for deserialization purposes only
        this(new byte[0]);
    }

    Sender(final byte[] bytes) {
        this.byteBuffer = bytes;
        this.bytes = ByteBuffer.wrap(bytes);
    }

    Sender(final InetAddress address, final int port) {
        this(ByteBuffer.allocate(BYTES_SENDER_BUFFER)
            .put(address.getAddress())
            .putInt(port)
            .array());
    }

    @Override
    public final boolean equals(final Object o) {
        return this == o || !(o == null || getClass() != o.getClass()) && bytes.equals(((Sender) o).bytes);
    }

    @Override
    public final int hashCode() {
        return bytes.hashCode();
    }

    @Override
    public int compareTo(@NotNull final Sender o) {
        return bytes.compareTo(o.bytes);
    }

    @Override
    public String toString() {
        return "Sender{id=" + Base64.getEncoder().encodeToString(bytes.array()) + '}';
    }

    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        this.bytes = ByteBuffer.wrap(byteBuffer);
    }
}

@@ -63,70 +63,8 @@
 l;%0A%0A
-import java.io.IOException;%0Aimport java.io.ObjectInputStream;%0A
 impo
@@ -145,16 +145,41 @@
 Buffer;%0A
+import java.util.Arrays;%0A
 import j
@@ -522,24 +522,16 @@
 T;%0A%0A    
-private 
 final by
@@ -552,49 +552,8 @@
 r;%0A%0A
-    private transient ByteBuffer bytes;%0A%0A
     
@@ -764,53 +764,8 @@
 es;%0A
-        this.bytes = ByteBuffer.wrap(bytes);%0A
     
@@ -1110,25 +1110,50 @@
 s())
- && bytes.equals(
+%0A            && Arrays.equals(byteBuffer, 
 ((Se
@@ -1161,25 +1161,30 @@
 der) o).byte
-s
+Buffer
 );%0A    %7D%0A%0A  
@@ -1244,21 +1244,43 @@
  return 
-bytes
+ByteBuffer.wrap(byteBuffer)
 .hashCod
@@ -1376,31 +1376,75 @@
 urn 
-bytes.compareTo(o.bytes
+ByteBuffer.wrap(byteBuffer).compareTo(ByteBuffer.wrap(o.byteBuffer)
 );%0A 
@@ -1567,214 +1567,21 @@
 byte
-s.array()) + '%7D';%0A    %7D%0A%0A    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException %7B%0A        in.defaultReadObject();%0A        this.bytes = ByteBuffer.wrap(byte
 Buffer)
+ + '%7D'
 ;%0A  

Remove transient ByteBuffer from Sender