package com.apixandru.games.rummikub.brotocol;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @author Alexandru-Constantin Bledea
 * @since January 08, 2016
 */
public final class SocketWrapper implements PacketWriter, PacketReader {

    private final Serializer serializer = new RummikubSerializer();

    private final Socket socket;
    private final DataInputStream in;
    private final DataOutputStream out;

    public SocketWrapper(final Socket socket) throws IOException {
        this.socket = socket;

        this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        this.out.flush(); // send out the stream header

        this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    }

    public String readString() throws IOException {
        return this.in.readUTF();
    }

    public void write(final String string) {
        try {
            this.out.writeUTF(string);
            this.out.flush();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    @Override
    public void writePacket(final Packet packet) {
        try {
            synchronized (out) {
                serializer.serialize(packet, out);
            }
        } catch (IOException e) {
            throw new IllegalStateException("Failed to send packet", e);
        }
    }

    @Override
    public synchronized Packet readPacket() throws IOException {
        synchronized (in) {
            return serializer.deserialize(in);
        }
    }

    @Override
    public void close() throws IOException {
        this.in.close();
        this.socket.close();
    }

}

@@ -1217,32 +1217,361 @@
        %7D%0A    %7D%0A%0A
+    public boolean readBoolean() throws IOException %7B%0A        return this.in.readBoolean();%0A    %7D%0A%0A    public void write(final boolean value) %7B%0A        try %7B%0A            this.out.writeBoolean(value);%0A            this.out.flush();%0A        %7D catch (IOException e) %7B%0A            throw new IllegalStateException(e);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

add boolean value