StrUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.template.common.utils;
  2. import java.text.ParseException;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.UUID;
  7. /**
  8. * @Author: binguo
  9. * @Date: 2023/7/25 星期二 14:20
  10. * @Description: com.repair.common.utils
  11. * @Version: 1.0
  12. */
  13. public class StrUtils {
  14. public static String getRandomName(String fileName){
  15. int index=fileName.lastIndexOf(".");
  16. String houzhui=fileName.substring(index);//获取后缀名
  17. String uuidFileName=UUID.randomUUID().toString().replace("-","")+houzhui;
  18. return uuidFileName;
  19. }
  20. /**
  21. * 采用URL Base64字符,即把“+/”换成“-_”
  22. */
  23. static private char[] alphabet = "01234567899876543210012345678998765432100123456789987654321001234".toCharArray();//"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789xyz".toCharArray();
  24. /**
  25. * 获取指定位数的UUID
  26. * @param bits 指定位数
  27. * @return
  28. */
  29. public static String getUUIDBits(int bits) {
  30. UUID uuid = UUID.randomUUID();
  31. long msb = uuid.getMostSignificantBits();
  32. long lsb = uuid.getLeastSignificantBits();
  33. char[] out = new char[24];
  34. int tmp = 0, idx = 0;
  35. // 循环写法
  36. int bit = 0, bt1 = 8, bt2 = 8;
  37. int mask = 0x00, offsetm = 0, offsetl = 0;
  38. for(; bit < 16; bit += 3, idx += 4) {
  39. offsetm = 64 - (bit + 3) * 8;
  40. offsetl = 0;
  41. tmp = 0;
  42. if(bt1 > 3) {
  43. mask = (1 << 8 * 3) - 1;
  44. } else if(bt1 >= 0) {
  45. mask = (1 << 8 * bt1) - 1;
  46. bt2 -= 3 - bt1;
  47. } else {
  48. mask = (1 << 8 * ((bt2 > 3) ? 3 : bt2)) - 1;
  49. bt2 -= 3;
  50. }
  51. if(bt1 > 0) {
  52. bt1 -= 3;
  53. tmp = (int) ((offsetm < 0) ? msb : (msb >>> offsetm) & mask);
  54. if(bt1 < 0) {
  55. tmp <<= Math.abs(offsetm);
  56. mask = (1 << 8 * Math.abs(bt1)) - 1;
  57. }
  58. }
  59. if(offsetm < 0) {
  60. offsetl = 64 + offsetm;
  61. tmp |= ((offsetl < 0) ? lsb : (lsb >>> offsetl)) & mask;
  62. }
  63. if(bit == 15) {
  64. out[idx + 3] = alphabet[64];
  65. out[idx + 2] = alphabet[64];
  66. tmp <<= 4;
  67. } else {
  68. out[idx + 3] = alphabet[tmp & 0x3f];
  69. tmp >>= 6;
  70. out[idx + 2] = alphabet[tmp & 0x3f];
  71. tmp >>= 6;
  72. }
  73. out[idx + 1] = alphabet[tmp & 0x3f];
  74. tmp >>= 6;
  75. out[idx] = alphabet[tmp & 0x3f];
  76. }
  77. return new String(out, 0, bits);
  78. }
  79. public static void main(String[] args) throws ParseException
  80. {
  81. List<String> data = new ArrayList<>();
  82. for (int i = 0;i<10000000;i++){
  83. String sss = getUUIDBits(15);
  84. data.add(sss);
  85. }
  86. long allCount = data.size();
  87. long disCount = data.stream().distinct().count();
  88. Collections.sort(data);
  89. String sss ="";
  90. }
  91. }