Coverage Summary for Class: AbstractNonStreamingHashFunction (com.google.common.hash)

Class Method, % Line, %
AbstractNonStreamingHashFunction 0% (0/8) 0% (0/13)
AbstractNonStreamingHashFunction$BufferingHasher 0% (0/5) 0% (0/9)
AbstractNonStreamingHashFunction$ExposedByteArrayOutputStream 0% (0/4) 0% (0/8)
Total 0% (0/17) 0% (0/30)


1 /* 2  * Copyright (C) 2011 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 com.google.common.base.Preconditions; 18 import com.google.errorprone.annotations.Immutable; 19 import java.io.ByteArrayOutputStream; 20 import java.nio.ByteBuffer; 21 import java.nio.ByteOrder; 22 import java.nio.charset.Charset; 23 import java.util.Arrays; 24  25 /** 26  * Skeleton implementation of {@link HashFunction}, appropriate for non-streaming algorithms. All 27  * the hash computation done using {@linkplain #newHasher()} are delegated to the {@linkplain 28  * #hashBytes(byte[], int, int)} method. 29  * 30  * @author Dimitris Andreou 31  */ 32 @Immutable 33 @ElementTypesAreNonnullByDefault 34 abstract class AbstractNonStreamingHashFunction extends AbstractHashFunction { 35  @Override 36  public Hasher newHasher() { 37  return newHasher(32); 38  } 39  40  @Override 41  public Hasher newHasher(int expectedInputSize) { 42  Preconditions.checkArgument(expectedInputSize >= 0); 43  return new BufferingHasher(expectedInputSize); 44  } 45  46  @Override 47  public HashCode hashInt(int input) { 48  return hashBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(input).array()); 49  } 50  51  @Override 52  public HashCode hashLong(long input) { 53  return hashBytes(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(input).array()); 54  } 55  56  @Override 57  public HashCode hashUnencodedChars(CharSequence input) { 58  int len = input.length(); 59  ByteBuffer buffer = ByteBuffer.allocate(len * 2).order(ByteOrder.LITTLE_ENDIAN); 60  for (int i = 0; i < len; i++) { 61  buffer.putChar(input.charAt(i)); 62  } 63  return hashBytes(buffer.array()); 64  } 65  66  @Override 67  public HashCode hashString(CharSequence input, Charset charset) { 68  return hashBytes(input.toString().getBytes(charset)); 69  } 70  71  @Override 72  public abstract HashCode hashBytes(byte[] input, int off, int len); 73  74  @Override 75  public HashCode hashBytes(ByteBuffer input) { 76  return newHasher(input.remaining()).putBytes(input).hash(); 77  } 78  79  /** In-memory stream-based implementation of Hasher. */ 80  private final class BufferingHasher extends AbstractHasher { 81  final ExposedByteArrayOutputStream stream; 82  83  BufferingHasher(int expectedInputSize) { 84  this.stream = new ExposedByteArrayOutputStream(expectedInputSize); 85  } 86  87  @Override 88  public Hasher putByte(byte b) { 89  stream.write(b); 90  return this; 91  } 92  93  @Override 94  public Hasher putBytes(byte[] bytes, int off, int len) { 95  stream.write(bytes, off, len); 96  return this; 97  } 98  99  @Override 100  public Hasher putBytes(ByteBuffer bytes) { 101  stream.write(bytes); 102  return this; 103  } 104  105  @Override 106  public HashCode hash() { 107  return hashBytes(stream.byteArray(), 0, stream.length()); 108  } 109  } 110  111  // Just to access the byte[] without introducing an unnecessary copy 112  private static final class ExposedByteArrayOutputStream extends ByteArrayOutputStream { 113  ExposedByteArrayOutputStream(int expectedInputSize) { 114  super(expectedInputSize); 115  } 116  117  void write(ByteBuffer input) { 118  int remaining = input.remaining(); 119  if (count + remaining > buf.length) { 120  buf = Arrays.copyOf(buf, count + remaining); 121  } 122  input.get(buf, count, remaining); 123  count += remaining; 124  } 125  126  byte[] byteArray() { 127  return buf; 128  } 129  130  int length() { 131  return count; 132  } 133  } 134 }