package com.jscape.inet.http.protocol.marshaling;

import com.jscape.inet.http.protocol.messages.Message;
import com.jscape.util.io.DataCodec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
public class ServerCodec
        implements DataCodec<Message> {

    private final RequestCodec reader;
    private final ResponseCodec writer;

    public static ServerCodec defaultCodec() {
        return new ServerCodec(Requests.init(new RequestCodec()), new ResponseCodec());
    }

    public ServerCodec(RequestCodec reader, ResponseCodec writer) {
        assertNotNull(reader);
        this.reader = reader;

        assertNotNull(writer);
        this.writer = writer;
    }

    @Override
    public Message read(InputStream in)
            throws IOException {
        return this.reader.read(in);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        this.writer.write(value, out);
    }
}
