package com.jscape.inet.proxy.socks.protocol.v5.marshaling;

import com.jscape.inet.proxy.socks.protocol.v5.messages.AuthenticationMethodResponse;
import com.jscape.inet.proxy.socks.protocol.v5.messages.Message;
import com.jscape.util.io.primitive.ByteCodec;

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

/**
 * @author Alex Usun
 */
public class AuthenticationMethodResponseCodec
        extends BaseCodec {

    @Override
    public Message read(InputStream in)
            throws IOException {
        int version = ByteCodec.readValue(in) & 0xFF;
        assertVersionSupported(version);
        int method = ByteCodec.readValue(in) & 0xFF;

        return new AuthenticationMethodResponse(method);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        AuthenticationMethodResponse message = (AuthenticationMethodResponse) value;
        ByteCodec.writeValue((byte) VERSION, out);
        ByteCodec.writeValue((byte) message.method, out);
    }

}
