1. import javax.xml.bind.DatatypeConverter;
    2. import java.util.Arrays;
    3. import java.util.Base64;
    4. public final class BytesHub {
    5. private static final char[] HEX_CODE = "0123456789ABCDEF".toCharArray();
    6. private final byte[] bytes;
    7. BytesHub(byte[] bytes) {
    8. this.bytes = bytes.clone();
    9. }
    10. public static BytesHub fromBase64(String baseStr) {
    11. return new BytesHub(Base64.getDecoder().decode(baseStr));
    12. }
    13. public static BytesHub fromHex(byte[] bytes) {
    14. return new BytesHub(bytes);
    15. }
    16. public static BytesHub fromHexStr(String hexStr) {
    17. hexStr = hexStr.toLowerCase();
    18. byte[] bytes = DatatypeConverter.parseHexBinary(hexStr);
    19. return new BytesHub(bytes);
    20. }
    21. public static BytesHub fromPrettyHexStr(String hexStr) {
    22. hexStr = hexStr.toLowerCase();
    23. hexStr = hexStr.replaceAll("[^0-9a-f]", "");
    24. byte[] bytes = DatatypeConverter.parseHexBinary(hexStr);
    25. return new BytesHub(bytes);
    26. }
    27. public byte[] toHex() {
    28. return bytes.clone();
    29. }
    30. public String printHexBinary(byte[] data, String separator) {
    31. if (data == null || data.length == 0) {
    32. return "";
    33. }
    34. StringBuilder r = new StringBuilder(data.length * 2);
    35. r.append(HEX_CODE[(data[0] >> 4) & 0xF]);
    36. r.append(HEX_CODE[(data[0] & 0xF)]);
    37. for (int i = 1; i < data.length; i++) {
    38. r.append(separator);
    39. r.append(HEX_CODE[(data[i] >> 4) & 0xF]);
    40. r.append(HEX_CODE[(data[i] & 0xF)]);
    41. }
    42. return r.toString();
    43. }
    44. public String toHexUpperStr() {
    45. return printHexBinary(bytes, "").toUpperCase();
    46. }
    47. public String toHexLowerStr() {
    48. return printHexBinary(bytes, "").toLowerCase();
    49. }
    50. public String toHexPrettyUpperStr() {
    51. return printHexBinary(bytes, " ").toUpperCase();
    52. }
    53. public String toHexPrettyLowerStr() {
    54. return printHexBinary(bytes, " ").toLowerCase();
    55. }
    56. public String toHexPrettyUpperStr(String separator) {
    57. return printHexBinary(bytes, separator).toUpperCase();
    58. }
    59. public String toHexPrettyLowerStr(String separator) {
    60. return printHexBinary(bytes, separator).toLowerCase();
    61. }
    62. public String toBase64() {
    63. return Base64.getEncoder().encodeToString(bytes);
    64. }
    65. public BytesHub append(BytesHub bytesHub) {
    66. return append(bytesHub.toHex());
    67. }
    68. public BytesHub append(byte[] bs) {
    69. int length = this.bytes.length;
    70. int newLength = length + bs.length;
    71. byte[] newBs = Arrays.copyOf(this.bytes, newLength);
    72. System.arraycopy(bs, 0, newBs, length, bs.length);
    73. return new BytesHub(newBs);
    74. }
    75. public BytesHub append(BytesHub... bytesHubs) {
    76. int newLength = this.bytes.length;
    77. for (BytesHub bytesHub : bytesHubs) {
    78. newLength += bytesHub.bytes.length;
    79. }
    80. byte[] newBs = Arrays.copyOf(this.bytes, newLength);
    81. int index = this.bytes.length;
    82. for (BytesHub bytesHub : bytesHubs) {
    83. byte[] bs = bytesHub.bytes;
    84. System.arraycopy(bs, 0, newBs, index, bs.length);
    85. index += bs.length;
    86. }
    87. return new BytesHub(newBs);
    88. }
    89. }