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

import com.jscape.inet.sftp.protocol.messages.Message;
import com.jscape.inet.sftp.protocol.v4.messages.SshFxpWrite;
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.marshaling.primitive.Uint64Codec;
import com.jscape.util.io.DataCodec;

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

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

    @Override
    public Message read(InputStream in)
            throws IOException {
        int id = Uint32Codec.readValue(in);
        byte[] handle = StringCodec.readValue(in);
        long offset = Uint64Codec.readValue(in);
        byte[] data = StringCodec.readValue(in);

        return new SshFxpWrite(id, handle, offset, data);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshFxpWrite message = (SshFxpWrite) value;
        Uint32Codec.writeValue(message.id, out);
        StringCodec.writeValue(message.handle, out);
        Uint64Codec.writeValue(message.offset, out);
        StringCodec.writeValue(message.data, message.dataOffset, message.dataLength, out);
    }

}
