package com.jscape.inet.sftp.protocol.v4.marshaling;

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

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

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

    @Override
    public Message read(InputStream in)
            throws IOException {
        int id = Uint32Codec.readValue(in);
        int code = Uint32Codec.readValue(in);
        String message = StringCodec.readUtf8Value(in);
        String languageTag = StringCodec.readUtf8Value(in);

        return new SshFxpStatus(id, code, message, languageTag);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshFxpStatus message = (SshFxpStatus) value;
        Uint32Codec.writeValue(message.id, out);
        Uint32Codec.writeValue(message.code, out);
        StringCodec.writeUtf8Value(message.message, out);
        StringCodec.writeUtf8Value(message.languageTag, out);
    }

}
