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;

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

    private final ResponseCodec reader;
    private final RequestCodec writer;

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

    public ClientCodec(ResponseCodec reader, RequestCodec writer) {
        this.reader = reader;
        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);
    }
}
