You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by bw...@apache.org on 2003/05/27 13:24:53 UTC
cvs commit: maven-new/core/src/java/org/apache/maven/util MD5Sum.java
bwalding 2003/05/27 04:24:53
Modified: core/src/java/org/apache/maven/util MD5Sum.java
Log:
o Locked down access to some methods
o Made md5 incremental (don't need to load entire file into memory)
o Changed encoder to be readable without a degree in bitwise logic.
o Still needs to have argument checking locked down.
Revision Changes Path
1.2 +39 -55 maven-new/core/src/java/org/apache/maven/util/MD5Sum.java
Index: MD5Sum.java
===================================================================
RCS file: /home/cvs/maven-new/core/src/java/org/apache/maven/util/MD5Sum.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MD5Sum.java 26 Apr 2003 16:52:17 -0000 1.1
+++ MD5Sum.java 27 May 2003 11:24:53 -0000 1.2
@@ -56,10 +56,8 @@
* ====================================================================
*/
-import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
-import java.io.InputStream;
import java.security.MessageDigest;
/**
@@ -81,13 +79,6 @@
/** Checksum */
private String checksum;
- /** Hex digits. */
- private static final char[] hexadecimal =
- {
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'a', 'b', 'c', 'd', 'e', 'f'
- };
-
/** Set the file to perform the checksum on.
*
* @param file The file.
@@ -110,7 +101,7 @@
*
* @param checksum The checksum.
*/
- public void setChecksum( String checksum )
+ protected void setChecksum( String checksum )
{
this.checksum = checksum;
}
@@ -125,71 +116,64 @@
}
/**
- * Encodes the 128 bit (16 bytes) MD5 into a 32 character String.
- *
+ * Encodes a 128 bit (16 bytes) byte array into a 32 character String.
+ * XXX I think it should at least throw an IllegalArgumentException rather than return null
* @param binaryData Array containing the digest
- *
- * @return Encoded MD5, or null if encoding failed
+ * @return Encoded hex string, or null if encoding failed
*/
- public String encode( byte[] binaryData )
+ protected String encode( byte[] binaryData )
{
-
if ( binaryData.length != 16 )
{
return null;
}
- char[] buffer = new char[32];
-
+ String result = "";
for ( int i = 0; i < 16; i++ )
{
- int low = (int) ( binaryData[i] & 0x0f );
- int high = (int) ( ( binaryData[i] & 0xf0 ) >> 4 );
- buffer[i * 2] = hexadecimal[high];
- buffer[i * 2 + 1] = hexadecimal[low];
+ String t = Integer.toHexString(binaryData[i] & 0xff);
+
+ if (t.length() == 1) {
+ result += "0" + t;
+ } else {
+ result += t;
+ }
}
- return new String( buffer );
+ return result;
}
- /** Pull in static content and store it
- *
- * @param file The file to read.
- *
- * @return The bytes of the file.
- *
- * @throws Exception If an error occurs reading in the file's bytes.
+ /**
+ * Perform the MD5-Sum work.
+ * @throws Exception If an error occurs while calculating the sum.
*/
- public byte[] getBytes( File file )
- throws Exception
+ public void execute() throws Exception
{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- InputStream stream = new FileInputStream( file );
-
- byte buf[] = new byte[1024];
- int len = 0;
-
- while ( ( len = stream.read( buf, 0, 1024 ) ) != -1 )
+ if ( file.exists() == false )
{
- baos.write( buf, 0, len );
+ //XXX should throw an exception here
+ System.err.println("Specified file " + file + " doesn't exist.");
}
- return baos.toByteArray();
- }
+ MessageDigest md5digester = MessageDigest.getInstance("MD5");
- /** Perform the MD5-Sum work.
- *
- * @throws Exception If an error occurs while calculating the sum.
- */
- public void execute()
- throws Exception
- {
- if ( file.exists() == false )
+ FileInputStream in = null;
+ try
{
- System.err.println( "Specified file " + file + " doesn't exist." );
+ byte[] buf = new byte[4096];
+ in = new FileInputStream(file);
+ while (true)
+ {
+ int read = in.read(buf);
+ if (read == -1)
+ break;
+ md5digester.update(buf, 0, read);
+ }
}
-
- MessageDigest md5Helper = MessageDigest.getInstance( "MD5" );
- setChecksum( encode( md5Helper.digest( getBytes( file ) ) ) );
+ finally
+ {
+ IOUtility.close(in);
+ }
+ setChecksum(encode(md5digester.digest()));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org