package org.xbib.logging.util;

import java.io.Console;
import java.nio.charset.Charset;

/**
 * JDK-specific code relating to {@link Console}.
 */
public final class CharsetUtil {
    private CharsetUtil() {
    }

    private static final Charset CONSOLE_CHARSET;

    static {
        Console console = System.console();
        Charset charset;
        if (console != null) {
            charset = console.charset();
        } else {
            // Make various guesses as to what the encoding of the console is
            String encodingName = System.getProperty("stdout.encoding");
            if (encodingName == null) {
                encodingName = System.getProperty("native.encoding");
            }
            charset = encodingName == null ? Charset.defaultCharset() : Charset.forName(encodingName);
        }
        CONSOLE_CHARSET = charset;
    }

    public static Charset consoleCharset() {
        return CONSOLE_CHARSET;
    }
}
