Skip to content

Commit 220b974

Browse files
committed
Remove caching of mutable bytes hashcode
1 parent 7bb6254 commit 220b974

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,8 +1880,7 @@ public boolean equalsContent(Bytes other) {
18801880
@Override
18811881
public int hashCode() {
18821882
if (hashCodeCache == 0) {
1883-
hashCodeCache = Arrays.hashCode(byteArray);
1884-
hashCodeCache = 31 * hashCodeCache + (byteOrder != null ? byteOrder.hashCode() : 0);
1883+
hashCodeCache = Util.hashCode(internalArray(), byteOrder());
18851884
}
18861885
return hashCodeCache;
18871886
}

src/main/java/at/favre/lib/bytes/MutableBytes.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public MutableBytes secureWipe(SecureRandom random) {
126126
return this;
127127
}
128128

129+
@Override
130+
public int hashCode() {
131+
return Util.hashCode(internalArray(), byteOrder());
132+
}
133+
129134
/**
130135
* Factory creating mutable byte types
131136
*/

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,25 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.*;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.DataInput;
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.io.InputStream;
2529
import java.nio.ByteBuffer;
30+
import java.nio.ByteOrder;
2631
import java.nio.file.Files;
27-
import java.util.*;
32+
import java.util.ArrayList;
33+
import java.util.Arrays;
34+
import java.util.Collection;
35+
import java.util.HashMap;
36+
import java.util.Iterator;
37+
import java.util.List;
38+
import java.util.Map;
39+
import java.util.NoSuchElementException;
40+
import java.util.Objects;
41+
import java.util.Random;
42+
import java.util.UUID;
2843

2944
/**
3045
* Common Util methods to convert or modify byte arrays
@@ -489,6 +504,19 @@ static ByteBuffer getBytesFromUUID(UUID uuid) {
489504
return bb;
490505
}
491506

507+
/**
508+
* Hashcode implementation for a byte array and given byte order
509+
*
510+
* @param byteArray
511+
* @param byteOrder
512+
* @return hashCode
513+
*/
514+
static int hashCode(byte[] byteArray, ByteOrder byteOrder) {
515+
int result = Arrays.hashCode(byteArray);
516+
result = 31 * result + (byteOrder != null ? byteOrder.hashCode() : 0);
517+
return result;
518+
}
519+
492520
/*
493521
=================================================================================================
494522
Copyright 2011 Twitter, Inc.

0 commit comments

Comments
 (0)