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

import com.jscape.inet.sftp.protocol.v4.messages.SshFxpExtendedReply;
import com.jscape.inet.sftp.protocol.v4.messages.SshFxpExtendedReplyCheckFileName;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.StringCodec;
import com.jscape.util.IOTools;

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

/**
 * @author Alex Usun
 */
public class SshFxpExtendedReplyCheckFileNameCodec
        implements SshFxpExtendedReplyCodec.TypeCodec {

    @Override
    public SshFxpExtendedReply read(InputStream in, int id)
            throws IOException {
        String hashAlgorithmUsed = StringCodec.readUsAsciiValue(in);
        byte[] data = IOTools.readFully(in);

        return new SshFxpExtendedReplyCheckFileName(id, hashAlgorithmUsed, data);
    }

    @Override
    public void write(SshFxpExtendedReply value, OutputStream out)
            throws IOException {
        SshFxpExtendedReplyCheckFileName message = (SshFxpExtendedReplyCheckFileName) value;
        StringCodec.writeUsAsciiValue(message.hashAlgorithmUsed, out);
        out.write(message.data);
    }
}
