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

import com.jscape.util.io.primitive.IntByteArrayCodec;
import com.jscape.util.io.primitive.IntStringCodec;

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

/**
 * @author Alex Usun
 */
public class StringCodec {

    public static byte[] readValue(InputStream in)
            throws IOException {
        return IntByteArrayCodec.readBigEndianValue(in);
    }

    public static void writeValue(byte[] value, int offset, int length, OutputStream out)
            throws IOException {
        IntByteArrayCodec.writeBigEndianValue(value, offset, length, out);
    }

    public static void writeValue(byte[] value, OutputStream out)
            throws IOException {
        IntByteArrayCodec.writeBigEndianValue(value, out);
    }

    public static String readUsAsciiValue(InputStream in)
            throws IOException {
        return IntStringCodec.readBigEndianValue(StandardCharsets.US_ASCII, in);
    }

    public static void writeUsAsciiValue(String value, OutputStream out)
            throws IOException {
        IntStringCodec.writeBigEndianValue(value, StandardCharsets.US_ASCII, out);
    }

    public static String readUtf8Value(InputStream in)
            throws IOException {
        return IntStringCodec.readBigEndianValue(StandardCharsets.UTF_8, in);
    }

    public static void writeUtf8Value(String value, OutputStream out)
            throws IOException {
        IntStringCodec.writeBigEndianValue(value, StandardCharsets.UTF_8, out);
    }

    private StringCodec() {
        // empty
    }
}
