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.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.security.AccessController;
28 import java.security.MessageDigest;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.PrivilegedAction;
33 * Applet that takes a file on the local filesystem and checksums it for sending to the server.
35 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37 public class ChecksumApplet
40 private static final int CHECKSUM_BUFFER_SIZE = 8192;
42 private static final int BYTE_MASK = 0xFF;
44 private JProgressBar progressBar;
48 setLayout( new BorderLayout() );
49 progressBar = new JProgressBar();
50 progressBar.setStringPainted( true );
51 add( progressBar, BorderLayout.CENTER );
52 JLabel label = new JLabel( "Checksum progress: " );
53 add( label, BorderLayout.WEST );
56 public String generateMd5( final String file )
57 throws IOException, NoSuchAlgorithmException
59 Object o = AccessController.doPrivileged( new PrivilegedAction()
65 return checksumFile( file );
67 catch ( NoSuchAlgorithmException e )
69 return "Error checksumming file: " + e.getMessage();
71 catch ( FileNotFoundException e )
73 return "Couldn't find the file. " + e.getMessage();
75 catch ( IOException e )
77 return "Error reading file: " + e.getMessage();
84 protected String checksumFile( String file )
85 throws NoSuchAlgorithmException, IOException
87 MessageDigest digest = MessageDigest.getInstance( "MD5" );
89 long total = new File( file ).length();
90 InputStream fis = new FileInputStream( file );
94 byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
98 numRead = fis.read( buffer );
101 digest.update( buffer, 0, numRead );
102 totalRead += numRead;
103 progressBar.setValue( (int) ( totalRead * progressBar.getMaximum() / total ) );
106 while ( numRead != -1 );
113 return byteArrayToHexStr( digest.digest() );
116 protected static String byteArrayToHexStr( byte[] data )
120 for ( int cnt = 0; cnt < data.length; cnt++ )
122 //Deposit a byte into the 8 lsb of an int.
123 int tempInt = data[cnt] & BYTE_MASK;
125 //Get hex representation of the int as a string.
126 String tempStr = Integer.toHexString( tempInt );
128 //Append a leading 0 if necessary so that each hex string will contain 2 characters.
129 if ( tempStr.length() == 1 )
131 tempStr = "0" + tempStr;
134 //Concatenate the two characters to the output string.
135 output = output + tempStr;
138 return output.toUpperCase();