|
1 | 1 | /*
|
2 |
| - * Copyright 2018-2019 the original author or authors. |
| 2 | + * Copyright 2018-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.kafka.requestreply;
|
18 | 18 |
|
19 |
| -import java.math.BigInteger; |
20 | 19 | import java.util.Arrays;
|
21 | 20 |
|
22 | 21 | import org.springframework.util.Assert;
|
23 | 22 |
|
24 | 23 | /**
|
25 | 24 | * Wrapper for byte[] that can be used as a hash key. We could have used BigInteger
|
26 |
| - * instead but this wrapper is much less expensive. We do use a BigInteger in |
27 |
| - * {@link #toString()} though. |
| 25 | + * instead but this wrapper is much less expensive. |
28 | 26 | *
|
29 | 27 | * @author Gary Russell
|
30 | 28 | * @since 2.1.3
|
31 | 29 | */
|
32 | 30 | public final class CorrelationKey {
|
33 | 31 |
|
| 32 | + private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray(); |
| 33 | + |
34 | 34 | private final byte[] correlationId;
|
35 | 35 |
|
| 36 | + private String asString; |
| 37 | + |
36 | 38 | private volatile Integer hashCode;
|
37 | 39 |
|
38 | 40 | public CorrelationKey(byte[] correlationId) { // NOSONAR array reference
|
@@ -74,9 +76,27 @@ public boolean equals(Object obj) {
|
74 | 76 | return true;
|
75 | 77 | }
|
76 | 78 |
|
| 79 | + private static String bytesToHex(byte[] bytes) { |
| 80 | + boolean uuid = bytes.length == 16; |
| 81 | + char[] hexChars = new char[bytes.length * 2 + (uuid ? 4 : 0)]; |
| 82 | + int i = 0; |
| 83 | + for (int j = 0; j < bytes.length; j++) { |
| 84 | + int v = bytes[j] & 0xFF; |
| 85 | + hexChars[i++] = HEX_ARRAY[v >>> 4]; |
| 86 | + hexChars[i++] = HEX_ARRAY[v & 0x0F]; |
| 87 | + if (uuid && (j == 3 || j == 5 || j == 7 || j == 9)) { |
| 88 | + hexChars[i++] = '-'; |
| 89 | + } |
| 90 | + } |
| 91 | + return new String(hexChars); |
| 92 | + } |
| 93 | + |
77 | 94 | @Override
|
78 | 95 | public String toString() {
|
79 |
| - return "[" + new BigInteger(this.correlationId) + "]"; |
| 96 | + if (this.asString == null) { |
| 97 | + this.asString = bytesToHex(this.correlationId); |
| 98 | + } |
| 99 | + return this.asString; |
80 | 100 | }
|
81 | 101 |
|
82 | 102 | }
|
0 commit comments