From: Brett Porter Date: Sun, 1 Jan 2006 02:58:35 +0000 (+0000) Subject: use new digester and clean up related code X-Git-Tag: archiva-0.9-alpha-1~1014 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1c39abca5704dfd68f69bbb97375a8d948a22249;p=archiva.git use new digester and clean up related code git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@360475 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/maven-repository-indexer/pom.xml b/maven-repository-indexer/pom.xml index 09d2d5481..bd7dd2c88 100644 --- a/maven-repository-indexer/pom.xml +++ b/maven-repository-indexer/pom.xml @@ -23,7 +23,6 @@ 4.0.0 maven-repository-indexer - jar Maven Repository Indexer @@ -50,10 +49,8 @@ plexus-container-default - junit - junit - 3.8.1 - test + org.apache.maven.repository + maven-repository-utils diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java index 36d09e2d1..11d288ec5 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java @@ -21,13 +21,10 @@ import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.maven.artifact.Artifact; +import org.apache.maven.repository.digest.Digester; -import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; -import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Enumeration; import java.util.zip.ZipEntry; @@ -66,9 +63,8 @@ public class ArtifactRepositoryIndex private Analyzer analyzer; - private static final int CHECKSUM_BUFFER_SIZE = 16384; - - private static final int BYTE_MASK = 0xFF; + /** @plexus.requirement */ + private Digester digester; /** * method to get the Analyzer used to create indices @@ -136,8 +132,8 @@ public class ArtifactRepositoryIndex ZipFile jar; try { - sha1sum = byteArrayToHexStr( createChecksum( artifact.getFile(), "SHA-1" ) ); - md5sum = byteArrayToHexStr( createChecksum( artifact.getFile(), "MD5" ) ); + sha1sum = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); + md5sum = digester.createChecksum( artifact.getFile(), Digester.MD5 ); jar = new ZipFile( artifact.getFile() ); } catch ( NoSuchAlgorithmException e ) @@ -189,77 +185,6 @@ public class ArtifactRepositoryIndex } } - /** - * Convert an incoming array of bytes into a string that represents each of - * the bytes as two hex characters. - * - * @param data - * @todo move to utilities - */ - private static String byteArrayToHexStr( byte[] data ) - { - String output = ""; - - for ( int cnt = 0; cnt < data.length; cnt++ ) - { - //Deposit a byte into the 8 lsb of an int. - int tempInt = data[cnt] & BYTE_MASK; - - //Get hex representation of the int as a string. - String tempStr = Integer.toHexString( tempInt ); - - //Append a leading 0 if necessary so that each hex string will contain 2 characters. - if ( tempStr.length() == 1 ) - { - tempStr = "0" + tempStr; - } - - //Concatenate the two characters to the output string. - output = output + tempStr; - } - - return output.toUpperCase(); - } - - /** - * Create a checksum from the specified metadata file. - * - * @param file The file that will be created a checksum. - * @param algo The algorithm to be used (MD5, SHA-1) - * @return - * @throws FileNotFoundException - * @throws NoSuchAlgorithmException - * @throws IOException - * @todo move to utility class - */ - private static byte[] createChecksum( File file, String algo ) - throws FileNotFoundException, NoSuchAlgorithmException, IOException - { - MessageDigest digest = MessageDigest.getInstance( algo ); - - InputStream fis = new FileInputStream( file ); - try - { - byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE]; - int numRead; - do - { - numRead = fis.read( buffer ); - if ( numRead > 0 ) - { - digest.update( buffer, 0, numRead ); - } - } - while ( numRead != -1 ); - } - finally - { - fis.close(); - } - - return digest.digest(); - } - private boolean addIfClassEntry( ZipEntry entry, StringBuffer classes ) { boolean isAdded = false;