package com.jscape.inet.scp.protocol.marshaling;

import com.jscape.inet.scp.protocol.messages.FatalErrorMessage;
import com.jscape.inet.scp.protocol.messages.Message;
import com.jscape.util.IOTools;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author Alex Usun
 */
public class FatalErrorMessageCodec
        extends CodecBase {

    public Message read(InputStream in)
            throws IOException {
        try {
            String message = readLine(in);
            return new FatalErrorMessage(message);
        } catch (Exception e) {
            throw IOTools.ioException(e);
        }
    }

    public void write(Message value, OutputStream out)
            throws IOException {
        FatalErrorMessage message = (FatalErrorMessage) value;
        byte[] bytes = message.message.getBytes(StandardCharsets.UTF_8);

        out.write(bytes);
        out.write(EOL);
    }
}
