Mac地址批量生成bin包说明

生成Mac地址,并制作mac.bin用于刷写

说明:

MAC地址由12位16进制的数字组成,同过申请或者购买可以获得一连串的Mac地址字符串,我们需要将这一连串的mac地址按照同意统一要求制作成单一的bin文件刷写,同时在刷写完成便删除此文件

  1. 通过提供起始mac地址,mac地址的个数以及mac文件命规则,批量生成mac.bin文件

  2. 采用java的写如流操作,批量生成.bin文件

  3. 为了便于测试,先采用string字符串写入文件进行查看测试

  4. 测试无误,在采用string转buye二进制数据进行写如文件

  5. 写入完成,同时提供测试批量打印输出写ru的bin文件是否符合要求(转码是否有误)

具体代码如下:

public class MacToBin {
    // 文件路径
    public static String filePath = "Mac";
    // 统一起始文件名
    public static String fileName = "Mac/mac";
    // Mac地址递增数
    public static int macCount = 300;
    // 起始Mac地址
    public static String macStart = "00:70:A4:00:00:00";
    // 批量制作还是打印标志位
    public static boolean makeMac = false;

    public static void main(String[] args) {
        // 制作
        if (makeMac) {
            // 文件存在判断
            File file = new File(filePath);
            if (file.exists()) {
                if (file.isDirectory()) {
                    File[] files = file.listFiles();
                    //  遍历删除文件
                    for (int i = 0; i < files.length; i++) {
                        files[i].delete();
                    }
                    file.delete();
                }
                file.delete();
            }
            file.mkdir();
            // 设定文件起始地址, 以及数量
            makingMac();
        } else {
            //打印
            printMac();
        }
    }

    // 批量生成bin文件
    private static void makingMac() {
        String start = macStart.replaceAll(":", "");
        BigInteger num = new BigInteger(start, 16);
        BigInteger addNum = new BigInteger("1");
        String result = "";

        for (int i = 0; i < macCount; i++) {
            try {
                StringBuilder macName = new StringBuilder(fileName).append(i).append(".bin");

                // 中间生成文件测试
//                FileWriter writer = new FileWriter(new File(macName.toString()), true);

                FileOutputStream fos = new FileOutputStream(macName.toString());
                result = num.toString(16).toUpperCase();
                for (int j = 12 - result.length(); j > 0; j--) {
                    result = "0" + result;
                }
                num = num.add(addNum);

                fos.write(hexStringToBytes(getMacAdr(result)));
                fos.close();

                System.out.println(getMacAdr(result));

                // 测试
//                writer.write(getMacAdr(result));
//                writer.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("finished");
    }

    // 批量打印测试代码段
    public static void printMac() {
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("No file exist!");
            return;
        } else {
            if (!file.isDirectory())
                System.out.println("The file is no directory!");
            else {
                StringBuffer sb = new StringBuffer();
                try {
                    // 批量打印
                    for (int i = 0; i < macCount; i++) {
                        StringBuilder macName = new StringBuilder(fileName).append(i).append(".bin");
                        File macfile = new File(macName.toString());
                        if (macfile.exists()) {
                            FileInputStream in = new FileInputStream(macfile);
                            byte[] temp = new byte[1];
                            while (in.read(temp) != -1) {
                                appendHexPair(temp[0], sb);
                                sb.append(":");
                            }
                            in.close();
                        }
                        String tempMac = sb.toString();
                        // 清空
                        sb.setLength(0);

//                        System.out.println(tempMac);

                        String macaddr = tempMac.substring(0, tempMac.length() - 1);
                        System.out.println(macaddr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("Print mafFile finished!");
            }
        }

    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        char c0 = hexDigits[(bt & 240) >> 4];
        char c1 = hexDigits[bt & 15];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }

    private static String getMacAdr(String str) {
        StringBuilder result = new StringBuilder("");
        for (int i = 1; i <= 12; i++) {
            result.append(str.charAt(i - 1));
//            if (i % 2 == 0) {
//                result.append(":");
//            }
        }
//        return result.substring(0, 17);
        return result.toString();
    }

    /**
     * 16进制字符串转换为byte[]
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase().replace(" ", "");
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

}

工厂mac地址刷写说明

  1. 检查/mnt/usb下是否存在(挂载U盘),是否存在MAC的文件夹

  2. 存在, 列出当前文件夹下所有文件,判断第一个文件是否存在

  3. 使用FileInputStream读流,获取字符串保存于tempMac中,同过裁剪获取最终的macaddr地址

  4. 获取本机存在的mac地址,CtvTvManager.getInstance().getEnvironment(mac_ethAddr),检查刷写和存在是否存在冲突(一样),一样,删除刷写的文件

  5. 不一样,执行刷写操作,CtvTvManager.getInstance().setEnvironment(mac_ethAddr, macaddr)

  6. 删除刷写的文件,避免刷写一样的mac地址

    public static final String MAC = "MAC";
    private static String mac_ethAddr = "macaddr";
    
    static int UpgradeMAC() {
          int ret = 0;
          try {
              File listMacfile = new File(checkFileIsExist("/mnt/usb", Constant.MAC));
              if (!listMacfile.exists()) {
                  return EnumUpgradeStatus.E_UPGRADE_FILE_NOT_FOUND.ordinal();
              }
              File macfile = listMacfile.listFiles()[0];
              StringBuffer sb = new StringBuffer();
              try {
                  if (macfile.exists()) {
                      FileInputStream in = new FileInputStream(macfile);
                      byte[] temp = new byte[1];
                      while (in.read(temp) != -1) {
                          appendHexPair(temp[0], sb);
                          sb.append(":");
                      }
                      in.close();
                  } else {
                      ret = EnumUpgradeStatus.E_UPGRADE_FILE_NOT_FOUND.ordinal();
                  }
                  String tempMac = sb.toString();
                  Log.d(TAG, "tempMac =" + tempMac);
                  String macaddr = tempMac.substring(0, tempMac.length() - 1);
                  Log.d(TAG, "macaddr =" + macaddr);
                  String existsMac = CtvTvManager.getInstance().getEnvironment(mac_ethAddr);
                  Log.d(TAG, "existsMac =" + existsMac);
                  if (existsMac.trim().equals(macaddr) || existsMac.trim() == macaddr) {
                      macfile.delete();
                      return ret;
                  }
                  if (!CtvTvManager.getInstance().setEnvironment(mac_ethAddr, macaddr)) {
                      return ret;
                  }
                  String successMac = CtvTvManager.getInstance().getEnvironment(mac_ethAddr);
                  if (!successMac.trim().equals(macaddr) && successMac.trim() != macaddr) {
                      return ret;
                  }
                  macfile.delete();
                  macaddress = successMac;
                  return EnumUpgradeStatus.E_UPGRADE_SUCCESS.ordinal();
              } catch (FileNotFoundException e) {
                  ret = EnumUpgradeStatus.E_UPGRADE_FILE_NOT_FOUND.ordinal();
                  e.printStackTrace();
                  return ret;
              } catch (IOException e2) {
                  ret = EnumUpgradeStatus.E_UPGRADE_FAIL.ordinal();
                  e2.printStackTrace();
                  return ret;
              }
          } catch (Exception e3) {
              ret = EnumUpgradeStatus.E_UPGRADE_FAIL.ordinal();
              e3.printStackTrace();
              return ret;
          }
      }
    
坚持原创技术分享,您的支持将鼓励我继续创作!