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:net.derquinse.common.base.ByteString.java

/**
 * Converts the byte string to a {@link HashCode}.
 * @throws IllegalStateException if the string is empty.
 *//*from   w  w w  .  java 2s .c  o  m*/
public HashCode toHashCode() {
    checkState(bytes.length > 0, "The byte string must not be empty");
    return HashCode.fromBytes(bytes);
}

From source file:com.facebook.presto.bloomfilter.BloomFilter.java

public static HashCode readHash(Slice s) {
    if (s == null) {
        return HASH_CODE_NOT_FOUND;
    }//from  w  w  w  .  j av a 2  s  . com
    return HashCode.fromBytes(s.getBytes(0, 32));
}

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

@Override
public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext,
        AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles)
        throws ExecException, InterruptedException {
    Executor executor = actionExecutionContext.getExecutor();
    EventHandler eventHandler = executor.getEventHandler();
    StandaloneSpawnStrategy standaloneStrategy = Preconditions
            .checkNotNull(executor.getContext(StandaloneSpawnStrategy.class));

    if (executor.reportsSubcommands()) {
        executor.reportSubcommand(/*from   w  ww. j a v  a 2  s  . co m*/
                Label.print(spawn.getOwner().getLabel()) + " [" + spawn.getResourceOwner().prettyPrint() + "]",
                spawn.asShellCommand(executor.getExecRoot()));
    }

    if (!spawn.getExecutionInfo().containsKey("supports-workers")
            || !spawn.getExecutionInfo().get("supports-workers").equals("1")) {
        eventHandler.handle(Event
                .warn(String.format(ERROR_MESSAGE_PREFIX + REASON_NO_EXECUTION_INFO, spawn.getMnemonic())));
        standaloneStrategy.exec(spawn, actionExecutionContext);
        return;
    }

    // We assume that the spawn to be executed always gets a @flagfile argument, which contains the
    // flags related to the work itself (as opposed to start-up options for the executed tool).
    // Thus, we can extract the last element from its args (which will be the @flagfile), expand it
    // and put that into the WorkRequest instead.
    if (!Iterables.getLast(spawn.getArguments()).startsWith("@")) {
        throw new UserExecException(
                String.format(ERROR_MESSAGE_PREFIX + REASON_NO_FLAGFILE, spawn.getMnemonic()));
    }

    if (Iterables.isEmpty(spawn.getToolFiles())) {
        throw new UserExecException(String.format(ERROR_MESSAGE_PREFIX + REASON_NO_TOOLS, spawn.getMnemonic()));
    }

    executor.getEventBus().post(ActionStatusMessage.runningStrategy(spawn.getResourceOwner(), "worker"));

    FileOutErr outErr = actionExecutionContext.getFileOutErr();

    ImmutableList<String> args = ImmutableList.<String>builder()
            .addAll(spawn.getArguments().subList(0, spawn.getArguments().size() - 1)).add("--persistent_worker")
            .addAll(MoreObjects.firstNonNull(extraFlags.get(spawn.getMnemonic()), ImmutableList.<String>of()))
            .build();
    ImmutableMap<String, String> env = spawn.getEnvironment();

    try {
        ActionInputFileCache inputFileCache = actionExecutionContext.getActionInputFileCache();

        HashCode workerFilesHash = WorkerFilesHash.getWorkerFilesHash(spawn.getToolFiles(),
                actionExecutionContext);
        Map<PathFragment, Path> inputFiles = new SpawnHelpers(execRoot).getMounts(spawn,
                actionExecutionContext);
        Set<PathFragment> outputFiles = SandboxHelpers.getOutputFiles(spawn);
        WorkerKey key = new WorkerKey(args, env, execRoot, spawn.getMnemonic(), workerFilesHash, inputFiles,
                outputFiles, writeOutputFiles != null);

        WorkRequest.Builder requestBuilder = WorkRequest.newBuilder();
        expandArgument(requestBuilder, Iterables.getLast(spawn.getArguments()));

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

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

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

        WorkResponse response = execInWorker(eventHandler, key, requestBuilder.build(), maxRetries,
                writeOutputFiles);

        outErr.getErrorStream().write(response.getOutputBytes().toByteArray());

        if (response.getExitCode() != 0) {
            throw new UserExecException(
                    String.format("Worker process sent response with exit code: %d.", response.getExitCode()));
        }
    } catch (IOException e) {
        String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), env,
                execRoot.getPathString());
        throw new UserExecException(message, e);
    }
}

From source file:fr.inria.eventcloud.pubsub.Subscription.java

private static HashCode fromString(String hashCode) {
    return HashCode.fromBytes(DatatypeConverter.parseHexBinary(hashCode));
}

From source file:com.google.devtools.build.lib.unix.NativePosixFiles.java

/**
 * Returns the MD5 digest of the specified file, following symbolic links.
 *
 * @param path the file whose MD5 digest is required.
 * @return the MD5 digest, as a {@link HashCode}
 * @throws IOException if the call failed for any reason.
 *///from www  .  j a  v a  2  s.  c o  m
public static HashCode md5sum(String path) throws IOException {
    return HashCode.fromBytes(md5sumAsBytes(path));
}

From source file:org.jclouds.kinetic.strategy.internal.KineticStorageStrategyImpl.java

private Blob createBlobFromByteSource(final String container, final String key, final ByteSource byteSource) {
    BlobBuilder builder = blobBuilders.get();
    builder.name(key);//from   w ww  .j a  va2 s. c o m
    File file = getFileForBlobKey(container, key);
    try {
        String cacheControl = null;
        String contentDisposition = null;
        String contentEncoding = null;
        String contentLanguage = null;
        String contentType = null;
        HashCode hashCode = null;
        Date expires = null;
        ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder();

        UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath());
        if (view != null) {
            Set<String> attributes = ImmutableSet.copyOf(view.list());

            cacheControl = readStringAttributeIfPresent(view, attributes, XATTR_CACHE_CONTROL);
            contentDisposition = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_DISPOSITION);
            contentEncoding = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_ENCODING);
            contentLanguage = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_LANGUAGE);
            contentType = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_TYPE);
            if (contentType == null && autoDetectContentType) {
                contentType = probeContentType(file.toPath());
            }
            if (attributes.contains(XATTR_CONTENT_MD5)) {
                ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_CONTENT_MD5));
                view.read(XATTR_CONTENT_MD5, buf);
                hashCode = HashCode.fromBytes(buf.array());
            }
            if (attributes.contains(XATTR_EXPIRES)) {
                ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_EXPIRES));
                view.read(XATTR_EXPIRES, buf);
                buf.flip();
                expires = new Date(buf.asLongBuffer().get());
            }
            for (String attribute : attributes) {
                if (!attribute.startsWith(XATTR_USER_METADATA_PREFIX)) {
                    continue;
                }
                String value = readStringAttributeIfPresent(view, attributes, attribute);
                userMetadata.put(attribute.substring(XATTR_USER_METADATA_PREFIX.length()), value);
            }

            builder.payload(byteSource).cacheControl(cacheControl).contentDisposition(contentDisposition)
                    .contentEncoding(contentEncoding).contentLanguage(contentLanguage)
                    .contentLength(byteSource.size()).contentMD5(hashCode).contentType(contentType)
                    .expires(expires).userMetadata(userMetadata.build());
        } else {
            builder.payload(byteSource).contentLength(byteSource.size())
                    .contentMD5(byteSource.hash(Hashing.md5()).asBytes());
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    Blob blob = builder.build();
    blob.getMetadata().setContainer(container);
    blob.getMetadata().setLastModified(new Date(file.lastModified()));
    blob.getMetadata().setSize(file.length());
    if (blob.getPayload().getContentMetadata().getContentMD5() != null)
        blob.getMetadata()
                .setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5()));
    return blob;
}

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

@VisibleForTesting
ImmutableList<RuleKey> directoryFileContentHashes() throws SQLException {
    ImmutableList.Builder<RuleKey> keys = ImmutableList.builder();
    try (ResultSet rs = db.connection.createStatement()
            .executeQuery("SELECT sha1 FROM content WHERE filepath NOTNULL")) {
        while (rs.next()) {
            keys.add(new RuleKey(HashCode.fromBytes(rs.getBytes(1))));
        }/*  ww w .  ja  v a  2s .  c  om*/
    }
    return keys.build();
}

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

@VisibleForTesting
ImmutableList<RuleKey> inlinedArtifactContentHashes() throws SQLException {
    ImmutableList.Builder<RuleKey> keys = ImmutableList.builder();
    try (ResultSet rs = db.connection.createStatement()
            .executeQuery("SELECT sha1 FROM content WHERE artifact NOTNULL")) {
        while (rs.next()) {
            keys.add(new RuleKey(HashCode.fromBytes(rs.getBytes(1))));
        }/*from   www.  j av  a  2 s.co  m*/
    }
    return keys.build();
}

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

@VisibleForTesting
ImmutableList<RuleKey> metadataRuleKeys() throws SQLException {
    ImmutableList.Builder<RuleKey> keys = ImmutableList.builder();
    try (ResultSet rs = db.connection.createStatement().executeQuery("SELECT rulekey FROM metadata")) {
        while (rs.next()) {
            keys.add(new RuleKey(HashCode.fromBytes(rs.getBytes(1))));
        }/*from  ww w.  j ava  2  s.  c om*/
    }
    return keys.build();
}

From source file:com.bouncestorage.swiftproxy.v1.ObjectResource.java

@PUT
public Response putObject(@NotNull @PathParam("container") String container,
        @NotNull @Encoded @PathParam("object") String objectName, @NotNull @PathParam("account") String account,
        @QueryParam("multipart-manifest") String multiPartManifest, @QueryParam("signature") String signature,
        @QueryParam("expires") String expires, @HeaderParam(DYNAMIC_OBJECT_MANIFEST) String objectManifest,
        @HeaderParam("X-Auth-Token") String authToken,
        @HeaderParam(HttpHeaders.CONTENT_LENGTH) String contentLengthParam,
        @HeaderParam("Transfer-Encoding") String transferEncoding,
        @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType contentType,
        @HeaderParam("X-Detect-Content-Type") boolean detectContentType,
        @HeaderParam("X-Copy-From") String copyFrom, @HeaderParam("X-Copy-From-Account") String copyFromAccount,
        @HeaderParam(HttpHeaders.ETAG) String eTag,
        @HeaderParam(HttpHeaders.CONTENT_DISPOSITION) String contentDisposition,
        @HeaderParam(HttpHeaders.CONTENT_ENCODING) String contentEncoding,
        @HeaderParam("X-Delete-At") long deleteAt, @HeaderParam("X-Delete-After") long deleteAfter,
        @HeaderParam(HttpHeaders.IF_MATCH) String ifMatch,
        @HeaderParam(HttpHeaders.IF_NONE_MATCH) String ifNoneMatch,
        @HeaderParam(HttpHeaders.IF_MODIFIED_SINCE) Date ifModifiedSince,
        @HeaderParam(HttpHeaders.IF_UNMODIFIED_SINCE) Date ifUnmodifiedSince,
        @HeaderParam(SwiftHeaders.OBJECT_COPY_FRESH_METADATA) boolean freshMetadata, @Context Request request) {
    //objectName = normalizePath(objectName);
    if (objectName.length() > InfoResource.CONFIG.swift.max_object_name_length) {
        return badRequest();
    }//from  w w w.  j  a  v  a  2s  .  com
    if (transferEncoding != null && !"chunked".equals(transferEncoding)) {
        return Response.status(Response.Status.NOT_IMPLEMENTED).build();
    }

    if (contentLengthParam == null && !"chunked".equals(transferEncoding)) {
        return Response.status(Response.Status.LENGTH_REQUIRED).build();
    }
    long contentLength = contentLengthParam == null ? 0 : Long.parseLong(contentLengthParam);

    logger.info("PUT {}", objectName);

    if (copyFromAccount == null) {
        copyFromAccount = account;
    }

    if (copyFrom != null) {
        Pair<String, String> copy = validateCopyParam(copyFrom);
        return copyObject(copy.getFirst(), copy.getSecond(), copyFromAccount, authToken,
                container + "/" + objectName, account, null, contentType.toString(), contentEncoding,
                contentDisposition, ifMatch, ifModifiedSince, ifUnmodifiedSince, freshMetadata, request);
    }

    Map<String, String> metadata = getUserMetadata(request);
    validateUserMetadata(metadata);
    InputStream copiedStream = null;

    BlobStore blobStore = getBlobStore(authToken).get(container, objectName);
    if ("put".equals(multiPartManifest)) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try (TeeInputStream tee = new TeeInputStream(request.getInputStream(), buffer, true)) {
            ManifestEntry[] manifest = readSLOManifest(tee);
            validateManifest(manifest, blobStore, authToken);
            Pair<Long, String> sizeAndEtag = getManifestTotalSizeAndETag(Arrays.asList(manifest));
            metadata.put(STATIC_OBJECT_MANIFEST, sizeAndEtag.getFirst() + " " + sizeAndEtag.getSecond());
            copiedStream = new ByteArrayInputStream(buffer.toByteArray());
        } catch (IOException e) {
            throw propagate(e);
        }
    } else if (objectManifest != null) {
        metadata.put(DYNAMIC_OBJECT_MANIFEST, objectManifest);
    }

    if (!blobStore.containerExists(container)) {
        return notFound();
    }

    HashCode contentMD5 = null;
    if (eTag != null) {
        try {
            contentMD5 = HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(eTag));
        } catch (IllegalArgumentException iae) {
            throw new ClientErrorException(422, iae); // Unprocessable Entity
        }
        if (contentMD5.bits() != Hashing.md5().bits()) {
            // Unprocessable Entity
            throw new ClientErrorException(contentMD5.bits() + " != " + Hashing.md5().bits(), 422);
        }
    }

    try (InputStream is = copiedStream != null ? copiedStream : request.getInputStream()) {
        BlobBuilder.PayloadBlobBuilder builder = blobStore.blobBuilder(objectName).userMetadata(metadata)
                .payload(is);
        if (contentDisposition != null) {
            builder.contentDisposition(contentDisposition);
        }
        if (contentEncoding != null) {
            builder.contentEncoding(contentEncoding);
        }
        if (contentType != null) {
            builder.contentType(contentType.toString());
        }
        if (contentLengthParam != null) {
            builder.contentLength(contentLength);
        }
        if (contentMD5 != null) {
            builder.contentMD5(contentMD5);
        }
        try {
            String remoteETag;
            try {
                remoteETag = blobStore.putBlob(container, builder.build());
            } catch (HttpResponseException e) {
                HttpResponse response = e.getResponse();
                if (response == null) {
                    throw e;
                }
                int code = response.getStatusCode();

                if (code == 400 && !"openstack-swift".equals(blobStore.getContext().unwrap().getId())) {
                    // swift expects 422 for md5 mismatch
                    throw new ClientErrorException(response.getStatusLine(), 422, e.getCause());
                } else {
                    throw new ClientErrorException(response.getStatusLine(), code, e.getCause());
                }
            }
            BlobMetadata meta = blobStore.blobMetadata(container, objectName);
            return Response.status(Response.Status.CREATED).header(HttpHeaders.ETAG, remoteETag)
                    .header(HttpHeaders.LAST_MODIFIED, meta.getLastModified())
                    .header(HttpHeaders.CONTENT_LENGTH, 0).header(HttpHeaders.CONTENT_TYPE, contentType)
                    .header(HttpHeaders.DATE, new Date()).build();
        } catch (ContainerNotFoundException e) {
            return notFound();
        }
    } catch (IOException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}