📄 checksum.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs;import java.security.DigestInputStream;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.io.File;import java.io.FileOutputStream;import java.io.FileInputStream;import java.io.FileReader;import java.io.BufferedReader;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Iterator;import java.util.Hashtable;import java.util.Enumeration;import java.util.Set;import java.util.Arrays;import java.text.MessageFormat;import java.text.ParseException;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.taskdefs.condition.Condition;import org.apache.tools.ant.types.EnumeratedAttribute;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.types.ResourceCollection;import org.apache.tools.ant.types.resources.Union;import org.apache.tools.ant.types.resources.Restrict;import org.apache.tools.ant.types.resources.FileResource;import org.apache.tools.ant.types.resources.selectors.Type;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.StringUtils;/** * Used to create or verify file checksums. * * @since Ant 1.5 * * @ant.task category="control" */public class Checksum extends MatchingTask implements Condition { private static class FileUnion extends Restrict { private Union u; FileUnion() { u = new Union(); super.add(u); super.add(Type.FILE); } public void add(ResourceCollection rc) { u.add(rc); } } /** * File for which checksum is to be calculated. */ private File file = null; /** * Root directory in which the checksum files will be written. * If not specified, the checksum files will be written * in the same directory as each file. */ private File todir; /** * MessageDigest algorithm to be used. */ private String algorithm = "MD5"; /** * MessageDigest Algorithm provider */ private String provider = null; /** * File Extension that is be to used to create or identify * destination file */ private String fileext; /** * Holds generated checksum and gets set as a Project Property. */ private String property; /** * Holds checksums for all files (both calculated and cached on disk). * Key: java.util.File (source file) * Value: java.lang.String (digest) */ private Map allDigests = new HashMap(); /** * Holds relative file names for all files (always with a forward slash). * This is used to calculate the total hash. * Key: java.util.File (source file) * Value: java.lang.String (relative file name) */ private Map relativeFilePaths = new HashMap(); /** * Property where totalChecksum gets set. */ private String totalproperty; /** * Whether or not to create a new file. * Defaults to <code>false</code>. */ private boolean forceOverwrite; /** * Contains the result of a checksum verification. ("true" or "false") */ private String verifyProperty; /** * Resource Collection. */ private FileUnion resources = null; /** * Stores SourceFile, DestFile pairs and SourceFile, Property String pairs. */ private Hashtable includeFileMap = new Hashtable(); /** * Message Digest instance */ private MessageDigest messageDigest; /** * is this task being used as a nested condition element? */ private boolean isCondition; /** * Size of the read buffer to use. */ private int readBufferSize = 8 * 1024; /** * Formater for the checksum file. */ private MessageFormat format = FormatElement.getDefault().getFormat(); /** * Sets the file for which the checksum is to be calculated. * @param file a <code>File</code> value */ public void setFile(File file) { this.file = file; } /** * Sets the root directory where checksum files will be * written/read * @param todir the directory to write to * @since Ant 1.6 */ public void setTodir(File todir) { this.todir = todir; } /** * Specifies the algorithm to be used to compute the checksum. * Defaults to "MD5". Other popular algorithms like "SHA" may be used as well. * @param algorithm a <code>String</code> value */ public void setAlgorithm(String algorithm) { this.algorithm = algorithm; } /** * Sets the MessageDigest algorithm provider to be used * to calculate the checksum. * @param provider a <code>String</code> value */ public void setProvider(String provider) { this.provider = provider; } /** * Sets the file extension that is be to used to * create or identify destination file. * @param fileext a <code>String</code> value */ public void setFileext(String fileext) { this.fileext = fileext; } /** * Sets the property to hold the generated checksum. * @param property a <code>String</code> value */ public void setProperty(String property) { this.property = property; } /** * Sets the property to hold the generated total checksum * for all files. * @param totalproperty a <code>String</code> value * * @since Ant 1.6 */ public void setTotalproperty(String totalproperty) { this.totalproperty = totalproperty; } /** * Sets the verify property. This project property holds * the result of a checksum verification - "true" or "false" * @param verifyProperty a <code>String</code> value */ public void setVerifyproperty(String verifyProperty) { this.verifyProperty = verifyProperty; } /** * Whether or not to overwrite existing file irrespective of * whether it is newer than * the source file. Defaults to false. * @param forceOverwrite a <code>boolean</code> value */ public void setForceOverwrite(boolean forceOverwrite) { this.forceOverwrite = forceOverwrite; } /** * The size of the read buffer to use. * @param size an <code>int</code> value */ public void setReadBufferSize(int size) { this.readBufferSize = size; } /** * Select the in/output pattern via a well know format name. * @param e an <code>enumerated</code> value * * @since 1.7.0 */ public void setFormat(FormatElement e) { format = e.getFormat(); } /** * Specify the pattern to use as a MessageFormat pattern. * * <p>{0} gets replaced by the checksum, {1} by the filename.</p> * @param p a <code>String</code> value * * @since 1.7.0 */ public void setPattern(String p) { format = new MessageFormat(p); } /** * Files to generate checksums for. * @param set a fileset of files to generate checksums for. */ public void addFileset(FileSet set) { add(set); } /** * Add a resource collection. * @param rc the ResourceCollection to add. */ public void add(ResourceCollection rc) { if (rc == null) { return; } resources = (resources == null) ? new FileUnion() : resources; resources.add(rc); } /** * Calculate the checksum(s). * @throws BuildException on error */ public void execute() throws BuildException { isCondition = false; boolean value = validateAndExecute(); if (verifyProperty != null) { getProject().setNewProperty( verifyProperty, (value ? Boolean.TRUE.toString() : Boolean.FALSE.toString())); } } /** * Calculate the checksum(s) * * @return Returns true if the checksum verification test passed, * false otherwise. * @throws BuildException on error */ public boolean eval() throws BuildException { isCondition = true; return validateAndExecute(); } /** * Validate attributes and get down to business. */ private boolean validateAndExecute() throws BuildException { String savedFileExt = fileext; if (file == null && (resources == null || resources.size() == 0)) { throw new BuildException( "Specify at least one source - a file or a resource collection."); } if (!(resources == null || resources.isFilesystemOnly())) { throw new BuildException("Can only calculate checksums for file-based resources."); } if (file != null && file.exists() && file.isDirectory()) { throw new BuildException("Checksum cannot be generated for directories"); } if (file != null && totalproperty != null) { throw new BuildException("File and Totalproperty cannot co-exist."); } if (property != null && fileext != null) { throw new BuildException("Property and FileExt cannot co-exist."); } if (property != null) { if (forceOverwrite) { throw new BuildException( "ForceOverwrite cannot be used when Property is specified"); } int ct = 0; if (resources != null) { ct += resources.size(); } if (file != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -