/*
 * Copyright (c) 2002-2011 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 *  the terms of the license agreement you entered into with JSCAPE.
 */

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

import com.jscape.inet.scp.protocol.messages.Message;
import com.jscape.util.io.DataCodec;

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

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

    protected static final char EOL = '\n';

    protected String readLine(InputStream in)
            throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int read;

        while (!eof(read = in.read()) && read != EOL) {
            buffer.write(read);
        }

        return new String(buffer.toByteArray(), StandardCharsets.UTF_8);
    }

    protected boolean eof(int read) {
        return read == -1;
    }
}
