Java Path Create nio createChecksums(final Path file, final Map functions)

Here you can find the source of createChecksums(final Path file, final Map functions)

Description

create Checksums

License

Open Source License

Declaration

public static Map<String, HashCode> createChecksums(final Path file, final Map<String, HashFunction> functions)
            throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Jens Reimann./*from  w  w w .  j a  v  a 2  s  .co m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Jens Reimann - initial API and implementation
 *******************************************************************************/

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hasher;

public class Main {
    public static Map<String, HashCode> createChecksums(final Path file, final Map<String, HashFunction> functions)
            throws IOException {
        if (functions.isEmpty()) {
            return Collections.emptyMap();
        }

        // init hashers

        final Map<String, Hasher> hasherMap = new HashMap<>();
        final Hasher[] hashers = new Hasher[functions.size()];
        int i = 0;
        for (final Map.Entry<String, HashFunction> entry : functions.entrySet()) {
            hashers[i] = entry.getValue().newHasher();
            hasherMap.put(entry.getKey(), hashers[i]);
            i++;
        }

        // read data

        try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(file.toString()))) {
            final byte[] buffer = new byte[4096];
            int len;
            while ((len = is.read(buffer)) >= 0) {
                for (final Hasher hasher : hashers) {
                    hasher.putBytes(buffer, 0, len);
                }
            }
        }

        // finalize hashes

        final Map<String, HashCode> result = new HashMap<String, HashCode>(hashers.length);
        for (final Map.Entry<String, Hasher> entry : hasherMap.entrySet()) {
            result.put(entry.getKey(), entry.getValue().hash());
        }

        // return result

        return result;
    }
}

Related

  1. clearAndCreate(Path dir)
  2. createACL(Path root, Path p, GroupPrincipal readOnly, GroupPrincipal readWrite)
  3. createAndGetOutputStream(String typename, String outputPath)
  4. createBufferedReader(Path path)
  5. createClassWithDepsNextExtra(int index, Path dir)
  6. createDir(Path p)
  7. createDirectoriesIfRequired(@Nonnull Path path)
  8. createDirectory(final Path parent, final String folderName)