|
21 | 21 |
|
22 | 22 | package at.favre.lib.bytes; |
23 | 23 |
|
24 | | -import java.io.*; |
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.DataInput; |
| 26 | +import java.io.File; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.io.Serializable; |
25 | 29 | import java.math.BigInteger; |
26 | 30 | import java.nio.ByteBuffer; |
27 | 31 | import java.nio.ByteOrder; |
|
31 | 35 | import java.nio.charset.StandardCharsets; |
32 | 36 | import java.security.SecureRandom; |
33 | 37 | import java.text.Normalizer; |
34 | | -import java.util.*; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.BitSet; |
| 40 | +import java.util.Collection; |
| 41 | +import java.util.Iterator; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Objects; |
| 44 | +import java.util.Random; |
| 45 | +import java.util.UUID; |
35 | 46 |
|
36 | 47 | /** |
37 | 48 | * Bytes is wrapper class for an byte-array that allows a lot of convenience operations on it: |
|
52 | 63 | * <p> |
53 | 64 | * <strong>Example:</strong> |
54 | 65 | * <pre> |
55 | | - * Bytes b = Bytes.from(array); |
| 66 | + * Bytes b = Bytes.from(array).mutable(); |
56 | 67 | * b.not(); |
57 | 68 | * System.out.println(b.encodeHex()); |
58 | 69 | * </pre> |
@@ -609,6 +620,7 @@ public static Bytes random(int length, Random random) { |
609 | 620 | private final byte[] byteArray; |
610 | 621 | private final ByteOrder byteOrder; |
611 | 622 | private final BytesFactory factory; |
| 623 | + private transient int hashCodeCache; |
612 | 624 |
|
613 | 625 | Bytes(byte[] byteArray, ByteOrder byteOrder) { |
614 | 626 | this(byteArray, byteOrder, new Factory()); |
@@ -1867,13 +1879,15 @@ public boolean equalsContent(Bytes other) { |
1867 | 1879 |
|
1868 | 1880 | @Override |
1869 | 1881 | public int hashCode() { |
1870 | | - int result = Arrays.hashCode(byteArray); |
1871 | | - result = 31 * result + (byteOrder != null ? byteOrder.hashCode() : 0); |
1872 | | - return result; |
| 1882 | + if (hashCodeCache == 0) { |
| 1883 | + hashCodeCache = Arrays.hashCode(byteArray); |
| 1884 | + hashCodeCache = 31 * hashCodeCache + (byteOrder != null ? byteOrder.hashCode() : 0); |
| 1885 | + } |
| 1886 | + return hashCodeCache; |
1873 | 1887 | } |
1874 | 1888 |
|
1875 | 1889 | /** |
1876 | | - * A memory safe toString implementation, which only shows the byte length and at most 8 bytes preview in hex |
| 1890 | + * A constant length output toString() implementation, which only shows the byte length and at most 8 bytes preview in hex |
1877 | 1891 | * representation. |
1878 | 1892 | * |
1879 | 1893 | * @return string representation |
|
0 commit comments