package com.jscape.inet.ssh.protocol.v2.marshaling;

import com.jscape.inet.ssh.protocol.messages.Message;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.StringCodec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgIgnore;
import com.jscape.util.io.DataCodec;

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

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

    private static final byte[] EMPTY_DATA = new byte[0];

    @Override
    public Message read(InputStream in)
            throws IOException {
        try {
            byte[] data = in.available() > 0 ? StringCodec.readValue(in) : EMPTY_DATA;
            return new SshMsgIgnore(data);
        } catch (EOFException e) {
            return new SshMsgIgnore(EMPTY_DATA);
        }
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshMsgIgnore message = (SshMsgIgnore) value;
        StringCodec.writeValue(message.data, out);
    }
}
