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

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

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

/**
 * @author Alex Usun
 */
public class PasswordAuthenticationResponseCodec
        extends BaseAuthenticationCodec {

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

        return new PasswordAuthenticationResponse(statusCode);
    }

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

}
