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

import com.jscape.inet.sftp.protocol.v4.messages.SshFxpExtended;
import com.jscape.inet.sftp.protocol.v4.messages.SshFxpExtendedCheckFileName;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.NameListCodec;
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 java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author Alex Usun
 */
public class SshFxpExtendedCheckFileNameCodec
        implements SshFxpExtendedCodec.TypeCodec {

    @Override
    public SshFxpExtended read(InputStream in, int id)
            throws IOException {
        String name = StringCodec.readUtf8Value(in);
        List<String> hashAlgorithmList = NameListCodec.readUsAsciiValue(in);
        long startOffset = Uint64Codec.readValue(in);
        long length = Uint64Codec.readValue(in);
        int blockSize = Uint32Codec.readValue(in);

        return new SshFxpExtendedCheckFileName(id, name, hashAlgorithmList, startOffset, length, blockSize);
    }

    @Override
    public void write(SshFxpExtended value, OutputStream out)
            throws IOException {
        SshFxpExtendedCheckFileName message = (SshFxpExtendedCheckFileName) value;
        StringCodec.writeUtf8Value(message.name, out);
        NameListCodec.writeUsAsciiValue(message.hashAlgorithmList, out);
        Uint64Codec.writeValue(message.startOffset, out);
        Uint64Codec.writeValue(message.length, out);
        Uint32Codec.writeValue(message.blockSize, out);
    }
}
