package com.jscape.util.io.primitive;

import com.jscape.util.IOTools;
import com.jscape.util.io.DataCodec;

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

/**
 * @author Alex Usun
 */
public class BooleanCodec
        implements DataCodec<Boolean> {

    public static void writeValue(boolean value, OutputStream out)
            throws IOException {
        out.write(value ? 1 : 0);
    }

    public static boolean readValue(InputStream in)
            throws IOException {
        return IOTools.read(in) != 0;
    }

    public void write(Boolean data, OutputStream out)
            throws IOException {
        writeValue(data, out);
    }

    public Boolean read(InputStream in)
            throws IOException {
        return readValue(in) ? Boolean.TRUE : Boolean.FALSE;
    }
}