package com.jscape.inet.proxy.socks.protocol.v4.marshaling;

import com.jscape.util.IOTools;

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

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

    private static final byte[] EOL = {0};

    public static void writeValue(String value, Charset charset, OutputStream out)
            throws IOException {
        out.write(value.getBytes(charset));
        out.write(0);
    }

    public static String readValue(Charset charset, InputStream in)
            throws IOException {
        byte[] data = IOTools.readSequence(in, EOL, false);
        return new String(data, charset);
    }

    private StringCodec() {
        //empty
    }
}
