View Javadoc
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  abstract class AbstractByteHasher extends AbstractHasher {
36    private final ByteBuffer scratch = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
37  
38    /**
39     * Updates this hasher with the given byte.
40     */
41    protected abstract void update(byte b);
42  
43    /**
44     * Updates this hasher with the given bytes.
45     */
46    protected void update(byte[] b) {
47      update(b, 0, b.length);
48    }
49  
50    /**
51     * Updates this hasher with {@code len} bytes starting at {@code off} in the given buffer.
52     */
53    protected void update(byte[] b, int off, int len) {
54      for (int i = off; i < off + len; i++) {
55        update(b[i]);
56      }
57    }
58  
59    /** Updates this hasher with bytes from the given buffer. */
60    protected void update(ByteBuffer b) {
61      if (b.hasArray()) {
62        update(b.array(), b.arrayOffset() + b.position(), b.remaining());
63        b.position(b.limit());
64      } else {
65        for (int remaining = b.remaining(); remaining > 0; remaining--) {
66          update(b.get());
67        }
68      }
69    }
70  
71    @Override
72    public Hasher putByte(byte b) {
73      update(b);
74      return this;
75    }
76  
77    @Override
78    public Hasher putBytes(byte[] bytes) {
79      checkNotNull(bytes);
80      update(bytes);
81      return this;
82    }
83  
84    @Override
85    public Hasher putBytes(byte[] bytes, int off, int len) {
86      checkPositionIndexes(off, off + len, bytes.length);
87      update(bytes, off, len);
88      return this;
89    }
90  
91    @Override
92    public Hasher putBytes(ByteBuffer bytes) {
93      update(bytes);
94      return this;
95    }
96  
97    /**
98     * Updates the sink with the given number of bytes from the buffer.
99     */
100   private Hasher update(int bytes) {
101     try {
102       update(scratch.array(), 0, bytes);
103     } finally {
104       scratch.clear();
105     }
106     return this;
107   }
108 
109   @Override
110   public Hasher putShort(short s) {
111     scratch.putShort(s);
112     return update(Shorts.BYTES);
113   }
114 
115   @Override
116   public Hasher putInt(int i) {
117     scratch.putInt(i);
118     return update(Ints.BYTES);
119   }
120 
121   @Override
122   public Hasher putLong(long l) {
123     scratch.putLong(l);
124     return update(Longs.BYTES);
125   }
126 
127   @Override
128   public Hasher putChar(char c) {
129     scratch.putChar(c);
130     return update(Chars.BYTES);
131   }
132 }