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

import com.jscape.inet.sftp.protocol.messages.Message;
import com.jscape.inet.sftp.protocol.v4.messages.FileAttributeFlags;
import com.jscape.inet.sftp.protocol.v4.messages.SshFxpStat;
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 SshFxpStatCodec
        implements DataCodec<Message> {

    @Override
    public Message read(InputStream in)
            throws IOException {
        int id = Uint32Codec.readValue(in);
        String path = StringCodec.readUtf8Value(in);
        FileAttributeFlags flags = FileAttributeFlagsFormat.parse(Uint32Codec.readValue(in));

        return new SshFxpStat(id, path, flags);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshFxpStat message = (SshFxpStat) value;
        Uint32Codec.writeValue(message.id, out);
        StringCodec.writeUtf8Value(message.path, out);
        Uint32Codec.writeValue(FileAttributeFlagsFormat.format(message.flags), out);
    }

}
