1 package org.apache.archiva.applet;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.applet.Applet;
23 import java.awt.BorderLayout;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.security.AccessController;
30 import java.security.MessageDigest;
31 import java.security.NoSuchAlgorithmException;
32 import java.security.PrivilegedAction;
34 import javax.swing.JLabel;
35 import javax.swing.JProgressBar;
38 * Applet that takes a file on the local filesystem and checksums it for sending to the server.
41 public class ChecksumApplet
44 private static final int CHECKSUM_BUFFER_SIZE = 8192;
46 private static final int BYTE_MASK = 0xFF;
48 private JProgressBar progressBar;
52 setLayout( new BorderLayout() );
53 progressBar = new JProgressBar();
54 progressBar.setStringPainted( true );
55 add( progressBar, BorderLayout.CENTER );
56 JLabel label = new JLabel( "Checksum progress: " );
57 add( label, BorderLayout.WEST );
60 public String generateMd5( final String file )
61 throws IOException, NoSuchAlgorithmException
63 return AccessController.doPrivileged( new PrivilegedAction<String>()
69 return checksumFile( file );
71 catch ( NoSuchAlgorithmException e )
73 return "Error checksumming file: " + e.getMessage();
75 catch ( FileNotFoundException e )
77 return "Couldn't find the file: " + e.getMessage();
79 catch ( IOException e )
81 return "Error reading file: " + e.getMessage();
87 protected String checksumFile( String file )
88 throws NoSuchAlgorithmException, IOException
90 MessageDigest digest = MessageDigest.getInstance( "MD5" );
92 File f = new File( file );
93 long total = f.length();
94 InputStream fis = new FileInputStream( f );
98 byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
102 numRead = fis.read( buffer );
105 digest.update( buffer, 0, numRead );
106 totalRead += numRead;
107 progressBar.setValue( (int) ( totalRead * progressBar.getMaximum() / total ) );
110 while ( numRead != -1 );
117 return byteArrayToHexStr( digest.digest() );
120 protected static String byteArrayToHexStr( byte[] data )
124 for ( int cnt = 0; cnt < data.length; cnt++ )
126 //Deposit a byte into the 8 lsb of an int.
127 int tempInt = data[cnt] & BYTE_MASK;
129 //Get hex representation of the int as a string.
130 String tempStr = Integer.toHexString( tempInt );
132 //Append a leading 0 if necessary so that each hex string will contain 2 characters.
133 if ( tempStr.length() == 1 )
135 tempStr = "0" + tempStr;
138 //Concatenate the two characters to the output string.
139 output = output + tempStr;
142 return output.toUpperCase();