package com.jscape.inet.ssh.protocol.v2.marshaling;

import com.jscape.inet.ssh.protocol.messages.Message;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.StringCodec;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.Uint32Codec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthInfoResponse;
import com.jscape.util.io.DataCodec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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

    @Override
    public Message read(InputStream in)
            throws IOException {
        int numResponses = Uint32Codec.readValue(in);
        List<String> responses = new ArrayList<String>(numResponses);
        for (int i = 0; i < numResponses; ++i) {
            String response = StringCodec.readUtf8Value(in);
            responses.add(response);
        }

        return new SshMsgUserAuthInfoResponse(responses);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshMsgUserAuthInfoResponse message = (SshMsgUserAuthInfoResponse) value;
        Uint32Codec.writeValue(message.responses.size(), out);
        for (String response : message.responses) {
            StringCodec.writeUtf8Value(response, out);
        }
    }
}
