Last active
August 29, 2015 14:18
-
-
Save scottcagno/17b5ede0a32e67ad68dc to your computer and use it in GitHub Desktop.
A simplistic byte[] wrapper that has better memory performance than a ByteBuffer... easy to use with collections like HashMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.nio.charset.StandardCharsets; | |
| public final class Bytes { | |
| protected final byte[] buffer; | |
| public Bytes(byte[] buffer) { | |
| this.buffer = buffer; | |
| } | |
| public Bytes(String str) { | |
| this.buffer = str.getBytes(StandardCharsets.UTF_8); | |
| } | |
| public boolean equals(Object o) { | |
| if(o != null) { | |
| if (o instanceof Bytes) { | |
| Bytes other = (Bytes) o; | |
| return other.size() == this.size() && checkEquals(other.buffer, this.buffer, size()); | |
| } | |
| } | |
| return false; | |
| } | |
| public int hashCode() { | |
| int h = 1, max = this.buffer.length; | |
| for(int i = 0; i < max; i++) | |
| h = 31 * h + rehash(this.buffer[i]); | |
| return h; | |
| } | |
| public Bytes makeKey(Object key) { | |
| return key instanceof byte[] ? new Bytes((byte[]) key) : (Bytes) key; | |
| } | |
| public byte[] getBuffer() { | |
| return buffer; | |
| } | |
| public int size() { | |
| return buffer.length; | |
| } | |
| public String toString() { | |
| return new String(buffer, StandardCharsets.UTF_8); | |
| } | |
| private boolean checkEquals(byte [] b1, byte [] b2, int length) { | |
| for (int i = 0; i < length; i++) | |
| if (!((b1[i]) == (b2[i]))) | |
| return false; | |
| return true; | |
| } | |
| // murmur hash 3 algorithm (32-bit/int finalizer) | |
| private static int rehash(int k) { | |
| k ^= k >>> 16; | |
| k *= 0x85ebca6b; | |
| k ^= k >>> 13; | |
| k *= 0xc2b2ae35; | |
| k ^= k >>> 16; | |
| return k; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment