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.Uint32Codec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgChannelOpenConfirmation;
import com.jscape.util.io.DataCodec;

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

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

    @Override
    public Message read(InputStream in)
            throws IOException {
        int recipientChannel = Uint32Codec.readValue(in);
        int senderChannel = Uint32Codec.readValue(in);
        long initialWindowSize = Uint32Codec.readLongValue(in);
        int maxPacketSize = Uint32Codec.readValue(in);

        return new SshMsgChannelOpenConfirmation(recipientChannel, senderChannel, initialWindowSize, maxPacketSize);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshMsgChannelOpenConfirmation message = (SshMsgChannelOpenConfirmation) value;
        Uint32Codec.writeValue(message.recipientChannel, out);
        Uint32Codec.writeValue(message.senderChannel, out);
        Uint32Codec.writeValue(message.initialWindowSize, out);
        Uint32Codec.writeValue(message.maxPacketSize, out);
    }
}
