001package org.apache.archiva.checksum; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.FileNotFoundException; 023import java.io.IOException; 024import java.nio.MappedByteBuffer; 025import java.nio.channels.FileChannel; 026import java.nio.file.Path; 027import java.nio.file.StandardOpenOption; 028import java.util.List; 029import java.util.stream.Collectors; 030 031/** 032 * Utility class that handles multiple checksums for a single file. 033 */ 034public class ChecksumUtil { 035 036 037 static final int BUFFER_SIZE = 32768; 038 039 public static void update(List<Checksum> checksumList, Path file ) throws IOException { 040 long fileSize; 041 try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ )) { 042 fileSize = channel.size(); 043 long pos = 0; 044 while (pos < fileSize) { 045 long bufferSize = Math.min(BUFFER_SIZE, fileSize - pos); 046 MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, pos, bufferSize); 047 for (Checksum checksum : checksumList) { 048 checksum.update(buffer); 049 buffer.rewind(); 050 } 051 fileSize = channel.size(); 052 pos += BUFFER_SIZE; 053 } 054 for (Checksum checksum : checksumList) { 055 checksum.finish(); 056 } 057 } 058 } 059 060 public static void update(Checksum checksum, Path file) 061 throws IOException 062 { 063 long fileSize; 064 try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ )) { 065 fileSize = channel.size(); 066 long pos = 0; 067 while (pos<fileSize) 068 { 069 long bufferSize = Math.min(BUFFER_SIZE, fileSize-pos); 070 MappedByteBuffer buffer = channel.map( FileChannel.MapMode.READ_ONLY, pos, bufferSize); 071 checksum.update( buffer ); 072 buffer.rewind(); 073 fileSize = channel.size(); 074 pos += BUFFER_SIZE; 075 } 076 checksum.finish(); 077 } 078 } 079 080 public static List<Checksum> initializeChecksums(Path file, List<ChecksumAlgorithm> checksumAlgorithms) throws IOException { 081 final List<Checksum> checksums = newChecksums(checksumAlgorithms); 082 update(checksums, file); 083 return checksums; 084 } 085 086 /** 087 * Returns the list of configured checksum types. 088 * 089 * @param checksumTypes The list of checksum strings 090 * @return The list of checksum objects 091 */ 092 public static List<ChecksumAlgorithm> getAlgorithms(List<String> checksumTypes) { 093 return checksumTypes.stream().map(ca -> 094 ChecksumAlgorithm.valueOf(ca.toUpperCase())).collect(Collectors.toList()); 095 } 096 097 public static List<Checksum> newChecksums(List<ChecksumAlgorithm> checksumAlgorithms) { 098 return checksumAlgorithms.stream().map( a -> new Checksum(a)).collect(Collectors.toList()); 099 } 100 101}