1 package org.apache.maven.repository.applet;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 import java.applet.Applet;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.security.AccessController;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.PrivilegedAction;
34 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
36 public class ChecksumApplet
39 private static final int CHECKSUM_BUFFER_SIZE = 8192;
41 private static final int BYTE_MASK = 0xFF;
43 private JProgressBar progressBar;
47 setLayout( new BorderLayout() );
48 progressBar = new JProgressBar();
49 progressBar.setStringPainted( true );
50 add( progressBar, BorderLayout.CENTER );
51 JLabel label = new JLabel( "Checksum progress: " );
52 add( label, BorderLayout.WEST );
55 public String generateMd5( final String file )
56 throws IOException, NoSuchAlgorithmException
58 Object o = AccessController.doPrivileged( new PrivilegedAction()
64 return checksumFile( file );
66 catch ( NoSuchAlgorithmException e )
70 catch ( IOException e )
77 //noinspection ChainOfInstanceofChecks
78 if ( o instanceof IOException )
80 throw (IOException) o;
82 else if ( o instanceof NoSuchAlgorithmException )
84 throw (NoSuchAlgorithmException) o;
92 protected String checksumFile( String file )
93 throws NoSuchAlgorithmException, IOException
95 MessageDigest digest = MessageDigest.getInstance( "MD5" );
97 long total = new File( file ).length();
98 InputStream fis = new FileInputStream( file );
102 byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
106 numRead = fis.read( buffer );
109 digest.update( buffer, 0, numRead );
110 totalRead += numRead;
111 progressBar.setValue( (int) ( totalRead * progressBar.getMaximum() / total ) );
114 while ( numRead != -1 );
121 return byteArrayToHexStr( digest.digest() );
124 protected static String byteArrayToHexStr( byte[] data )
128 for ( int cnt = 0; cnt < data.length; cnt++ )
130 //Deposit a byte into the 8 lsb of an int.
131 int tempInt = data[cnt] & BYTE_MASK;
133 //Get hex representation of the int as a string.
134 String tempStr = Integer.toHexString( tempInt );
136 //Append a leading 0 if necessary so that each hex string will contain 2 characters.
137 if ( tempStr.length() == 1 )
139 tempStr = "0" + tempStr;
142 //Concatenate the two characters to the output string.
143 output = output + tempStr;
146 return output.toUpperCase();