Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License
  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  * or implied. See the License for the specific language governing permissions and limitations under
  * the License.
  */
 
 package com.google.common.hash;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
 
Skeleton implementation of HashFunction. Provides default implementations which invokes the appropriate method on HashFunction.newHasher(), then return the result of Hasher.hash().

Invocations of newHasher(int) also delegate to HashFunction.newHasher(), ignoring the expected input size parameter.

Author(s):
Kevin Bourrillion
 
 abstract class AbstractStreamingHashFunction implements HashFunction {
   @Override public <T> HashCode hashObject(T instanceFunnel<? super T> funnel) {
     return newHasher().putObject(instancefunnel).hash();
   }
 
     return newHasher().putUnencodedChars(input).hash();
   }
 
   @Override public HashCode hashString(CharSequence inputCharset charset) {
     return newHasher().putString(inputcharset).hash();
   }
 
   @Override public HashCode hashInt(int input) {
     return newHasher().putInt(input).hash();
   }
 
   @Override public HashCode hashLong(long input) {
     return newHasher().putLong(input).hash();
   }
 
   @Override public HashCode hashBytes(byte[] input) {
     return newHasher().putBytes(input).hash();
   }
 
   @Override public HashCode hashBytes(byte[] inputint offint len) {
     return newHasher().putBytes(inputofflen).hash();
   }
 
   @Override public Hasher newHasher(int expectedInputSize) {
     Preconditions.checkArgument(expectedInputSize >= 0);
     return newHasher();
   }

  
A convenience base class for implementors of Hasher; handles accumulating data until an entire "chunk" (of implementation-dependent length) is ready to be hashed.

Author(s):
Kevin Bourrillion
Dimitris Andreou
 
   // TODO(kevinb): this class still needs some design-and-document-for-inheritance love
   protected static abstract class AbstractStreamingHasher extends AbstractHasher {
    
Buffer via which we pass data to the hash algorithm (the implementor)
 
     private final ByteBuffer buffer;

    
Number of bytes to be filled before process() invocation(s).
 
     private final int bufferSize;

    
Number of bytes processed per process() invocation.
 
     private final int chunkSize;

    
Constructor for use by subclasses. This hasher instance will process chunks of the specified size.

Parameters:
chunkSize the number of bytes available per process(java.nio.ByteBuffer) invocation; must be at least 4
 
     protected AbstractStreamingHasher(int chunkSize) {
       this(chunkSizechunkSize);
     }

    
Constructor for use by subclasses. This hasher instance will process chunks of the specified size, using an internal buffer of bufferSize size, which must be a multiple of chunkSize.

Parameters:
chunkSize the number of bytes available per process(java.nio.ByteBuffer) invocation; must be at least 4
bufferSize the size of the internal buffer. Must be a multiple of chunkSize
    protected AbstractStreamingHasher(int chunkSizeint bufferSize) {
      // TODO(kevinb): check more preconditions (as bufferSize >= chunkSize) if this is ever public
      checkArgument(bufferSize % chunkSize == 0);
      // TODO(user): benchmark performance difference with longer buffer
      this. = ByteBuffer
          .allocate(bufferSize + 7) // always space for a single primitive
          .order(.);
      this. = bufferSize;
      this. = chunkSize;
    }

    
Processes the available bytes of the buffer (at most chunk bytes).
    protected abstract void process(ByteBuffer bb);

    
This is invoked for the last bytes of the input, which are not enough to fill a whole chunk. The passed ByteBuffer is guaranteed to be non-empty.

This implementation simply pads with zeros and delegates to process(java.nio.ByteBuffer).

    protected void processRemaining(ByteBuffer bb) {
      bb.position(bb.limit()); // move at the end
      bb.limit( + 7); // get ready to pad with longs
      while (bb.position() < ) {
        bb.putLong(0);
      }
      bb.limit();
      bb.flip();
      process(bb);
    }
    @Override
    public final Hasher putBytes(byte[] bytes) {
      return putBytes(bytes, 0, bytes.length);
    }
    @Override
    public final Hasher putBytes(byte[] bytesint offint len) {
      return putBytes(ByteBuffer.wrap(bytesofflen).order(.));
    }
    private Hasher putBytes(ByteBuffer readBuffer) {
      // If we have room for all of it, this is easy
      if (readBuffer.remaining() <= .remaining()) {
        .put(readBuffer);
        munchIfFull();
        return this;
      }
      // First add just enough to fill buffer size, and munch that
      int bytesToCopy =  - .position();
      for (int i = 0; i < bytesToCopyi++) {
        .put(readBuffer.get());
      }
      munch(); // buffer becomes empty here, since chunkSize divides bufferSize
      // Now process directly from the rest of the input buffer
      while (readBuffer.remaining() >= ) {
        process(readBuffer);
      }
      // Finally stick the remainder back in our usual buffer
      .put(readBuffer);
      return this;
    }
    @Override
    public final Hasher putUnencodedChars(CharSequence charSequence) {
      for (int i = 0; i < charSequence.length(); i++) {
        putChar(charSequence.charAt(i));
      }
      return this;
    }
    @Override
    public final Hasher putByte(byte b) {
      .put(b);
      munchIfFull();
      return this;
    }
    @Override
    public final Hasher putShort(short s) {
      .putShort(s);
      munchIfFull();
      return this;
    }
    @Override
    public final Hasher putChar(char c) {
      .putChar(c);
      munchIfFull();
      return this;
    }
    @Override
    public final Hasher putInt(int i) {
      .putInt(i);
      munchIfFull();
      return this;
    }
    @Override
    public final Hasher putLong(long l) {
      .putLong(l);
      munchIfFull();
      return this;
    }
    @Override
    public final <T> Hasher putObject(T instanceFunnel<? super T> funnel) {
      funnel.funnel(instancethis);
      return this;
    }
    @Override
    public final HashCode hash() {
      munch();
      .flip();
      if (.remaining() > 0) {
        processRemaining();
      }
      return makeHash();
    }
    abstract HashCode makeHash();
    // Process pent-up data in chunks
    private void munchIfFull() {
      if (.remaining() < 8) {
        // buffer is full; not enough room for a primitive. We have at least one full chunk.
        munch();
      }
    }
    private void munch() {
      .flip();
      while (.remaining() >= ) {
        // we could limit the buffer to ensure process() does not read more than
        // chunkSize number of bytes, but we trust the implementations
        process();
      }
      .compact(); // preserve any remaining data that do not make a full chunk
    }
  }
New to GrepCode? Check out our FAQ X