package com.epson.mobilephone.common.wifidirect;

import java.nio.ByteBuffer;
import java.util.Locale;

public class MacAddrUtils {
    private static final int EPS_MACADRESS_POS = 1;
    private static final int EPS_NETID_POS = 3;
    private static final String EPS_NETID_SEP = "\\?";
    private static final String EPS_PID_SEP = "#";
    protected static final int LENGTH_MAC_ADRESS = 6;
    protected static final int LENGTH_STR_MAC_ADRESS = 12;

    public static String getMacAddressFromPrinterId(String str) {
        if (str == null) {
            return null;
        }
        String[] split = str.split(EPS_PID_SEP);
        if (split.length <= 3) {
            return null;
        }
        String[] split2 = split[3].split(EPS_NETID_SEP);
        if (split2.length > 1) {
            return split2[1].toUpperCase(Locale.US);
        }
        return null;
    }

    public static String getMacAddressFromScannerId(String str) {
        if (str == null) {
            return null;
        }
        String[] split = str.split(EPS_NETID_SEP);
        if (split.length > 1) {
            return split[1].toUpperCase(Locale.US);
        }
        return null;
    }

    static byte[] getMacAdressBytes(String str) {
        ByteBuffer allocate = ByteBuffer.allocate(6);
        if (str.contains(":")) {
            str = str.replaceAll(":", "");
        }
        if (str.length() == 12) {
            int i = 0;
            while (i < 12) {
                int i2 = i + 2;
                allocate.put((byte) Integer.parseInt(str.substring(i, i2), 16));
                i = i2;
            }
        }
        return allocate.array();
    }

    static String getMacAdressStr(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bArr.length; i++) {
            stringBuffer.append(String.format("%02x", new Object[]{Byte.valueOf(bArr[i])}).toUpperCase(Locale.US));
        }
        return stringBuffer.toString();
    }

    public static String p2pAddr2MacAddrStr(String str) {
        return getMacAdressStr(getMacAdressBytes(str));
    }
}
