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.SshMsgChannelOpenFailure;
import com.jscape.util.io.DataCodec;

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

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

    @Override
    public Message read(InputStream in)
            throws IOException {
        int recipientChannel = Uint32Codec.readValue(in);
        int reasonCode = Uint32Codec.readValue(in);
        String description = StringCodec.readUtf8Value(in);
        String languageTag = StringCodec.readUsAsciiValue(in);

        return new SshMsgChannelOpenFailure(recipientChannel, reasonCode, description, languageTag);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshMsgChannelOpenFailure message = (SshMsgChannelOpenFailure) value;
        Uint32Codec.writeValue(message.recipientChannel, out);
        Uint32Codec.writeValue(message.reasonCode, out);
        StringCodec.writeUtf8Value(message.description, out);
        StringCodec.writeUsAsciiValue(message.languageTag, out);
    }
}
