Example usage for com.google.common.hash HashCode fromBytes

List of usage examples for com.google.common.hash HashCode fromBytes

Introduction

In this page you can find the example usage for com.google.common.hash HashCode fromBytes.

Prototype

@CheckReturnValue
public static HashCode fromBytes(byte[] bytes) 

Source Link

Document

Creates a HashCode from a byte array.

Usage

From source file:com.facebook.presto.ml.ModelUtils.java

public static Model deserialize(Slice slice) {
    int version = slice.getInt(VERSION_OFFSET);
    checkArgument(version == CURRENT_FORMAT_VERSION, format("Unsupported version: %d", version));

    byte[] modelHashBytes = slice.getBytes(HASH_OFFSET, 32);
    HashCode expectedHash = HashCode.fromBytes(modelHashBytes);
    HashCode actualHash = Hashing.sha256()
            .hashBytes(slice.getBytes(ALGORITHM_OFFSET, slice.length() - ALGORITHM_OFFSET));
    checkArgument(actualHash.equals(expectedHash), "model hash does not match data");

    int id = slice.getInt(ALGORITHM_OFFSET);
    Class<? extends Model> algorithm = MODEL_SERIALIZATION_IDS.inverse().get(id);
    requireNonNull(algorithm, format("Unsupported algorith %d", id));

    int hyperparameterLength = slice.getInt(HYPERPARAMETER_LENGTH_OFFSET);

    byte[] hyperparameterBytes = slice.getBytes(HYPERPARAMETERS_OFFSET, hyperparameterLength);

    int dataLengthOffset = HYPERPARAMETERS_OFFSET + hyperparameterLength;
    long dataLength = slice.getLong(dataLengthOffset);

    int dataOffset = dataLengthOffset + SIZE_OF_LONG;
    byte[] data = slice.getBytes(dataOffset, (int) dataLength);

    try {// w ww  .j a  v a2 s  . c o m
        Method deserialize = algorithm.getMethod("deserialize", byte[].class);
        return (Model) deserialize.invoke(null, new Object[] { data });
    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.gradle.api.internal.changedetection.state.TaskExecutionSnapshotSerializer.java

private static ImplementationSnapshot readImplementation(Decoder decoder) throws IOException {
    String typeName = decoder.readString();
    HashCode classLoaderHash = decoder.readBoolean() ? HashCode.fromBytes(decoder.readBinary()) : null;
    return new ImplementationSnapshot(typeName, classLoaderHash);
}

From source file:com.facebook.buck.artifact_cache.HttpArtifactCacheBinaryProtocol.java

public static MetadataAndPayloadReadResultInternal readMetadataAndPayload(DataInputStream input,
        OutputStream payloadSink) throws IOException {
    // Read the size of a the metadata, and use that to build a input stream to read and
    // process the rest of it.
    int metadataSize = input.readInt();
    if (metadataSize > MAX_METADATA_HEADER_SIZE) {
        throw new IOException(String.format("Metadata header size of %d is too big.", metadataSize));
    }//from  w ww  .  j  av  a 2s  .c  om
    MetadataAndPayloadReadResultInternal.Builder result = MetadataAndPayloadReadResultInternal.builder();
    // Create a hasher to be used to generate a hash of the metadata and input.  We'll use
    // this to compare against the embedded checksum.
    Hasher hasher = HASH_FUNCTION.newHasher();
    byte[] rawMetadata = new byte[metadataSize];
    ByteStreams.readFully(input, rawMetadata);
    try (InputStream rawMetadataIn = new ByteArrayInputStream(rawMetadata)) {

        // The first part of the metadata needs to be included in the hash.
        try (DataInputStream metadataIn = new DataInputStream(new HasherInputStream(hasher, rawMetadataIn))) {

            // Read in the rule keys that stored this artifact, and add them to the hash we're
            // building up.
            int size = metadataIn.readInt();
            for (int i = 0; i < size; i++) {
                result.addRuleKeys(new RuleKey(metadataIn.readUTF()));
            }

            // Read in the actual metadata map, and add it the hash.
            size = metadataIn.readInt();
            for (int i = 0; i < size; i++) {
                String key = metadataIn.readUTF();
                int valSize = metadataIn.readInt();
                byte[] val = new byte[valSize];
                ByteStreams.readFully(metadataIn, val);
                result.putMetadata(key, new String(val, Charsets.UTF_8));
            }
        }

        // Next, read in the embedded expected checksum, which should be the last byte in
        // the metadata header.
        byte[] hashCodeBytes = new byte[HASH_FUNCTION.bits() / Byte.SIZE];
        ByteStreams.readFully(rawMetadataIn, hashCodeBytes);
        result.setExpectedHashCode(HashCode.fromBytes(hashCodeBytes));
    }

    // The remaining data is the payload, which we write to the created file, and also include
    // in our verification checksum.
    Hasher artifactOnlyHasher = HASH_FUNCTION.newHasher();
    try (InputStream payload = new HasherInputStream(artifactOnlyHasher,
            new HasherInputStream(hasher, input))) {
        result.setResponseSizeBytes(ByteStreams.copy(payload, payloadSink));
        result.setArtifactOnlyHashCode(artifactOnlyHasher.hash());
    }

    result.setActualHashCode(hasher.hash());

    return result.build();
}

From source file:org.jclouds.googlecloudstorage.domain.GCSObject.java

public HashCode getMd5HashCode() {
    if (md5Hash != null) {
        HashCode hc = HashCode.fromBytes(BaseEncoding.base64().decode(md5Hash));
        return hc;
    }/* www .ja va2  s . c o m*/
    return null;
}

From source file:com.facebook.buck.rules.keys.CategorizedRuleKeyHasher.java

@Override
public HashCode hash() {
    byte[] bytes = new byte[RuleKeyFieldCategory.values().length * 4];
    int offset = 0;
    for (RuleKeyFieldCategory category : RuleKeyFieldCategory.values()) {
        writeBytes(bytes, offset, hashers.get(category).hash());
        offset += 4;//from  ww  w .  j  a  v  a2 s.c  o  m
    }
    return HashCode.fromBytes(bytes);
}

From source file:io.bazel.rules.closure.worker.PersistentWorker.java

private int runAsPersistentWorker() throws IOException, InterruptedException {
    InputStream realStdIn = System.in;
    PrintStream realStdOut = System.out;
    PrintStream realStdErr = System.err;
    try (InputStream emptyIn = new ByteArrayInputStream(new byte[0]);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(buffer)) {
        System.setIn(emptyIn);/*from  www .j av  a  2s . c  o  m*/
        System.setOut(ps);
        System.setErr(ps);
        while (true) {
            WorkRequest request = WorkRequest.parseDelimitedFrom(realStdIn);
            if (request == null) {
                return 0;
            }
            for (Input input : request.getInputsList()) {
                inputDigests.put(fs.getPath(input.getPath()),
                        HashCode.fromBytes(input.getDigest().toByteArray()));
            }
            arguments.addAll(request.getArgumentsList());
            loadArguments(true);
            int exitCode = runProgram(ps, Collections.unmodifiableMap(inputDigests));
            WorkResponse.newBuilder().setOutput(new String(buffer.toByteArray(), UTF_8)).setExitCode(exitCode)
                    .build().writeDelimitedTo(realStdOut);
            realStdOut.flush();
            buffer.reset();
            arguments.clear();
            inputDigests.clear();
        }
    } finally {
        System.setIn(realStdIn);
        System.setOut(realStdOut);
        System.setErr(realStdErr);
    }
}

From source file:org.jclouds.googlecloudstorage.domain.GCSObject.java

public HashCode getCrc32cHashcode() {
    if (crc32c != null) {
        HashCode hc = HashCode.fromBytes(DomainUtils.reverse(BaseEncoding.base64().decode(crc32c)));
        return hc;
    }//from   ww  w  . j a  va 2 s  . c om
    return null;

}

From source file:com.google.devtools.build.lib.syntax.BuildFileAST.java

public static BuildFileAST parseSkylarkFile(Path file, long fileSize, EventHandler eventHandler)
        throws IOException {
    ParserInputSource input = ParserInputSource.create(file, fileSize);
    Parser.ParseResult result = Parser.parseFileForSkylark(input, eventHandler);
    return create(ImmutableList.<Statement>of(), result, HashCode.fromBytes(file.getDigest()).toString(),
            eventHandler);//from ww w  .  j av  a2s.  co  m
}

From source file:com.google.devtools.build.lib.worker.WorkerSpawnRunner.java

private WorkRequest createWorkRequest(Spawn spawn, SpawnExecutionPolicy policy, List<String> flagfiles,
        ActionInputFileCache inputFileCache) throws IOException {
    WorkRequest.Builder requestBuilder = WorkRequest.newBuilder();
    for (String flagfile : flagfiles) {
        expandArgument(execRoot, flagfile, requestBuilder);
    }//from   w  w w.j  a v  a 2  s.c  o m

    List<ActionInput> inputs = ActionInputHelper.expandArtifacts(spawn.getInputFiles(),
            policy.getArtifactExpander());

    for (ActionInput input : inputs) {
        byte[] digestBytes = inputFileCache.getMetadata(input).getDigest();
        ByteString digest;
        if (digestBytes == null) {
            digest = ByteString.EMPTY;
        } else {
            digest = ByteString.copyFromUtf8(HashCode.fromBytes(digestBytes).toString());
        }

        requestBuilder.addInputsBuilder().setPath(input.getExecPathString()).setDigest(digest).build();
    }
    return requestBuilder.build();
}

From source file:com.google.devtools.build.lib.exec.SpawnLogContext.java

/**
 * Computes the digest of the given ActionInput or corresponding path. Will try to access the
 * Metadata cache first, if it is available, and fall back to digesting the contents manually.
 */// www  . ja va  2  s . co  m
private Digest computeDigest(@Nullable ActionInput input, @Nullable Path path,
        MetadataProvider metadataProvider) throws IOException {
    Preconditions.checkArgument(input != null || path != null);
    FileSystem.HashFunction hashFunction = execRoot.getFileSystem().getDigestFunction();
    Digest.Builder digest = Digest.newBuilder().setHashFunctionName(hashFunction.toString());
    if (input != null) {
        if (input instanceof VirtualActionInput) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ((VirtualActionInput) input).writeTo(buffer);
            byte[] blob = buffer.toByteArray();
            return digest.setHash(hashFunction.getHash().hashBytes(blob).toString()).setSizeBytes(blob.length)
                    .build();
        }
        // Try to access the cached metadata, otherwise fall back to local computation.
        try {
            Metadata metadata = metadataProvider.getMetadata(input);
            if (metadata != null) {
                byte[] hash = metadata.getDigest();
                if (hash != null) {
                    return digest.setHash(HashCode.fromBytes(hash).toString()).setSizeBytes(metadata.getSize())
                            .build();
                }
            }
        } catch (IOException | IllegalStateException e) {
            // Pass through to local computation.
        }
    }
    if (path == null) {
        path = execRoot.getRelative(input.getExecPath());
    }
    // Compute digest manually.
    return digest.setHash(HashCode.fromBytes(path.getDigest()).toString()).setSizeBytes(path.getFileSize())
            .build();
}