Example usage for com.google.common.io ByteSource read

List of usage examples for com.google.common.io ByteSource read

Introduction

In this page you can find the example usage for com.google.common.io ByteSource read.

Prototype

@Beta
public <T> T read(ByteProcessor<T> processor) throws IOException 

Source Link

Document

Reads the contents of this byte source using the given processor to process bytes as they are read.

Usage

From source file:net.derquinse.common.base.Digests.java

/**
 * Computes and returns as a byte string the digest of the provided data.
 *///from  www  . jav a2s.  co m
public static ByteString getDigest(ByteSource source, final MessageDigest md) throws IOException {
    checkNotNull(source);
    checkNotNull(md);
    final byte[] digest = source.read(new ByteProcessor<byte[]>() {
        @Override
        public boolean processBytes(byte[] buf, int off, int len) {
            md.update(buf, off, len);
            return true;
        }

        @Override
        public byte[] getResult() {
            return md.digest();
        }
    });
    return ByteString.copyFrom(digest);
}