Coverage Summary for Class: AbstractByteHasher (com.google.common.hash)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| AbstractByteHasher | 0% (0/1) | 0% (0/13) | 0% (0/32) |
1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.hash; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 import static com.google.common.base.Preconditions.checkPositionIndexes; 19 20 import com.google.common.primitives.Chars; 21 import com.google.common.primitives.Ints; 22 import com.google.common.primitives.Longs; 23 import com.google.common.primitives.Shorts; 24 import com.google.errorprone.annotations.CanIgnoreReturnValue; 25 import java.nio.ByteBuffer; 26 import java.nio.ByteOrder; 27 28 /** 29 * Abstract {@link Hasher} that handles converting primitives to bytes using a scratch {@code 30 * ByteBuffer} and streams all bytes to a sink to compute the hash. 31 * 32 * @author Colin Decker 33 */ 34 @CanIgnoreReturnValue 35 @ElementTypesAreNonnullByDefault 36 abstract class AbstractByteHasher extends AbstractHasher { 37 private final ByteBuffer scratch = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN); 38 39 /** Updates this hasher with the given byte. */ 40 protected abstract void update(byte b); 41 42 /** Updates this hasher with the given bytes. */ 43 protected void update(byte[] b) { 44 update(b, 0, b.length); 45 } 46 47 /** Updates this hasher with {@code len} bytes starting at {@code off} in the given buffer. */ 48 protected void update(byte[] b, int off, int len) { 49 for (int i = off; i < off + len; i++) { 50 update(b[i]); 51 } 52 } 53 54 /** Updates this hasher with bytes from the given buffer. */ 55 protected void update(ByteBuffer b) { 56 if (b.hasArray()) { 57 update(b.array(), b.arrayOffset() + b.position(), b.remaining()); 58 Java8Compatibility.position(b, b.limit()); 59 } else { 60 for (int remaining = b.remaining(); remaining > 0; remaining--) { 61 update(b.get()); 62 } 63 } 64 } 65 66 /** Updates the sink with the given number of bytes from the buffer. */ 67 private Hasher update(int bytes) { 68 try { 69 update(scratch.array(), 0, bytes); 70 } finally { 71 Java8Compatibility.clear(scratch); 72 } 73 return this; 74 } 75 76 @Override 77 public Hasher putByte(byte b) { 78 update(b); 79 return this; 80 } 81 82 @Override 83 public Hasher putBytes(byte[] bytes) { 84 checkNotNull(bytes); 85 update(bytes); 86 return this; 87 } 88 89 @Override 90 public Hasher putBytes(byte[] bytes, int off, int len) { 91 checkPositionIndexes(off, off + len, bytes.length); 92 update(bytes, off, len); 93 return this; 94 } 95 96 @Override 97 public Hasher putBytes(ByteBuffer bytes) { 98 update(bytes); 99 return this; 100 } 101 102 @Override 103 public Hasher putShort(short s) { 104 scratch.putShort(s); 105 return update(Shorts.BYTES); 106 } 107 108 @Override 109 public Hasher putInt(int i) { 110 scratch.putInt(i); 111 return update(Ints.BYTES); 112 } 113 114 @Override 115 public Hasher putLong(long l) { 116 scratch.putLong(l); 117 return update(Longs.BYTES); 118 } 119 120 @Override 121 public Hasher putChar(char c) { 122 scratch.putChar(c); 123 return update(Chars.BYTES); 124 } 125 }