package com.github.wovnio.wovnjava;

import sun.rmi.runtime.Log;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Utf8 {

    // https://docs.oracle.com/javase/jp/6/technotes/guides/intl/encoding.doc.html
    static final String[] ENCODINGS = new String[] {
            "UTF-8",
            "Shift_JIS",
            "windows-31j",
            "EUC-JP",
            "x-euc-jp-linux",
            "x-eucJP-Open",
    };

    static String detectEncoding(byte[] data) {
        for (String encoding : ENCODINGS) {
            byte[] converted;
            try {
                converted = (new String(data, encoding)).getBytes(encoding);
            } catch (UnsupportedEncodingException e) {
                Logger.log.error("UnsupportedEncodingException while detecting encoding: ", e);
                continue;
            }
            if (Arrays.equals(converted, data)) {
                return encoding;
            }
        }
        return "UTF-8";
    }

    static String toStringUtf8(byte[] data) {
        String encoding = detectEncoding(data);

        if (Logger.isDebug()) {
            Logger.log.info("encoding: " + encoding);
        }

        if (encoding.equals("UTF-8")) {
            return new String(data);
        }
        try {
            return new String(data, encoding);
        } catch (UnsupportedEncodingException e) {
            Logger.log.error("UnsupportedEncodingException while encoding to UTF-8: ", e);
            return new String(data);
        }
    }
}

@@ -34,37 +34,8 @@
 a;%0A%0A
-import sun.rmi.runtime.Log;%0A%0A
 impo

Remove unnecessary import.